0

I have two dataframes one with the original information and the second one with corrections about the first observations. I would like to create a function or find a way to replace in multiple columns the information I have in my first dataframe with the new information I received. I have an ID to identify the observations that need to be replace but since so many columns will be changing for certain IDs I don´t know which will be the appropriate way of changing them.

My first data frame has 500 columns and 1000 observations and my second data frame has 100 columns and 800 observations that will change the original dataframe. I don´t know how to efficiently replace those values according to the ID

Here is an example of what the 2 dataframes look like, I need to replace in multiple columns just some values and a merge is not the most efficient options since I have more than 100 columns at least that will need changes in some of the observations.

I just need to replace the new info and keep the old one

enter image description here

Dataframe 1

|ID | X1 | X2 | X3 | X4 | XN |

|a1 | 1 | 1 | 1 | 1 | 1 |

|a2 | 2 | 2 | 2 | 2 | 2 |

|a3 | 3 | 3 | 3 | 3 | 3 |

|a4 | 4 | 4 | 4 | 4 | 4 |

|a5 | 5 | 5 | 5 | 5 | 5 |

|an | 6 | 6 | 6 | 6 | 6 |

dataframe 2

|ID | X1 | X2 | X4|

|a1 | 8 | | 4 |

|a3 | | | 2 |

|a4 | 2 | 9 | |

|an | 1 | | 3 |

The outcome should have the old values of dataframe 1 just with the replacements I got from dataframe 2

outcome

|ID | X1 | X2 | X3 | X4 | XN |

|a1 | 8 | 1 | 1 | 4 | 1 |

|a2 | 2 | 2 | 2 | 2 | 2 |

|a3 | 3 | 3 | 3 | 2 | 3 |

|a4 | 2 | 9 | 4 | 4 | 4 |

|a5 | 5 | 5 | 5 | 5 | 5 |

|an | 1 | 6 | 6 | 3 | 6 |

  • `dplyr::left_join(df1, df2, by = "id")` will join the two data frames by id. – Phil Sep 10 '21 at 05:10
  • Provide reproducible example, see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nad Pat Sep 10 '21 at 08:23
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 10 '21 at 11:34
  • I added a picture, to show a simple example of what the two dataframes look like. Hopefully it will make clear what I need to accomplish and is to replace the first one with the changes I see in the second one – Vanessa Pinedo Sep 10 '21 at 14:18
  • @VanessaPinedo pictures make it much harder for people to help you. Please share your data by pasting the output of ``dput(data)`` into your original question. Thank you. – user438383 Sep 10 '21 at 14:21
  • I don´t think what I did to show the tables is very good, I think the picture is better to understand – Vanessa Pinedo Sep 10 '21 at 15:10
  • @VanessaPinedo [This](https://meta.stackoverflow.com/a/285557/5784757) is why images are not a good way to share data. Please share your data by pasting the output of ``dput(data)`` into your original question. There's no way anyone can access your data in either format. Thank you. – user438383 Sep 10 '21 at 15:14

0 Answers0