0

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
DSA
  • 655
  • 5
  • 13

1 Answers1

1
df1$status[df1$ID %in% df2$ID] <- df2$status[df2$ID %in% df1$ID]

What about this? You only have to fill the condition in the assignment

aserrin55
  • 350
  • 5
  • 15
  • Hey, thanks for the reply but it happened again, it doesn't work in the example data above.There is no replacing in the values. – DSA Apr 09 '18 at 11:20
  • `ID year status 1 A1 2000 1 2 A2 2001 1 3 B1 2002 0 4 B2 2003 1 5 C1 2004 0 6 C2 2005 1` I'm obtaining this output.. – aserrin55 Apr 10 '18 at 11:27
  • Sorry, I meant that It doesn't work on my real data as in the hypothetical data above. It does indeed work the data created above. – DSA Apr 10 '18 at 13:50