Skip to content
Back to formatted view

Raw Message

Message-ID: <CAA1Oq-2eZ30w24iAk2Rk_fABW9_vyiMjLiOkF8Yx_sALY__3dQ@mail.gmail.com>
Date: 2019-01-17T09:25:09Z
From: Alexandros Kouretsis
Subject: RW: Banner using R (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]]