I'm trying to create a function that creates a variable. Like this:
Add_Extreme_Variable <- function(dataframe, variable, variable_name){
dataframe %>%
group_by(cod_station, year_station) %>%
mutate(variable_name= ifelse(variable > quantile(variable, 0.95, na.rm=TRUE),1,0)) %>%
ungroup() %>%
return()
}
df <- Add_Extreme_Variable (df, rain, extreme_rain)
df
is the dataframe I'm working with, rain
is a numeric variable in df
, and extreme_rain
is the name of the variable I want to create.
If I use mutate_()
everything works well, but the problem it's deprecated. However, the solutions I have found in stackoverflow (1, 2, 3) and the vignette doesn't seem to fit my problem or it seems far more complicated than I need it to be, as I cannot find good examples about how to work with quo()
, !!
without space, !!
with space, how to replace =
for :=
, and I don't know if working with them at all will solve the problem I have or it's even necessary as the ultimate goal doing this function is to make the code cleaner. Any suggestions?