-1

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.

  • 1
    It will be much easier for people to give you relevant assistance if you can provide some specifics in your question. What does your data look like? (For this, it is most helpful if you can share a bit of data in the form of code, e.g. by using `dput(head(YOUR_DATA))` and pasting the output into your question. That will produce code that will create a perfect copy of the first six rows of your data (I'm assuming it's a data frame), formats and structure and all. We also don't know what you mean specifically by "category" and "environment." – Jon Spring Dec 26 '22 at 00:36
  • So I am working on a data set of animals, and under the sector "species" there is a non-existing animal and I need to remove that particular response from the dataset. Remove the row that has the non existent species name in. In SPSS I would just clear the row that had the nonexistent species title, but in R I don't know how to remove it. – user20861379 Dec 26 '22 at 00:40
  • So.... can you give an example of data? – Jon Spring Dec 26 '22 at 00:44
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jon Spring Dec 26 '22 at 00:46
  • Look for the documentation for the subset function: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/subset – User 123732 Dec 26 '22 at 02:21

1 Answers1

-1

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.

SteveM
  • 2,226
  • 3
  • 12
  • 16
  • Hi, thanks for this! The textbook referred to dplyr but I didn't realise it was a package! I managed to do it more hard removal like this: data[-c(15), ] data <- data[-c(15), ] #successfully removed the outlier by changing the dataframe But thank you that is much more useful!! – user20861379 Dec 26 '22 at 02:46