0

I have one big dataframe that needs to be populated within a loop, where, at each step, data for a different year are imported.

Let's say the big dataframe df1 is initialized like this:

date VAR 
2006-01-01 NA 
2006-01-02 NA 
2006-01-03 NA 
...
2007-01-01 NA 
2007-01-02 NA 
...
2008-01-01 NA 
2008-01-02 NA

At each step of the loop (i.e., a different year) I have such data df2:

date VAR
2006-01-01 NA
2006-01-02 NA
...

How could I "populate" df1 by values in df2, using some basic R functions, e.g. merge? I would also like to keep NA where data from df2 are missing. Thanks!

davide-p
  • 25
  • 4
  • Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – SamR Jul 20 '22 at 14:02
  • Partially, the use of `df1 <- merge(df1, df2, all = TRUE)` seems to work properly, but new rows are added instead of merging with the existing ones – davide-p Jul 20 '22 at 14:53
  • It seems unlikely that the behaviour you are looking for isn't covered by one of the many joins (left, right, inner, outer, semi, anti, cross, rolling). But if it truly isn't please post a question with [reproducible data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and expected output so we can see what it is you are trying to do. – SamR Jul 20 '22 at 15:28

0 Answers0