gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:09:47 26 4
gpt4 key购买 nike

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





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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com