I am trying to find a one-line parameter for assigning factor levels in the%>% command sequence.
My strategy for this was to run a sequence of functions on ., which gives ordered levels of factors that interest me. This results "Error: 'match' requires vector arguments"in an evaluation without use. gives appropriate levels.
library(dplyr)
library(magrittr)
data = data.frame(variable = LETTERS[c(1:4,2:4,3:4)])
data %>% count(variable) %>% arrange(desc(n)) %$% variable
data %>% mutate(variable = factor(variable, levels = . %>% count(variable) %>% arrange(desc(n)) %$% variable))
Can anyone think of a better way to do this or shed light on my mistake?
source
share