Voglio passare arrange()
{dplyr} un vettore di nomi di variabili su cui ordinare. Di solito scrivo solo le variabili che voglio, ma sto cercando di creare una funzione in cui le variabili di ordinamento possano essere inserite come parametro di funzione.Passare un vettore di nomi di variabili per organizzare() in dplyr
df <- structure(list(var1 = c(1L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 4L, 4L
), var2 = structure(c(10L, 1L, 8L, 3L, 5L, 4L, 7L, 9L, 2L, 6L
), .Label = c("b", "c", "f", "h", "i", "o", "s", "t", "w", "x"
), class = "factor"), var3 = c(7L, 5L, 5L, 8L, 5L, 8L, 6L, 7L,
5L, 8L), var4 = structure(c(8L, 5L, 1L, 4L, 7L, 4L, 3L, 6L, 9L,
2L), .Label = c("b", "c", "d", "e", "f", "h", "i", "w", "y"),
class = "factor")), .Names = c("var1", "var2", "var3", "var4"),
row.names = c(NA, -10L), class = "data.frame")
# this is the normal way to arrange df with dplyr
df %>% arrange(var3, var4)
# but none of these (below) work for passing a vector of variables
vector_of_vars <- c("var3", "var4")
df %>% arrange(vector_of_vars)
df %>% arrange(get(vector_of_vars))
df %>% arrange(eval(parse(text = paste(vector_of_vars, collapse = ", "))))
Imo, l'uso di%>% deve essere salvato per il concatenamento, come è piuttosto brutta ... (per le azioni singole <- oppure = funziona bene ... – Kevin