user3099627
user3099627

Reputation: 65

Selecting a subset where a categorical variable (column) can have 2 values

My data consists of frequency tables listed under each other for different models and scenario's (ie variables). I want to make selections of this database to make graphs for each subset. Most of my variables are categorical and texts (eg weather, scenario). I couln't find a way to allow multiple values from a categorical variable (mostly %in% c() is used for numbers). I tried the following:

ThisSelection <- subset (Hist, all_seeds==0 & weather == "normal" & scenario %in% c("intact","depauperate"))

which doesn't work and

ThisSelection <- subset (Hist, all_seeds==0 & scenario =="intact" | scenario =="depauperate")

which gives only "inatct" scenarios.

My apologies if the answer is simple here, I searched the web but couldn't find where I'm wrong, and I believe there must be an other way than turning string variable-values into numerical ones. I'm a starter in R...

Upvotes: 5

Views: 19451

Answers (2)

Troy
Troy

Reputation: 8691

Your first should work. Hesitate to suggest it but is your spelling of "depauperate" consistent (including case?):

Hist<-data.frame(all_seeds=0, weather=sample(c("normal","odd"),20,T),scenario=sample(c("intact","depauperate"),20,T))
ThisSelection <- subset (Hist, all_seeds==0 & weather == "normal" & scenario %in% c("intact","depauperate"))
ThisSelection


   all_seeds weather    scenario
1          0  normal      intact
3          0  normal      intact
4          0  normal      intact
5          0  normal depauperate
6          0  normal      intact
10         0  normal depauperate
14         0  normal      intact
15         0  normal      intact

Upvotes: 3

tonytonov
tonytonov

Reputation: 25638

Don't forget about logical operators priority:

set.seed(3099627)
Hist <- data.frame(first=sample(letters[1:3], 20, rep=T), second=sample(letters[4:6], 20, rep=T))
subset (Hist, first=="a" & (second=="d" | second=="e"))

   first second
1      a      e
4      a      d
15     a      e
20     a      d

subset (Hist, first=="a" & (second %in% c("d", "e")))

   first second
1      a      e
4      a      d
15     a      e
20     a      d

Upvotes: 0

Related Questions