Manipulating a data frame with contents from a different data frame similar to a SQL join
Question
Say I have a data frame with the contents:
Trial Person Time
1 John 1.2
2 John 1.3
3 John 1.1
1 Bill 2.3
2 Bill 2.5
3 Bill 2.7
and another data frame with the contents:
Person Offset
John 0.5
Bill 1.0
and I want to modify the original frame based on the appropriate value from the second. I could do this easily in any other language or in SQL, and I'm sure I could manage using for loops and what, but with everything else I see in R, I'm guessing it has special syntax to do this as a one-liner. So, if so, how? And if not, could you show how it could be done using loops. I haven't actually got around to learning looping in R yet since it has amazing things to simply extract and manipulate whatever values.
For reference, the output would:
Trial Person Time
1 John 0.7
2 John 0.8
3 John 0.6
1 Bill 1.3
2 Bill 1.5
3 Bill 1.7
Solution
There are many possibilities. Here is a simple one using merge()
and a simple column-wise subtraction in the enlarged data.frame
:
R> DF1 <- data.frame(trial=rep(1:3,2), \
Person=rep(c("John","Bill"), each=3), \
Time=c(1.2,1.3,1.1,2.3,2.5,2.7))
R> DF2 <- data.frame(Person=c("John","Bill"), Offset=c(0.5,1.0))
R> DF <- merge(DF1, DF2)
R> DF
Person trial Time Offset
1 Bill 1 2.3 1.0
2 Bill 2 2.5 1.0
3 Bill 3 2.7 1.0
4 John 1 1.2 0.5
5 John 2 1.3 0.5
6 John 3 1.1 0.5
R> DF$NewTime <- DF$Time - DF$Offset
R> DF
Person trial Time Offset NewTime
1 Bill 1 2.3 1.0 1.3
2 Bill 2 2.5 1.0 1.5
3 Bill 3 2.7 1.0 1.7
4 John 1 1.2 0.5 0.7
5 John 2 1.3 0.5 0.8
6 John 3 1.1 0.5 0.6
R>
OTHER TIPS
One liner:
transform(merge(d1,d2), Time=Time - Offset, Offset=NULL)