say I have:
T1 <- c("A","B","C","D","E", "M")
T2 <- c("F","G","H","I","J", "K")
score1 <- c(1,2,3,4,5, 6)
score3 <- c(7,8,9,10,11, 12)
score2 <- c(13, 14, 15, 16, 17, 18)
df1 <- data.frame(T2, T1, score1, score2, score3)
T1 <- c("A","D","C","B","K")
T2 <- c("f","g","h","i","j")
score1 <- c(-1,-2,-3,-4,-5)
score2 <- c(-13, -14, -15, -16, -17)
df2 <- data.frame(T1, T2, score1, score2)
> df1
T2 T1 score1 score2 score3
1 F A 1 13 7
2 G B 2 14 8
3 H C 3 15 9
4 I D 4 16 10
5 J E 5 17 11
6 K M 6 18 12
> df2
T1 T2 score1 score2
1 A f -1 -13
2 D g -2 -14
3 C h -3 -15
4 B i -4 -16
5 L j -5 -17
df1
represents original values but then i have an updated df2
that recorded only some of the same variables. But df2
is more up-to-date so I want to replace all the values (for all matching columns) when df1$T1
matches df2$T1
.
I thought something like df1[match(df1$T1, df2$T1),] <- df2
but it doesn't work when they are different dimensions, also it won't match along the same colnames. Joining dfs is tedious because it doesn't actually replace the values (it adds a new column like T2.y, score1.y, etc.)
I want to get something like:
> df1
T2 T1 score1 score2 score3
1 f A -1 -13 7
2 i B -4 -16 8
3 h C -3 -15 9
4 g D -2 -14 10
5 J E 5 17 11
6 K M 6 18 12
CodePudding user response:
The new dplyr
rows_update
makes this easy.
library(dplyr)
df1 %>% rows_update(df2, by = 'T1', unmatched = "ignore")
# T2 T1 score1 score2 score3
#1 f A -1 -13 7
#2 i B -4 -16 8
#3 h C -3 -15 9
#4 g D -2 -14 10
#5 J E 5 17 11
#6 K M 6 18 12