subsetting data using multiple variables in R

徘徊边缘 提交于 2019-12-24 14:09:34

问题


I have a data set, DATA, with many variables. DATA has a list mode and its class is a data.frame. The variables I'm concerned with are AGE.MONTHS and LOCATION. I need to subset DATA into another set called SUB, and I want SUB to only contain the observations where AGE.MONTHS <= 2 and LOCATION = "Area A". AGE.MONTHS is has a numeric mode and class. LOCATION has a numeric mode and its class is a factor. I have tried the following,

SUB<-which((DATA$AGE.MONTHS <= 2 )& (DATA$LOCATION=="Area A"))

But this only tells me which observations these conditions hold true for, and what I need is a subset of all the data for which these conditions hold. Thanks for your help.


回答1:


Use the subset function.

subset(DATA, AGE.MONTHS <= 2 & LOCATION == "Area A")



回答2:


If this is in a program, you are better to use [ than subset. For example, see here: Why is `[` better than `subset`?

To subset with [, you want this:

DATA[with(DATA, AGE.MONTHS <= 2 & LOCATION == "Area A"), ]


来源:https://stackoverflow.com/questions/16247105/subsetting-data-using-multiple-variables-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!