I am trying to update a df.1 with values from df.2 when the column values df.1$time and df.1$region matches df.2$time df.2$region, respectively
df.1
region, time, value
a, 1, 2
b, 1, 22
c, 1, 23
a, 2, na
b, 2, na
c, 2, na
df.1 = structure(list(region = c("a", "b", "c", "a", "b", "c"), time = c(1L,
1L, 1L, 2L, 2L, 2L), value = c(2L, 22L, 23L, NA, NA, NA)), .Names = c("region",
"time", "value"), row.names = c(NA, -6L), class = "data.frame")
df.2
region, time, value
a, 2, 45
b, 2, 54
c, 2, 56
df.2 = structure(list(region = c("a", "b", "c"), time = c(2L, 2L, 2L
), value = c(45L, 54L, 56L)), .Names = c("region", "time", "value"
), row.names = c(NA, -3L), class = "data.frame")
I am using merge(df.1, df.2, by=c(“region”, "time”), all.x=TRUE)
I am ending up new columns added up to the df.1 table, however I want only the values in df.1$value to be updated when a match is found. I also need to preserve all the values in df.1 table. Suggestions appreciated.
The desired ouput is as follows
region, time, value
a, 1, 2
b, 1, 22
c, 1, 23
a, 2, 45
b, 2, 54
c, 2, 56