简体   繁体   English

基础 R 中是否有一种快速方法可以根据操作关系对数据帧进行子集化? (例如 MPG >20)

[英]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.

相关问题 如何根据基 R 中的另一个数据框对数据框进行子集化 - How to subset a data frame based on another data frame in base R 每个索引的子集或过滤器data.frame,例如每行逐列 - Subset or filter data.frame per indices e.g. column-wise per row 如何按字段类型(例如,数字、字符)对我的 data.frame 进行子集化? - How do I subset my data.frame by field type (e.g., numeric, character)? 您如何有效地为R中的图表(例如箱形图)子集数据? - How do you efficiently subset data for charts (e.g., boxplots) in R? R:如何在R中继承基本数据类型(例如,列表,向量) - R: How to inherit a base data type (e.g. list, vector) in R R将包含指数(例如E + 11,E + 20)的字符数据替换为动态数字0。 - R Replace character data containing exponent (e.g. E+11, E+20) with number of 0's that is dynamic R Studio不显示内联输出数据帧(例如,标题,尾巴……) - R Studio not displaying inline output data frame (e.g. header, tail…) How to search for words with asterisks and wildcards (eg, exampl*) in R(数据框中的词出现) - How to search for words with asterisks and wildcards (e.g., exampl*) in R (word appearance in a data frame) 使用DT格式R Shiny时,“数据”必须是二维的(例如,数据帧或矩阵) - 'data' must be 2-dimensional (e.g. data frame or matrix) while using DT format R Shiny r 中的 table() function - 是否有更好的方法,例如 dplyr? - table() function in r - is there a better way with e.g., dplyr?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM