mrquad mrquad - 19 days ago 8
R Question

Querying a dataframe in R

I have a dataframe which has a 3 fields: NAME, CATEGORY, COUNTRY

Now I would like to have all data where the category is the String

over 10 mio
or the Country is
Germany
and the Country is
Sweden
.

Any recommendation how to "query" my dataFrame in R?

I would appreciate your replies!

Answer

You can use which.

desireDf <- df[ which( df$CATEGORY=="over 10 mio" &  (df$COUNTRY=="Germany" | df$COUNTRY=="Sweden" )), ]

Also it works without which:

desireDf <- df[ (df$CATEGORY=="over 10 mio" &  (df$COUNTRY=="Germany" | df$COUNTRY=="Sweden")), ]