Home > Net >  Directly Converting Data Frames into Contingency Tables (R)
Directly Converting Data Frames into Contingency Tables (R)

Time:01-12

I am working with R.

Given the following random data I generated, I was able to make a contingency table with this data:

library(memisc)
library(dplyr)

set.seed(123)

v1 <- c("2010-2011","2011-2012", "2012-2013", "2013-2014", "2014-2015") 
v2 <- c("A", "B", "C", "D", "E")
v3 <- c("Z", "Y", "X", "W" )
v4 <- c("data_1", "data_2", "data_3", "data_4" )


dates <- as.factor(sample(v1, 1000, replace=TRUE, prob=c(0.5, 0.2, 0.1, 0.1, 0.1)))

types <- as.factor(sample(v2,1000, replace=TRUE, prob=c(0.3, 0.2, 0.1, 0.1, 0.1)))

types2 <- as.factor(sample(v3, 1000, replace=TRUE, prob=c(0.3, 0.5, 0.1, 0.1)))

names <- as.factor(sample(v3, 1000, replace=TRUE, prob=c(0.3, 0.5, 0.1, 0.1)))

var = rnorm(1000,10,10)

problem_data = data.frame(var,dates, types, types2, names)


summary <- xtabs(~dates names types types2, problem_data)
t = ftable(summary, row.vars=1, col.vars=2:4)

show_html(t)

enter image description here

Is it possible to make contingency tables similar to the table directly from a data frame?

For example, suppose I wanted to make the above contingency table, but instead of filling this table with "counts", I would like to fill the table with the average of "var". Using the "dplyr" library, I can create a data frame that contains all the values required for this contingency table:

library(dplyr)

contingency_table = data.frame(problem_data %>% group_by(dates,names, types, types2) %>% summarise(mean_value = mean(var)))

head(contingency_table)

      dates names types types2 mean_value
1 2010-2011     W     A      X -10.128687
2 2010-2011     W     A      Y   9.552724
3 2010-2011     W     A      Z   9.686354
4 2010-2011     W     B      W  -4.411400
5 2010-2011     W     B      Y  13.624970
6 2010-2011     W     B      Z   7.008089

Can this above data frame be made into a contingency table, and then converted to a html publishable format?

Using this stackoverflow post here (enter image description here

Is it possible to make a contingency table where instead of counts, the mean value of the variable "var" for each group is used? Is it better to do this using the xtabs() function , and I have overcomplicated this using the "data frame approach"? Can someone please show me how to do this?

Thanks!

CodePudding user response:

The generic cross-tabulation method is known as tapply in R. You can read in more detail x

memisc::show_html(y) gives y

  •  Tags:  
  • Related