list all factor levels of a data.frame

前端 未结 6 1196
一整个雨季
一整个雨季 2020-12-28 15:57

with str(data) I get the headof the levels (1-2 values)

fac1: Factor w/ 2  levels ... :
fac2: Factor w/ 5  levels ... :
fac3: Facto         


        
6条回答
  •  生来不讨喜
    2020-12-28 16:37

    Or using purrr:

    data %>% purrr:map(levels)
    

    Or to first factorize everything:

    data %>% dplyr::mutate_all(as.factor) %>% purrr:map(levels)
    

    And answering the question about how to get the lengths:

    data %>% map(levels) %>% map(length)
    

提交回复
热议问题