I need to remove a participant from a data set in R, but struggling to find an easy way to do so. I identified the participant in the data set via a category. I need to take out the participants data from the entire environment. How do I do it?
I tried googling it and couldn't find a simple answer.
CodePudding user response:
In base R there is a subset
function. Here's an example using the built in iris
dataframe:
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
iris2 <- subset(iris, iris$Species != "setosa")
head(iris2)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
The dplyr
package of the tidyverse has a filter
function for more complex operations.
CodePudding user response:
One way, make a new dataset (iris2) that doesn't not contain any rows where Species is not equal to "virginica",
iris2 <- iris[iris$Species != "virginica",]
Using filter from the tidyverse library:
library(tidyverse)
iris2 <- filter(iris, Species != "virginica")