RW: Banner using R (Alexandros Kouretsis)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

RW: Banner using R (Alexandros Kouretsis)

Alexandros Kouretsis
This might be slightly tricky. You can use factor levels and table to take
into account that B is missing from the third column. Just introducing
dplyr library for piping.

library(dplyr)

Data <- data.frame(v1 = c('A', 'B' ,'B' ,'A', 'B'), v2 = c('A', 'B', 'A',
'A', 'B'), v3 = c('A', 'A', 'A', 'A', 'A'))

lvls <- lapply(Data, unique) %>% unlist %>% unique

c_counts <- lapply(Data, function(x){
  levels(x) <- lvls
  x %>% table
})

ag_data <- do.call(cbind, counts)
TOT <- apply(ag_data, 1, sum)

ag_data <- cbind(Count = lvls, ag_data %>% data.frame, TOT)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.