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

后端 未结 4 690
小鲜肉
小鲜肉 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 15:55

    In general you probably would not want to run table() on every column of a data frame because at least one of the variables will be unique (an id field) and produce a very long output. However, you can use group_by() and tally() to obtain frequency tables in a dplyr chain. Or you can use count() which does the group_by() for you.

    > mtcars %>% 
        group_by(cyl) %>% 
        tally()
    > # mtcars %>% count(cyl)
    
    Source: local data frame [3 x 2]
    
      cyl  n
    1   4 11
    2   6  7
    3   8 14
    

    If you want to do a two-way frequency table, group by more than one variable.

    > mtcars %>% 
        group_by(gear, cyl) %>% 
        tally()
    > # mtcars %>% count(gear, cyl)
    

    You can use spread() of the tidyr package to turn that two-way output into the output one is used to receiving with table() when two variables are input.

    0 讨论(0)
  • 2021-02-18 16:16

    You can try the following which does not rely on the tidyr package.

    mtcars %>% 
       lapply(table) %>% 
       lapply(as.data.frame) %>% 
       Map(cbind,var = names(mtcars),.) %>% 
       rbind_all() %>% 
       group_by(var) %>% 
       mutate(pct = Freq / sum(Freq))
    
    0 讨论(0)
  • 2021-02-18 16:19

    Solution by Caner did not work but from comenter akrun (credit goes to him), this solution worked great. Also using a much larger tibble to demo it. Also I added an order by percent descending.

    library(nycflights13);dim(flights)
    
    tte<-gather(flights, Var, Val) %>% 
    group_by(Var) %>% dplyr::mutate(n=n()) %>% 
    group_by(Var,Val) %>% dplyr::mutate(n1=n(), Percent=n1/n)%>%
    arrange(Var,desc(n1) %>% unique()
    
    0 讨论(0)
  • 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 )
    
    0 讨论(0)
提交回复
热议问题