How to apply janitor::tabyl to several columns on data table?

Viewed 683

I have a dataframe:

ID   value    value_type
A    256       new
B    543       new
A    544       old

I want to apply janitor::tabyl to columns ID and value_type to get:

ID
     n    percent
A    2     0.66
B    1     0.33


value_type

       n    percent
new    2     0.66
old    1     0.33

How could I do that? When I do:

janitor::tabyl(dt[, c(ID, value_type)])

it doesn't separate those statistic tables by column

3 Answers
library(purrr)
library(dplyr)

df %>% map(., ~tabyl(.))

You could select the columns you need:

df %>% 
  select(ID, value_type) %>% 
  map(., ~tabyl(.))

This gives us:

$ID
 . n   percent
 A 2 0.6666667
 B 1 0.3333333

$value_type
   . n   percent
 new 2 0.6666667
 old 1 0.3333333

With data.table, we can specify the columns of interest in .SDcols, loop over those with lapply, apply the tabyl wrap it in a list and return those list columns (as the output from tabyl is a tabyl/data.frame class

library(data.table)
out <- setDT(df1)[, lapply(.SD, function(x) list(tabyl(x))), .SDcols = c('ID', 'value_type')]

-output

out$ID[[1]]
# x n   percent
# A 2 0.6666667
# B 1 0.3333333

You can do it in baseR easily

apply(mpg[,1:2], 2, tabyl)

$manufacturer
  newX[, i]  n    percent
       audi 18 0.07692308
  chevrolet 19 0.08119658
      dodge 37 0.15811966
       ford 25 0.10683761
      honda  9 0.03846154
    hyundai 14 0.05982906
       jeep  8 0.03418803
 land rover  4 0.01709402
    lincoln  3 0.01282051
    mercury  4 0.01709402
     nissan 13 0.05555556
    pontiac  5 0.02136752
     subaru 14 0.05982906
     toyota 34 0.14529915
 volkswagen 27 0.11538462

$model
              newX[, i]  n     percent
            4runner 4wd  6 0.025641026
                     a4  7 0.029914530
             a4 quattro  8 0.034188034
             a6 quattro  3 0.012820513
                 altima  6 0.025641026
     c1500 suburban 2wd  5 0.021367521
                  camry  7 0.029914530
           camry solara  7 0.029914530
            caravan 2wd 11 0.047008547
                  civic  9 0.038461538
                corolla  5 0.021367521
               corvette  5 0.021367521
      dakota pickup 4wd  9 0.038461538
            durango 4wd  7 0.029914530
         expedition 2wd  3 0.012820513
           explorer 4wd  6 0.025641026
        f150 pickup 4wd  7 0.029914530
           forester awd  6 0.025641026
     grand cherokee 4wd  8 0.034188034
             grand prix  5 0.021367521
                    gti  5 0.021367521
            impreza awd  8 0.034188034
                  jetta  9 0.038461538
        k1500 tahoe 4wd  4 0.017094017
 land cruiser wagon 4wd  2 0.008547009
                 malibu  5 0.021367521
                 maxima  3 0.012820513
        mountaineer 4wd  4 0.017094017
                mustang  9 0.038461538
          navigator 2wd  3 0.012820513
             new beetle  6 0.025641026
                 passat  7 0.029914530
         pathfinder 4wd  4 0.017094017
    ram 1500 pickup 4wd 10 0.042735043
            range rover  4 0.017094017
                 sonata  7 0.029914530
                tiburon  7 0.029914530
      toyota tacoma 4wd  7 0.029914530
Related