从数据帧中删除某些值为NA的列 [英] Remove columns from dataframe where some of values are NA

查看:156
本文介绍了从数据帧中删除某些值为NA的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,其中一些值是NA。我想删除这些列。



我的数据框看起来像这样

  v1 v2 
1 1 NA
2 1 1
3 2 2
4 1 1
5 2 2
6 1 NA

我试图估计col的意思,选择列表示!= NA。我试过这个声明,它不起作用。

  data = subset(Itun,select = c(is.na(colMeans Itun))))

我收到错误,


错误:'x'必须是至少二维的数组


任何人给我一些帮助?

解决方案

数据:

  Itun<  -  data.frame(v1 = c(1,1,2,1,2,1),v2 = c(NA,1,2,1,2,NA))

这将删除所有包含至少一个 NA的列

  Itun [,colSums(is.na(Itun))== 0] 

另一种方法是使用 apply

  Itun [,apply(Itun,2,function(x)!any(is.na(x)))] 


I have a dataframe where some of the values are NA. I would like to remove these columns.

My data.frame looks like this

    v1   v2 
1    1   NA 
2    1    1 
3    2    2 
4    1    1 
5    2    2 
6    1   NA

I tried to estimate the col mean and select the column means !=NA. I tried this statement, it does not work.

data=subset(Itun, select=c(is.na(colMeans(Itun))))

I got an error,

error : 'x' must be an array of at least two dimensions

Can anyone give me some help?

解决方案

The data:

Itun <- data.frame(v1 = c(1,1,2,1,2,1), v2 = c(NA, 1, 2, 1, 2, NA)) 

This will remove all columns containing at least one NA:

Itun[ , colSums(is.na(Itun)) == 0]

An alternative way is to use apply:

Itun[ , apply(Itun, 2, function(x) !any(is.na(x)))]

这篇关于从数据帧中删除某些值为NA的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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