dplyr: apply function table() to each column of a data.frame

后端 未结 4 691
小鲜肉
小鲜肉 2021-02-18 15:42

Apply function table() to each column of a data.frame using dplyr

I often apply the table-function on each column of a data frame using plyr, like this:

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-18 16:20

    Using tidyverse (dplyr and purrr):

    library(tidyverse)
    
    mtcars %>%
        map( function(x) table(x) )
    

    Or simply:

    library(tidyverse)
    
    mtcars %>%
        map( table )
    

提交回复
热议问题