df
av bv tv u l value s
30 120 360 330 210 6600 0.005238424
35 125 360 325 200 6875 0.005028887
40 130 360 320 190 7150 0.004835468
45 135 360 315 180 7425 0.004656377
50 140 360 310 170 7700 0.004490078
55 145 360 305 160 7975 0.004335247
60 150 360 300 150 8250 0.004190739
65 155 360 295 140 8525 0.004055554
70 160 360 290 130 8800 0.003928818
75 165 360 285 120 9075 0.003809763
80 170 360 280 110 9350 0.003697711
输入(df)
df<-structure(list(av = c(30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
80), bv = c(120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170), tv = c(360, 360, 360, 360, 360, 360, 360, 360, 360, 360,
360), u = c(330, 325, 320, 315, 310, 305, 300, 295, 290, 285,
280), l = c(210, 200, 190, 180, 170, 160, 150, 140, 130, 120,
110), value = c(6600, 6875, 7150, 7425, 7700, 7975, 8250, 8525,
8800, 9075, 9350), s = c(0.005238424, 0.00502888704, 0.00483546830769231,
0.00465637688888889, 0.00449007771428572, 0.00433524744827586,
0.0041907392, 0.00405555406451613, 0.003928818, 0.00380976290909091,
0.00369771105882353)), .Names = c("av", "bv", "tv", "u", "l",
"value", "s"), row.names = c(1L, 13L, 25L, 37L, 49L, 61L, 73L,
85L, 97L, 109L, 121L), class = "data.frame")
df2
av bv tv u l value
30 120 0 0 0 0
30 120 20 0 0 0
30 120 40 10 0 550
30 120 60 30 0 1650
30 120 120 90 0 4950
30 120 180 150 30 6600
输入(df2)
df2<-structure(list(av = c(30, 30, 30, 30, 30, 30), bv = c(120, 120,
120, 120, 120, 120), tv = c(0, 20, 40, 60, 120, 180), u = c(0,
0, 10, 30, 90, 150), l = c(0, 0, 0, 0, 0, 30), value = c(0, 0,
550, 1650, 4950, 6600)), .Names = c("av", "bv", "tv", "u", "l",
"value"), row.names = c(1L, 2602L, 5203L, 7804L, 10405L, 13006L
), class = "data.frame")
我要做的就是将df$s
值添加df
到df2
where df$bv == df2$bv
。df2
中会有更多相同的bv
值df
,因此会有一些重复的s
值。
我正在尝试以下
newDF <- ddply(df2, .(bv,tv), summarise, s = df[df$bv %in% df2$bv,]$s)
虽然这对我不起作用,但也许是因为我并不真正理解这个函数中的变量参数。
在这一点上,实际上所有其他列都是任意的,但我想保持整个数据框完好无损。