2

I'm working with data frames in R and often use a lot of conditions on my dataframes, mainly & and | operators. I'm doing that like so:

df = data.frame("col1"=c("success", "failure", "success"), "col2"=c("success", "success", "failure"), "col3"=c(1,1,100))
#multiple conditions, option 1
df[(df[["col1"]]=="success") & (df[["col2"]]=="success") & (df[["col3"]] == 1), ]
#multiple conditions, option 2
df[which((df[["col1"]]=="success") & (df[["col2"]]=="success") & (df[["col3"]] == 1)),]

However, my expressions tend to get really long and hard to read that way.

  • Is there a better, more readable way of doing it?
  • EDIT: Preferably, I'd like to work within the base R environment w/out external libraries.

I put this together based on other posts here on SO about not using subset, using | correctly, but didnt' find anything addressing this specific issue.

I hope this is not too opinion-based, otherwise I'll retract my question. Thanks!

Community
  • 1
  • 1
patrick
  • 3,075
  • 4
  • 32
  • 46

2 Answers2

2

One option is to use the filter() function in the dplyr package:

library(dplyr)
filter(df, col1=="success" & col2=="success" & col3==1)

You can also use commas (equivalent to &) to separate multiple arguments:

filter(df, col1=="success", col2=="success", col3==1)
Julia Wilkerson
  • 581
  • 4
  • 4
  • Thanks, I didn't specify this in my question (my bad) but I was thinking standard package to make this more easily transferable. It's good to know about it though! – patrick Mar 24 '17 at 11:50
0

Try this: (may be not clumsy but with same '&')

> df[df$col1=="success" & df$col2=="success" & df$col3==1,]
     col1    col2 col3
1 success success    1
Venkat
  • 189
  • 3
  • 9
  • cool, I just figured that might get me in trouble once I use it in a script or with variables, cause of the `$`. – patrick Mar 24 '17 at 11:48