如何删除仅包含NA的列? [英] How to delete columns that contain ONLY NAs?

查看:184
本文介绍了如何删除仅包含NA的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame,其中包含一些具有所有NA值的列,如何从data.frame中删除它们.

I have a data.frame containing some columns with all NA values, how can I delete them from the data.frame.

我可以使用功能

na.omit(...) 

指定一些其他参数?

推荐答案

一种方法:

df[, colSums(is.na(df)) != nrow(df)]

如果一列中NA的数量等于行数,则它必须完全是NA.

If the count of NAs in a column is equal to the number of rows, it must be entirely NA.

或类似地

df[colSums(!is.na(df)) > 0]

这篇关于如何删除仅包含NA的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆