0

I am trying to read SPSS file to create crosstabs using R expss and foreign package. I have previously tried this with CSV files and that worked as I wanted. The parameters are passed using PHP script. For single select questions (for SPSS) it works just fine, but for multiple select questions things aren't working perfectly. Can anyone help me out with the syntax for tabulating multiple select questions in R ?

setwd('file path goes here')


library(foreign)
library(expss)



expss_output_viewer()

all_sav <- list.files(pattern ='\\.sav$')
pass<-all_sav[with(file.info(all_sav), which.max(mtime))]

mydata = read.spss(pass, stringsAsFactors = FALSE,reencode=TRUE)


w <- data.frame(mydata)
args <- commandArgs(TRUE)


Ques<-as.character(args[1])   
Ban<-as.character(args[2])

Ques_Vec = strsplit(Ques, ',')[[1]]
Ban_Vec = strsplit(Ban, ',')[[1]]

temp1 <- w[c(Ques_Vec)]
temp2 <- w[c(Ban_Vec)]

if(length(Ques_Vec)>1 & length(Ban_Vec)>1 )
{
Banner1 = w %>% 
tab_cells(mrset(as.category(temp1))) %>%
tab_cols(total(),mrset(as.category(temp2))) %>% 
tab_stat_cases(total_row_position = "none",label = "")
#tab_pivot(Banner1)


Banner2 = w %>%
  tab_cells(mrset(as.category(temp1))) %>%
  tab_cols(total(),mrset(as.category(temp2))) %>% 
  tab_stat_cpct(total_row_position = "below",label = "")%>%
tab_last_sig_cpct(sig_labels = paste0("<span style='color:red'>",LETTERS, "</b>"))
#tab_pivot(Banner2)

}


This is the code I have created for my crosstab with significance. The desired output should be something like


                  Total   Apple    Mango   Banana
                           A        B        C

Total Cases         120    40       50       30      

Apple                40     30       20      30
                     45%    34%      23%      10%AB

Mango                12      12       12       12
                     10%     10%      10%       7%

Banana                18     19       20        21
                     0.7%A    12%AC    23%      0.7%

This is the example output for the following

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 16 '19 at 16:44
  • @MrFlick I have edited my question with desired output and sample code. hope this is clear now what I want precisely. – Shubham Prabhakar Oct 16 '19 at 17:04
  • What do you mean by 'things aren't working perfectly'? – Gregory Demin Oct 20 '19 at 13:29

0 Answers0