You can use the following custom function to add one or more columns to a data frame in R if they do not already exist:
add_cols function(df, cols) {
add %in% names(df)]
if(length(add) != 0) df[add] return(df)
}
The following example shows how to use this syntax in practice.
Example: Add Column If It Does Not Exist in R
Suppose we have the following data frame in R:
#create data frame df frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B'), position=c('Gu', 'Fo', 'Fo', 'Fo', 'Gu', 'Gu', 'Fo'), points=c(18, 22, 19, 14, 14, 11, 20)) #view data frame df team position points 1 A Gu 18 2 A Fo 22 3 A Fo 19 4 A Fo 14 5 B Gu 14 6 B Gu 11 7 B Fo 20
Suppose we would like to add the following columns to the data frame if they do not already exist:
- points
- assists
- rebounds
We can use a custom function called add_cols to do so:
#define custom function to add columns to data frame if they do not exist add_cols function(df, cols) { add %in% names(df)] if(length(add) !=0 ) df[add] return(df) } #add three columns if they don't already exist df points', 'assists', 'rebounds')) #view updated data frame df team position points assists rebounds 1 A Gu 18 NA NA 2 A Fo 22 NA NA 3 A Fo 19 NA NA 4 A Fo 14 NA NA 5 B Gu 14 NA NA 6 B Gu 11 NA NA 7 B Fo 20 NA NA
Notice that the assists and rebounds columns were added to the data frame while the points column was not since it already existed.
Also note that R simply fills in each value in the new columns with NA values.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Add Column to Data Frame Based on Other Columns in R
How to Add an Index (numeric ID) Column to a Data Frame in R
How to Add an Empty Column to a Data Frame in R