开发者

Filtering a data frame by values in a column [duplicate]

开发者 https://www.devze.com 2023-04-04 07:55 出处:网络
This question already has answers here: 开发者_开发问答Filter data.frame rows by a logical condition
This question already has answers here: 开发者_开发问答 Filter data.frame rows by a logical condition (9 answers) Closed 6 years ago.

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".


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.


Try this:

subset(studentdata, Drink=='water')

that should do it.


Thought I'd update this with a dplyr solution

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号