Home > Software design >  How do I add new data columns to a data frame but only for certain rows in R?
How do I add new data columns to a data frame but only for certain rows in R?

Time:12-02

I have a running master csv file with various data columns for 896 eucalypt species (i.e., 896 rows). I recently collected new columns of information in a separate data frame, but only for 474 of those 896 species. How do I add the new columns to the master data frame and make sure they are sorted into the correct rows?

For example (but here I am adding 3 species' new data to a master dataset of 5 species, instead of 474 to 896): I would like to merge the following 2 data frames,

> master
    Species Variable1 Variable2
1   regnans       400       0.1
2   cornuta       421       0.1
3    caesia       378       0.2
4 viminalis       397       0.3
5   plumula       401       0.1

and

> newdata
    Species NewVariable
1   regnans           5
2 viminalis           9
3   plumula           7

into this:

> master.updated
    Species Variable1 Variable2 NewVariable
1   regnans       400       0.1           5
2   cornuta       421       0.1          NA
3    caesia       378       0.2          NA
4 viminalis       397       0.3           9
5   plumula       401       0.1           7

CodePudding user response:

This will probably work, try sharing your data with dput(), it makes easier to others help!

library(dplyr)

full_join(master,newdata, by = "Species")

CodePudding user response:

This should work: df = merge(x = master, y = newdata, by = "Species", all.x = TRUE)

  •  Tags:  
  • r
  • Related