r - How do I select a value based on two conditions from a data frame and then paste it into a cell in another data frame? -
i have 2 data frames. 1 has trials in 2 objects appeared. has information on how 2 objects relate (visual similarity). each row (trial) in first data frame, pull names of objects , use them specific rows in second data frame. this:
trial probepic target_pic vissim 1 1 robot1 robot6 na 2 2 robot1 robot3 na 3 3 robot5 robot6 na 4 4 robot2 robot1 na 5 5 robot3 robot9 na 6 6 robot14 robot9 na rob1 rob2 sim 1 robot1 robot1 na 2 robot2 robot1 2.88 3 robot3 robot1 3.75 4 robot4 robot1 1.63 5 robot5 robot1 3.63 6 robot6 robot1 2.50
etc. want use probepic , target_pic variables select value of sim second data frame , paste vissim in first data frame. i've been playing around subset , can correct value using code:
subset(vissim, rob1=="robot1" & rob2=="robot2")$sim
but want use probepic
, target_pic
variables each row instead of "robot1" , "robot2." , apply function each row in first data frame. so, in each row, function in probepic
column , match value rob1
column , in target_pic
column , match value rob2
column. specify unique row in second data frame. then, take value of sim
unique row , paste vissim
.
i think want merge data sets together. try following:
newdf <- merge(df1, df2, by.x=c("probepic", "target_pic"), by.y=c("rob1", "rob2"), all=t)
data
df1 <- read.table(header=t, text=" trial probepic target_pic vissim 1 robot1 robot6 na 2 robot1 robot3 na 3 robot5 robot6 na 4 robot2 robot1 na 5 robot3 robot9 na 6 robot14 robot9 na") df2 <- read.table(header= t, text=" rob1 rob2 sim robot1 robot1 na robot2 robot1 2.88 robot3 robot1 3.75 robot4 robot1 1.63 robot5 robot1 3.63 robot6 robot1 2.50")