[英]Is there a quick way in base R to subset a data frame based on an operational relator? (e.g. MPG >20)
I am sure this can be handled much easier with packages such as Tidyverse.我相信使用 Tidyverse 等软件包可以更轻松地处理这件事。 However, I am trying to solve it by just simply using base R.但是,我试图通过简单地使用基础 R 来解决它。 To illustrate the problem I've used the "mtcars" dataset.为了说明问题,我使用了“mtcars”数据集。
Example: Subset only for cars with MPG > 20示例:仅适用于 MPG > 20 的汽车的子集
1.) first intuition of mine was to try the following: 1.)我的第一个直觉是尝试以下方法:
mtcars[mtcars$mpg>20]
Which, rather unsurprisingly, does not work.毫不奇怪,这不起作用。
2.) Second, I've realized that I get an output when using: 2.)其次,我意识到我在使用时得到了 output:
mtcars$mpg[mtcars$mpg>20]
[1] 21.0 21.0 22.8 21.4 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4
3.) This is not what I want though - I want keep the whole DF so I did the following: 3.)这不是我想要的——我想保留整个 DF,所以我做了以下事情:
mtcars$mpg>20 #gives me the Boolean values TRUE, FALSE
mtcars$newcolum <- mtcars$mpg>20 #creating a new column
subset(mtcars, newcolum==TRUE) #subsetting
This leads to the subsetted data frame I intended to extract.这导致我打算提取的子集数据框。 However, it feels like this is quite tedious and I am overcomplicating the steps.但是,感觉这很乏味,而且我的步骤过于复杂。 Do you have any helpful advice on how the same output could be achieved faster?您对如何更快地实现相同的 output 有任何有用的建议吗?
Thank you very much!非常感谢! Chris克里斯
Subsetting in R
requires both rows an columns so you can use ,
: R
中的子集需要两行一列,因此您可以使用,
:
#Code
new <- mtcars[mtcars$mpg>20,]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.