[R] subset by two variables

Marc Schwartz (via MN) mschwartz at mn.rr.com
Tue Aug 29 22:20:25 CEST 2006

On Tue, 2006-08-29 at 13:05 -0700, Dean Sonneborn wrote:
> I'm using this syntax to create data subsets for plots:  
> subset=source=="Both". Now I would like to create a subset defined by 
> two different variables like this: subset=source=="Both"  
> subset=site=="home" but this syntax is not correct. The documents in the 
> manual for subset seem to be creating whole new data files not just 
> selecting rows based on the contents of the variables. How do I subset 
> based on two variables.

If your data frame is called 'DF':

  plot(y ~ x, data = DF, subset = (source == "Both") & (site == "home"))

See ?Logic and ?Syntax for more information on creating logical


Marc Schwartz

More information about the R-help mailing list