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
dput(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
dput(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")
All I want to do is add the df$s values in df to df2 where df$bv == df2$bv. df2 will have a lot more of the same bv values in df, so there will be some repetitive s values.
I was trying the following
newDF <- ddply(df2, .(bv,tv), summarise, s = df[df$bv %in% df2$bv,]$s)
Although this is not working for me, maybe it’s because I don’t really understand the variable arguments in this function.
Really all other columns are arbitrary at this point, but I would like to keep the entire dataframe intact.
This will pull the corresponding ‘s’-items in ‘df’ into the matched rows of ‘df2’:
This is going to be a lot more efficient than ‘subset()’-ting and ‘merge()’-ing. Oooops. I didn’t see the plyr part. It’s going to be a lot faster than any plyr method, too, but that’s cuz’ I’m a base-R guy. If you want to do it with plyr then this delivers what I think you asked for: