[R] subsetting based on joint values of critera

Ista Zahn izahn at psych.rochester.edu
Tue Jan 25 21:03:13 CET 2011


Hi Simon,
You almost had it! Just need to move the negation outside the rest of
the logic, and remove the quotes from year. Not actually tested (no
data), but I think

newdata<- subset(bpa, !(State=="Connecticut" & year<2010))

should do it.

Best,
Ista

On Tue, Jan 25, 2011 at 1:34 PM, Simon Kiss <simonjkiss at yahoo.ca> wrote:
> Dear colleagues, I have a dataset that looks as below.
>
> I would like to make a new dataset that excludes the cases which are joint conjunctions of particular state names and years, so Connecticut and 2010, Maryland and 2010 and Vermont and 2010.
>
> I'm trying the following subset code:
> newdata<- subset(bpa, (!State=="Connecticut" & year<"2010"))
>
> It appears that it's only evaluating both criteria independently and not jointly, so this is returning all cases in 2008 and 2009, leaving out connecticut for those years as well.
> How do I get subset to return a dataset based on the joint occurrence of values of two variables?
>
> Yours,  Simon Kiss
>
> str(bpa)
> 'data.frame':   150 obs. of  5 variables:
>  $ State   : Factor w/ 50 levels "Alabama","Alaska",..: 1 2 3 4 5 6 7 8 9 10 ...
>  $ year    : num  2008 2008 2008 2008 2008 ...
>  $ ban     : num  0 0 0 0 0 0 0 0 0 0 ...
>  $ partisan: Factor w/ 3 levels "democrat","mixed",..: 1 1 1 1 1 1 1 2 3 2 ...
>  $ news    : num  1.67 2 0 0 2.38 ...
> *********************************
> Simon J. Kiss, PhD
> Assistant Professor, Wilfrid Laurier University
> 73 George Street
> Brantford, Ontario, Canada
> N3T 2C9
> Cell: +1 519 761 7606
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list