Reputation: 27
Regarding doing mutations and simple transformations not depending on variables with names that can be matched via "starts_with
", "ends_with
" etc, but just variables stored in a vector variable.
Is there a way, pe, to do transformations on the variables stored in a vector? I bet there is, but despise my efforts and consulting the list of dplyr functions I can't find it.
For now I have tried all the usual selectors like any_of
, and all_of
, but at the moment, they keep failing because it's not in a selecting method.
So. I'm stuck with this that doesnt work.
myvars = c("Sepal.Length","Sepal.Width")
iris2 = iris %>% rowwise() %>%
mutate(geo.mean= psych::geometric.mean(any_of(myvars)))
# It should do something like this:
iris %>% rowwise() %>%
mutate(geo.mean = psych::geometric.mean(c(Sepal.Length,Sepal.Width)) )
I have also take a look at pick()
but Im currently trying to reinstall dplyr in order to update it from my 1.0.1 to the 1.1.0 version to no avail. (It keeps telling me to restart the session despise there are no packages loaded that could prevent it to update, I'm currently on it). Thing is, from the documentation, its like a subset and I intend to keep all my variables.
Upvotes: 0
Views: 138
Reputation: 395
Yes, you can do this using the 'c_across()' function which lets you use tidy selection syntax:
iris %>%
rowwise() %>%
mutate(geo.mean = psych::geometric.mean(c_across(all_of(myvars))))
Upvotes: 1