r - 如何过滤具有两列条件的数据框?

标签 r dataframe

这个问题在这里已经有了答案:





How to combine multiple conditions to subset a data-frame using "OR"?

(3 个回答)


5年前关闭。




我正在尝试从数据框中进行选择。问题是为什么我下面的最后一个查询返回所有 5 条记录而不是前两条记录?

> x <- c(5,1,3,2,4)
> y <- c(1,5,3,4,2)
> data <- data.frame(x,y)
> data
  x y
1 5 1
2 1 5
3 3 3
4 2 4
5 4 2
> data[data$x > 4 || data$y > 4]
  x y
1 5 1
2 1 5
3 3 3
4 2 4
5 4 2

最佳答案

(1) 对于选择数据(子集),我强烈推荐 subset函数来自 plyr由 Hadley Wickhm 编写的包,它更干净且易于使用:

library(plyr)
subset(data, x > 4 | y > 4)

更新:

有更新版本的 plyrdplyr ( here ) 也来自 Hadley,但据说更快更容易使用。如果你见过像 %.% 这样的运算符(operator)或 %>% ,您知道他们使用 dplyr 链接操作.
result <- data %>%
          filter(x>4 | y>4)  #NOTE filter(condition1, condition2..) for AND operators.

(2) |确实存在一些差异和 || :

您可以通过以下方式查看帮助手册:?'|'

The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.


> c(1,1,0) | c(0,0,0)
[1]  TRUE  TRUE FALSE
> c(1,1,0) || c(0,0,0)
[1] TRUE

根据您的问题,您所做的基本上是 data[TRUE] , ...将返回完整的数据帧。

关于r - 如何过滤具有两列条件的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20084462/

相关文章:

r - 在函数中使用 ddply 并包含感兴趣的变量作为参数

r - 在R中,如何建立一个包含一些引用列表中较早元素的元素的列表?

r - 基于多种条件的数据帧的多重子集

python - 如何逐行读取Excel文件并将其传递到curl命令中?

python - 如何将条件应用于 pandas iloc

r - geom_point 在 geom_极地饼图(圆环图)图表的开头引入偏移

R如何根据矩阵值堆叠图像

R 插入符号 glmnet 标准化 = FALSE

python - 将 pandas 数据框中的列值乘以列标题

python - 如何保留最新值并删除列中的所有其他值( Pandas )