Home > other >  conditionally adding columns to a list of dataframes
conditionally adding columns to a list of dataframes

Time:04-23

I have a list of dataframes with either 2 or 4 columns.

a <- data.frame(a=1:10,
                b=1:10,
                c=1:10,
                d=1:10)

b <- data.frame(a=1:10,
                b=1:10)

list_of_df <- list(a,b)

I want to add 2 empty columns to each dataframe with only 2 columns.

I've tried this lapply approach:

lapply(list_of_df, function(x) ifelse(ncol(x) < 4,x%>           
  •  Tags:  
  • r
  • Related