Dplyr: Conditional statements?

29 views
Skip to first unread message

Gouri Shankar Mishra

unread,
Sep 17, 2016, 3:32:21 PM9/17/16
to Davis R Users' Group
Hello All

I have a dataframe (df) and am currently creating a variable based on the following conditional statements:

df$var2[df$var1 %in% c("tag.vmt.d2", "tag.pmt.d3", "tag.all.d4"] <- c("tag_one")

df$var2[df$var1 %in% c("bag.vmt.d2", "bag.pmt.d3", "bag.all.d4", "bag.all.d5"] <- c("bag_two")

The above is okay but very inefficient. 

Is there a more efficient way of doing this in DPLYR with the use of "starts_with" and "mutate" functions? 

Thanks so much. 

Brandon Hurr

unread,
Sep 17, 2016, 3:34:55 PM9/17/16
to Davis R Users' Group
?if_else in a mutate would work for your problem and the dplyr version can handle NAs too.
--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Matt Espe

unread,
Sep 19, 2016, 1:32:14 AM9/19/16
to Davis R Users' Group
I am not quite sure what you mean by inefficient. That code should be quite fast (assuming the open parentheses are fixed). 

If you want to avoid typing out the variable names, the grep function is what you are looking for:

df$var2[grep("^tag",df$var1)]<-"tag_one"
df$var2[grep("^bag",df$var1)]<-"bag_two"

This should work, and if it doesn't you will not end up in a call stack 80 layers deep. :P The grep functions are worth knowing and are very flexible. 

Matt

Gouri Shankar Mishra

unread,
Sep 19, 2016, 8:58:08 PM9/19/16
to Davis R Users' Group
Thanks MAtt. I actually implemented something close to grep

df$var2[df$var1 %in% names(select(df, starts_with("Com")))] <- "tag_one"
Reply all
Reply to author
Forward
0 new messages