The tidyr spread function uses column names without quotes. Is there a way to pass a variable containing the column name for example
# example using gather() library("tidyr") dummy.data <- data.frame("a" = letters[1:25], "B" = LETTERS[1:5], "x" = c(1:25)) dummy.data var = "x" dummy.data %>% gather(key, value, var)
It gives an error
Error: All select() inputs must resolve to integer column positions. The following do not: * var
What is solved with the match function, which gives the desired column position
dummy.data %>% gather(key, value, match(var, names(.)))
But the same approach does not work for the distribution function
dummy.data %>% spread(a, match(var, names(.))) Error: Invalid column specification
Collect and distribute functions perform different column specifications. gather accepts the index of the column, while distribution doesnβt mention what it wants
source share