Filtering a data frame by values in a column

RFilterDataframe

R Problem Overview


I am working with the dataset LearnBayes. For those that want to see the actual data:

install.packages('LearnBayes')

I am trying to filter out rows based on the value in the columns. For example, if the column value is "water", then I want that row. If the column value is "milk", then I don't want it. Ultimately, I am trying to filter out all individuals who's Drink column is "water".

R Solutions


Solution 1 - R

The subset command is not necessary. Just use data frame indexing

studentdata[studentdata$Drink == 'water',]

Read the warning from ?subset

> This is a convenience function intended for use interactively. For > programming it is better to use the standard subsetting functions like > ‘[’, and in particular the non-standard evaluation of argument > ‘subset’ can have unanticipated consequences.

Solution 2 - R

Try this:

subset(studentdata, Drink=='water')

that should do it.

Solution 3 - R

Thought I'd update this with a dplyr solution

library(dplyr)    
filter(studentdata, Drink == "water")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser722224View Question on Stackoverflow
Solution 1 - RadamleerichView Answer on Stackoverflow
Solution 2 - RDave KincaidView Answer on Stackoverflow
Solution 3 - RrrsView Answer on Stackoverflow