寻找空的列

2020-04-09  本文已影响0人  东方不赞

今天学到了一个小技巧,对于data.frame格式的数据,有些列是没有一个数据的,也就是整列都是NA值。
处理的思路是:使用apply函数对每一列进行处理,函数是自定义的函数。

自定义函数

library(dplyr)
myfun=function(vector){
  tt=vector %>% is.na() %>% table() %>% names()
  ifelse((length(tt)==1 & tt=="TRUE"),return(0),return(1))
}

调用函数,将返回值存入count

count=apply(laml.data, 2, myfun)

删除空列

laml.data=laml.data[,which(count==1)]

其他

na.omit(x)
上一篇 下一篇

猜你喜欢

热点阅读