Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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...

like image 971
user3099627 Avatar asked Oct 20 '22 16:10

user3099627


1 Answers

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
like image 82
Troy Avatar answered Oct 24 '22 01:10

Troy