I want to merge variables with the same name so values from the y dataset overwrite those in the x datatset.
This code should produce a replica of b because a$V2 should be overwritten by b$V2.
Instead I get V2.x and V2.y
a = data.frame(c("A","B","C","D"), c("1","2"))
names (a) = c("V1","V2")
b = data.frame(c("A","B","C","D"), c("3","4"))
names (b) = c("V1","V2")
merge.data.frame(a,b, by.x = "V1", by.y = "V1", all.y = T,)
CodePudding user response:
It may be easier with rows_update
library(dplyr)
rows_update(a, b, by = 'V1')
Or do an assign (:=
) by joining with data.table
, which updates the column 'V2' in 'a' by the column ('V2') from 'b' data
library(data.table)
setDT(a)[b, V2 := i.V2, on = .(V1)]
CodePudding user response:
Do you mean this one:
a$V2 <- b$V2
V1 V2
1 A 3
2 B 4
3 C 3
4 D 4
CodePudding user response:
You can use ifelse
to override the values you want after merge
df <- merge.data.frame(a,b, by = "V1")
df$V2 <- ifelse(df$V2.x == df$V2.y , df$V2.x , df$V2.y)
df |> subset(select = c(V1 , V2))
Output
V1 V2
1 A 3
2 B 4
3 C 3
4 D 4