mrquad mrquad - 3 months ago 30
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
and the Country is

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

I would appreciate your replies!


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")), ]