简体   繁体   中英

How to deal with NA when using lappy in R

I have a data frame err consisting of 796 rows and 54432 columns

I have to check the columns that have values not exceeding 20 and -20.

This is my approach:

do.call(cbind, (lapply(err, function(x) if((all(x<20)  & all(x>-20))) return(x) )))

I Have NA values in all of the columns and after i got

Error in if ((all(x < 20) & all(x > -20))) return(x) :
  missing value where TRUE/FALSE needed

I update the command using !is.na as:

 do.call(cbind, (lapply(err, function(x) if(!is.na(all(x<20)  & all(x>-20))) return(x) )))

But in this case all the columns are reported and the filter does not work.

Any help?

由于我没有df示例,请检查这是否对您有用:

do.call("cbind", lapply(err, function(x) if(min(x, na.rm=T) > -20  & max(x, na.rm=T) < 20) return(x) ))

使用apply

err[apply(err, 2, function(x) min(x,na.rm=T) > -20 & max(x,na.rm=T) < 20)]

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