I want to update the rows of data.frame
df1
with the rows of data.frame
df2
. Any hint?
df1 <-
data.frame(
"V1" = LETTERS[1:4]
, "V2" = 1:4
, "V3" = 7:10
)
df1
V1 V2 V3
1 A 1 7
2 B 2 8
3 C 3 9
4 D 4 10
df2 <-
data.frame(
"V1" = c("A","D")
, "V2" = c(5, 7)
, "V3" = c(12, 15)
)
df2
V1 V2 V3
1 A 5 12
2 D 7 15
Required Output
V1 V2 V3
1 A 5 12
2 B 2 8
3 C 3 9
4 D 7 15
use dplyr 1.0.0
rows_update(df1, df2)
Matching, by = "V1"
V1 V2 V3
1 A 5 12
2 B 2 8
3 C 3 9
4 D 7 15
Try this:
df1[df1$V1 %in% df2$V1,c('V2','V3')] <- df2[df2$V1 %in% df1$V1,c('V2','V3')]
V1 V2 V3
1 A 5 12
2 B 2 8
3 C 3 9
4 D 7 15
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.