Home > Enterprise >  Check if a column exists and if not add it
Check if a column exists and if not add it

Time:07-01

I have a series of dataframes that are noncumulative. During the cleaning process I want to loop through the dataframe and check if certain columns exists and if they aren't present create them. I can't for the life of me figure out a method to do this. I am not package shy and prefer them to base.

Any direction is much appreciated.

CodePudding user response:

You can use this dummy data df and colToAdd columns to check if not exists to add

df <- data.frame(A = rnorm(5) , B = rnorm(5) , C = rnorm(5))

colToAdd <- c("B" , "D")

then apply the check if the column exists NULL produced else add your column e.g. rnorm(5)

add <- sapply(colToAdd , \(x) if(!(x %in% colnames(df))) rnorm(5))

data.frame(do.call(cbind , c(df , add)))

  • output
           A          B          C          D
1  1.5681665 -0.1767517  0.6658019 -0.8477818
2 -0.5814281 -1.0720196  0.5343765 -0.8259426
3 -0.5649507 -1.1552189 -0.8525945  1.0447395
4  1.2024881 -0.6584889 -0.1551638  0.5726059
5  0.7927576  0.5340098 -0.5139548 -0.7805733
  • Related