Using key identifying columns, add additional columns to an existing data frame. This function allows you to match new columns based on specified IDs and you can choose what columns to add. Additionally you can specify the column number at which to add the new columns, so they are not added to the end of the data frame. Helpful for adding environmental or taxonomic data to your quadrat data.
Arguments
- data
A data frame you want to add columns to.
- add
A data frame with columns you want to add to
data
.- cols
The column names from
add
that you wish to add todata
.- data_id
The ID column in
data
that will be used to match rows inadd
.- add_id
The ID column in
add
that will be used to match rows indata
.- number
The column number to start at to add the new columns, so they are not added to the end of the data frame. If not specified they will be added to the end of the data frame by default.
Examples
Sites <- as.factor(c("One", "One", "One", "Two", "Two", "Three"))
Transect <- as.factor(c("1-Deep", "1-Shallow", "2-Shallow", "1-Shallow", "1-Deep", "1"))
coral_name <- c("Acropora.sp", "Leptastrea.sp", "Sinularia.sp", "Psammocora.sp", "
Psammocora.sp", "Leptastrea.sp")
prop_cover <- c(0.1, 0.6, 0.4, 0.9, 0.2, 0.5)
coral_cover <- data.frame(Sites, Transect, coral_name, prop_cover)
corals <- c("Acropora.sp", "Leptastrea.sp", "Psammocora.sp")
lifehistory <- c("compeditive", "weedy", "stresstolerant")
functionalgroup <- c("hardcoral", "hardcoral", "hardcoral")
coral_info <- data.frame(corals, lifehistory, functionalgroup)
add_data(data = coral_cover, add = coral_info, cols = c("lifehistory", "functionalgroup"),
data_id = "coral_name", add_id = "corals", number = 4)
#> Sites Transect coral_name lifehistory functionalgroup prop_cover
#> 1 One 1-Deep Acropora.sp compeditive hardcoral 0.1
#> 2 One 1-Shallow Leptastrea.sp weedy hardcoral 0.6
#> 3 One 2-Shallow Sinularia.sp <NA> <NA> 0.4
#> 4 Two 1-Shallow Psammocora.sp stresstolerant hardcoral 0.9
#> 5 Two 1-Deep \n Psammocora.sp <NA> <NA> 0.2
#> 6 Three 1 Leptastrea.sp weedy hardcoral 0.5