Flatten contingency table in R -


this question has answer here:

i have contingency table, eg built in titanic dataset, , want way drop variable , merge values together. sort of project data down onto lower dimensional space.

e.g. looking @ 1 2-d slice of table

      sex class  male female   1st    57    140   2nd    14     80   3rd    75     76   crew  192     20 

if drop sex variable, want end 1-d contingency table looked like:

class  freq   1st   197   2nd    94   3rd   151   crew  212 

my actual use case n dimensional table want able construct n 1-way , n*(n-1)/2 2-way tables from. feels there should simple way work.

edit: note not duplicate of question has been linked with, referring data tables, not contingency tables. solution here convert contingency table data table, use xtabs contingency table. referenced solution deals case of starting data table , wanting end data table.

data(titanic) library(dplyr)  as.data.frame(titanic) %>% group_by(class) %>% summarise(n=sum(freq))  # class     n # (fctr) (dbl) # 1    1st   325 # 2    2nd   285 # 3    3rd   706 # 4   crew   885 

or data.table:

library(data.table) as.data.table(titanic)[, .(n = sum(n)), keyby=class] 

you can make vector of dim names , loop on get(dimname) in dplyr or data.table 1-way or 2-way freqs.

example:

dims <- c('class','sex','age') dt <- as.data.table(titanic) for(dim in dims)   print(dt[, .(n = sum(n)), keyby = get(dim)]) 

note get 1 way of passing variable name frequency tables programmatically.

to 2-way table in data.table, can use dcast:

dcast.data.table(dt, age ~ class, value.var='n', fun.aggregate=sum) #      age 1st 2nd 3rd crew # 1: adult 319 261 627  885 # 2: child   6  24  79    0 

to produce multiple 2-way tables dcast need build formula programatically, e.g. formula = as.formula(paste(v1,v2,sep='~'))

since data.table syntax takes getting used to, if want stay inside 'tidyverse' 2-way tables can do:

data(titanic) library('dplyr') library('tidyr')  as.data.frame(titanic) %>%    group_by(age,class) %>%    summarise(n=sum(freq)) %>%   spread(class, n)  #      age   1st   2nd   3rd  crew #   (fctr) (dbl) (dbl) (dbl) (dbl) # 1  child     6    24    79     0 # 2  adult   319   261   627   885 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -