So I have a problem in R to update values in the main data frame by using a second data frame, plus matching IDs.
So here is the data files demos: First the main data I have like this:
df1 <- data.frame(ID = c("A1", "A2", "B1", "B2", "C1", "C2"),
year = 2000:2005, status = c("0","1","0","0","1","1"))
ID year status
A1 2000 0
A2 2001 1
B1 2002 0
B2 2003 0
C1 2004 1
C2 2005 1
And second data, that I want to replace "status" values into the main data.
df2 = data.frame(ID = c("A1", "B2","C1"), status = c("1", "1", "0"))
ID status
A1 1
B2 1
C1 0
Finally desired ouput is like this:
ID year status
A1 2000 1
A2 2001 1
B1 2002 0
B2 2003 1
C1 2004 0
C2 2005 1
As you can see, status column is updated with the second data right now. Well, I have tried merge/left join and I couldn't get a solution, some part of the data updated, some part not.
The solutions were given here, mostly for the datasets where the second one is bigger than the first dataset, which I had lots of NAs in the end as you can guess.
Also tried this code:
df1$status[df1$id %in% df2$id] <- df2$status
Result was the same. So If you can help me where the problem is (whether something should be muted or mutated), that would be great.