Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subsetting data using multiple variables in R

Tags:

r

subset

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.

like image 720
Mike L Avatar asked Apr 23 '26 23:04

Mike L


2 Answers

Use the subset function.

subset(DATA, AGE.MONTHS <= 2 & LOCATION == "Area A")
like image 53
Victor K. Avatar answered Apr 26 '26 17:04

Victor K.


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"), ]
like image 39
Matthew Lundberg Avatar answered Apr 26 '26 19:04

Matthew Lundberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!