2

I have a large data frame with many variables. Many are likert scale answers and schools which observations belong to they are logic variables (and can include overlap).

Example:

Q1 <- c(1,2,2,4,3,5)

Q2 <- c(3,4,3,5,4,5)

A <- c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE)

B <- c(FALSE,TRUE,FALSE,TRUE,FALSE,FALSE)

df <- data.frame(Q1,Q2, A, B)

The output I want is a contingency table :

Q1

1 2 3 4 5

A 1 1 0 1 1

B 0 1 0 1 0

where I can do a chi2 test between schools - here A and B. Nothing I have tried works.

I think there maybe answer in what I have read online but I lack the knowledge to recognize it!

Sarah R
  • 23
  • 4

1 Answers1

2

We can use dplyr/tidyr. We group by 'Q1', get the sum of 'A', 'B' columns using summarise_each, convert the 'wide' to 'long' format with gather and reshape it back to 'wide' with `spread.

library(dplyr)
library(tidyr)
df %>% 
  group_by(Q1) %>% 
  summarise_each(funs(sum(.)), A:B) %>% 
  gather(Var, Val,-Q1) %>%
  spread(Q1, Val)

#     Var     1     2     3     4     5
#   (fctr) (int) (int) (int) (int) (int) 
# 1      A     1     1     0     1     1
# 2      B     0     1     0     1     0

A base R option is xtabs after converting to long format

 d1 <- data.frame(Q1= rep(Q1,2), Var= rep(names(df)[3:4],
           each=nrow(df)), Val=unlist(df[3:4]))
 xtabs(Val~Var+Q1, d1)
 #      Q1
 #Var 1 2 3 4 5
 # A 1 1 0 1 1
 # B 0 1 0 1 0
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi, this isn't really a comment, but a question: I can not find tidyr under install packages. I found dplyr there and installed it. I set the cran mirror to New Zealand (not that I think that should matter). – Sarah R Nov 29 '15 at 20:11
  • @SarahR When you do `install.packages('tidyr')` what is the message? Usually the Cloud mirror should have that. – akrun Nov 29 '15 at 20:12
  • I had only looked it up. Typing install.. gave message Warning message: package ‘tidyr’ is not available (for R version 3.0.3) Which I guess answers why I can not find it. Your other suggestion works great. – Sarah R Nov 29 '15 at 21:43
  • @SarahR The new R version is R 3.2.2. Can you update it to the new version and then install. – akrun Nov 30 '15 at 04:36