R在DF2中按间隔聚合DF1中的值

时间:2016-03-18 03:22:00

标签: r aggregate

给定DF1$Ages,如何将其聚合以列出由DF2$Ages定义的区间内的行数?

例如,我想使用out1定义的时间间隔从DF1生成DF2$Ages

DF1 <- data.frame("Ages"=c(1,2,4,8,11,13,15,16))
DF1
#    Ages
#  1    1
#  2    2
#  3    4
#  4    8
#  5   11
#  6   13
#  7   15
#  8   16

DF2 <- data.frame("Ages" = c(3, 5, 9, 11, 12, 17), 
                  "Type" = c("N", "R", "N", "R", "N", "R"))
#   Ages Type
# 1    3    N
# 2    5    R
# 3    9    N
# 4   11    R
# 5   12    N
# 6   17    R

out1 <- data.frame("Counts"=c(2,1,1,1,0,3))
out1
#   Counts
# 1      2
# 2      1
# 3      1
# 4      1
# 5      0
# 6      3

即,

    2中的{li> DF1值属于(0,3]范围DF2$Ages, {li} 1DF1范围(3,5]范围DF2$Ages, {li} 1DF1范围(5,9]范围DF2$Ages, {li} 1DF1范围(9,11]范围DF2$Ages0中的{li> DF1值位于(11,12] DF2$Ages范围内, 3中的{li> DF1值位于(12,17]范围DF2$Ages范围内。

0 个答案:

没有答案