I have two data frames df1 and df2.
df1 <- data.frame(x1=c("A35", "A41", "A49"),
x2=c(8, 24, 33),
x3=c(15, 63, 54))
df2 <- data.frame(y1=c("A35", "A38", "A41", "A41", "A49"),
y2 = c(9, 20, 24, 32, 84))
I want to select the rows from the df2 based on the following three criteria
(1) The y1 of df2 matches x1 of df1;
(2) The y2 of df2 >= x2 of df1
(3) The y2 of df2 =< x3 of df1
The data meeting the criteria will be added to df1 as new columns. If the row(s) of df1 has more than one matches, those additional match(es) will be added as new columns as well.
The expected results are
data.frame(x1=c("A35", "A41", "A49"),
x2=c(8, 24, 33),
x3=c(15, 63, 54),
z1 = c("A35", "A41", ""),
z2 = c(9, 24,""),
z3 = c("", "A41", ""),
z4 = c("", 32, ""))
x1 x2 x3 z1 z2 z3 z4
A35 8 15 A35 9
A41 24 63 A41 24 A41 32
A49 33 54
Thanks in advance!
df2have 5 rows, but the resultant data frame only has 4 new columns? - Tim Biegeleisen