Let me share an example of what I'm trying to do, since the title may not be as clear as I'd like it to be. This doesn't have reproducible code, but i can add a reproducible example if that will help:
library(dplyr) if(this_team != "") { newdf <- mydf %>% filter(team == this_team) %>% mutate(totalrows = nrow(.)) %>% group_by(x1, y1) %>% summarize(dosomestuff) } else { newdf <- mydf %>% filter(firstname == this_name & lastname == that_name) %>% mutate(totalrows = nrow(.)) %>% group_by(x1, y1) %>% summarize(dosomestuff) }
I am creating a function in R that does some data manipulations on the mydf dataframe. If I pass a value to the function's team_name parameter, then I would like to filter the dataframe using the 'team' column. If I don't pass a value to the team_name parameter, then it defaults to "", and I instead pass values for this_name and that_name, which correspond to the columns 'firstname' and 'lastname' in mydf.
Is there a better way to do this, rather than having to create the entire dplyr pipeline again in two separate if else statements? My actual pipeline of code is much longer than 4 lines each, so having to reproduce code like this is quite frustrating.
You could do
library(dplyr) y <- "" data.frame(x = 1:5) %>% {if (y=="") filter(., x>3) else filter(., x<3)} %>% tail(1)
or
data.frame(x = 1:5) %>% filter(if (y=="") x>3 else x<3) %>% tail(1)
or even store your pipe in the veins of
mypipe <- . %>% tail(1) %>% print data.frame(x = 1:5) %>% mypipe
Building on lukeA's comment, you could also use case_when()
:
library(dplyr) y <- "" data.frame(x = 1:5) %>% filter(case_when(y=="" ~ x > 3, #When y == "", x > 3 T ~ x<3) #Otherwise, x < 3 ) %>% tail(1)
This would be better particularly if you have more than two conditions to evaluate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With