2

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.

Mike L
  • 486
  • 5
  • 16
  • 33

2 Answers2

7

Use the subset function.

subset(DATA, AGE.MONTHS <= 2 & LOCATION == "Area A")
Victor K.
  • 4,054
  • 3
  • 25
  • 38
  • Ha, thanks! I'm actually a bit embarrassed I didn't come to this myself! – Mike L Apr 27 '13 at 00:42
  • Nothing to be embarrassed about :). R does have a stepper learning curve than some other languages, but it's very powerful once you learn it. – Victor K. Apr 27 '13 at 00:43
  • I tried subset, but using this code: SUB<-subset(DATA, subset=AGE>MONTHS<=2 and subset=LOCATION=="Area A").[which is straight out of the R manual, BTW] When it didn't work, I just thought it wasn't the right thing to do. – Mike L Apr 27 '13 at 00:43
4

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"), ]
Community
  • 1
  • 1
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112