I am sure this can be handled much easier with packages such as Tidyverse. However, I am trying to solve it by just simply using base R. To illustrate the problem I've used the "mtcars" dataset.
Example: Subset only for cars with MPG > 20
1.) first intuition of mine was to try the following:
mtcars[mtcars$mpg>20]
Which, rather unsurprisingly, does not work.
2.) Second, I've realized that I get an output when using:
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:
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?
Thank you very much! Chris
Subsetting in R
requires both rows an columns so you can use ,
:
#Code
new <- mtcars[mtcars$mpg>20,]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.