在 R 中选择具有值范围的列 [英] Selecting columns with range of values in R

查看:48
本文介绍了在 R 中选择具有值范围的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集如下

date    A   B   C   D   E   F   G   H   I   J   K   L   M   N
2001    2   3   5   9   2   24  50  2   11  37  9   2   24  50
2002    3   14  14  5   2   21  28  3   14  14  2   3   2   8
2003    0   12  2   3   4   29  30  0   12  2   3   4   3   30
2004    1   3   3   2   2   1   4   1   3   3   2   2   1   4
2005    0   0   2   0   2   1   1   0   0   2   0   2   1   1
2006    0   0   0   0   0   0   0   0   0   0   0   0   0   0
2007    0   1   0   1   0   1   0   0   1   0   1   0   1   0
2008    0   0   1   1   0   0   0   0   0   1   1   0   0   0
2009    0   0   0   1   0   0   0   0   0   0   1   0   0   0
2010    0   0   0   0   0   1   0   0   0   0   0   0   1   0

从这个集合中我只想选择那些至少有一个大于 20 的值的列.我想要的集合如下

from this set I want to select only those columns which as at least one value greater than 20. My desired set is as follows

date    F   G   J   M   N
2001    24  50  37  24  50
2002    21  28  14  2   8
2003    29  30  2   3   30
2004    1   4   3   1   4
2005    1   1   2   1   1
2006    0   0   0   0   0
2007    1   0   0   1   0
2008    0   0   1   0   0
2009    0   0   0   0   0
2010    1   0   0   1   0

我尝试使用

mydf<-mydf[,apply(mydf,2,function(z) any(z>20))]

但我没有得到结果.我有包含 500 多列的数据集.

but I'm not getting the result. I have dataset containing more than 500 columns.

如何过滤具有特定值范围的列?

How can I filter columns with specific range of values?

推荐答案

如果您想保留非数字列,这可能是一个稍微安全一些的版本:

This may be a slightly safer version if you want to keep the non-numeric columns:

mydf[, sapply(mydf, function(col) !is.numeric(col) || any(col >= 20)), drop = FALSE]

如果你真的只想要数字:

And if you really want just the numerics:

mydf[, sapply(mydf, function(col) is.numeric(col) && any(col >= 20)), drop = FALSE]

这篇关于在 R 中选择具有值范围的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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