简体   繁体   中英

How to loop through dataframes and delete empty columns (in R)?

I want to delete empty columns for multiple dataframes using for loop or lapply.

My dataframes always start with "test.xxx".

If I do it one by one as below, it works.

test.acc_consoles2 <- Filter(function(x)!all(is.na(x) || is.null(x) || x == "" || x == 0), test.acc_consoles2)

However, if I use for loop or lapply, it doesn't work. I tried the following code already.

for(i in lst) {
  i <- Filter(function(x)!all(is.na(x) || is.null(x) || x == "" || x == 0),x=i)
}

I want to have clean dataframes with no empty columns. Since there are 189 dataframes, I would like to use lapply or for loop.

We could use lapply and filter the columns from each dataframe in the list

output <- lapply(lst, function(df) 
     Filter(function(x)!all(is.na(x) || is.null(x) || x == "" || x == 0),df))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM