Home > Blockchain >  create contingency table from multiple cols vs multiple cols
create contingency table from multiple cols vs multiple cols

Time:10-06

I had a data set and created contingency table using the code as below:

disease <- c("high", "high", "high", "high", "low","low","low","low");
ToA <- c("P","A","P","P","A","A","A","P");
ToB <- c("P","A","A","P","A","P","A","P");
ToC <- c("P","P","A","P","A","A","A","P"); 
df <- data.frame(disease, ToA, ToB, ToC)

df %>%
  pivot_longer(!disease, names_to = 'columns', values_to = 'vals') %>%
  count(disease, columns, vals) %>%
  pivot_wider(names_from = c(disease, vals), values_from = n, 
              names_sep = '_')

the following table was the results. now I would to produce same contingency table even when I have multiple disease columns - see the data set below:

enter image description here

disease1 <- c("high", "low", "high", "high", "low","low","low","low");
disease2 <- c("high", "high", "high", "high", "low","low","low","low");
ToA <- c("P","A","P","P","A","A","A","P");
ToB <- c("P","A","A","P","A","P","A","P");
ToC <- c("P","P","A","P","A","A","A","P"); 
df <- data.frame(disease1, disease2, ToA, ToB, ToC)

Could anyone please guide me through?

CodePudding user response:

Does this work for you?

library(tidyverse)
disease1 <- c("high", "low", "high", "high", "low","low","low","low");
disease2 <- c("high", "high", "high", "high", "low","low","low","low");
ToA <- c("P","A","P","P","A","A","A","P");
ToB <- c("P","A","A","P","A","P","A","P");
ToC <- c("P","P","A","P","A","A","A","P"); 
df1 <- data.frame(disease1, disease2, ToA, ToB, ToC)
df1 |> pivot_longer(ToA:ToC,names_to = "columns", values_to = "vals") |> pivot_longer(disease1:disease2, names_to = "diseases", values_to = "disease_level") |> count(diseases,columns,vals) |> pivot_wider(names_from = c(diseases,vals),values_from = n,names_sep = "_")

Basically, you will have to pivot_longer twice, and then your own workflow that you followed when there was only one disease.

  • Related