Cross-tab with multiple items

In SPSS (relatively), itโ€™s easy to create a cross-tab with multiple variables using coefficients (or values) as the table header. So, something like the following (compiled data, etc.). Q1, Q2 and Q3 each has either 1, 2, or 3 for each person. I just left them as numbers, but they could be factors, and it seems they did not help solve the problem.

                         1 (very Often) 2 (Rarely) 3 (Never)
    Q1.  Likes it 12 15 13
    Q2.  Recommends it 22 11 10
    Q3.  Used it 22 12 9

In SPSS, you can even query rows, columns, or total percentages.

I tried using table (), ftable (), xtab (), CrossTable () from gmodels and CrossTable () from descr, and none of them can handle (afaik) multiple variables; they basically seem to handle 1 variable crossed with another variable, and the third creates layers.

Is there a package with some good cross tabbing / table examples that I could use to figure this out? I'm sure I missed something simple, so I appreciate that you indicate that I missed. Perhaps I need to generate each line as a separate list, and then make a data frame and print the data?

UPDATE: I opened ctab () in the catspec package, which is also on the right path. Interestingly, R does not have a consistent Ctables equivalent in SPSS, which is basically a tabbing tool ala old tabulation tools used for research. ctab () tries and is a great first step ... but you still can't make this table (above) with it.

+7
r crosstab categorical-data
source share
6 answers

There is a function summary.formula in the Hmisc package that can do something according to the desired line. It is very flexible, so look at the help page for examples, but here is the application for your problem:

 library(Hmisc) dd <- data.frame(Q1=sample(1:3, 20, replace=T), Q2=sample(1:3, 20, replace=T), Q3=sample(1:3, 20, replace=T)) #fake data summary(~Q1+Q2+Q3, data=dd, fun=table) 

This gives the following result:

  Descriptive Statistics (N=20) +------+-------+ | | | +------+-------+ |Q1 : 1|25% (5)| +------+-------+ | 2 |45% (9)| +------+-------+ | 3 |30% (6)| +------+-------+ |Q2 : 1|30% (6)| +------+-------+ | 2 |35% (7)| +------+-------+ | 3 |35% (7)| +------+-------+ |Q3 : 1|35% (7)| +------+-------+ | 2 |30% (6)| +------+-------+ | 3 |35% (7)| +------+-------+ 

Possible values โ€‹โ€‹are specified in strings because they have the flexibility of different sets of values โ€‹โ€‹for different variables. You can play with function parameters (e.g. method and fun ) to get a different direction.

+7
source share

Change to the previous example

 library(Hmisc) library(plyr) dd <- data.frame(q1=sample(1:3, 20, replace=T), q2=sample(1:3, 20, replace=T), q3=sample(1:3, 20, replace=T)) #fake data cross <- ldply(describe(dd), function(x) x$values[1,])[-1] rownames(cross) <- c("Q1. Likes it","Q2. Recommends it","Q3. Used it") names(cross) <- c("1 (very Often)","2 (Rarely)","3 (Never)") 

Now the cross is as follows

 > cross 1 (very Often) 2 (Rarely) 3 (Never) Q1. Likes it 4 10 6 Q2. Recommends it 7 9 4 Q3. Used it 6 4 10 
+6
source share

just check out the Hadley Wickham change package . AFAIS, you need the cast function from the package.

+1
source share

The main problem is that this data is not in tidy format . Crosstabbing several variables will be easier when the data is converted to a "long" form. We can do this using gather from the tidyr package.

After conversion, many crosstab functions will work; I will use crosstab from the crosstab package (since - full disclosure - I support this package and create a function for this purpose).

 # Create reproducible sample data set.seed(1) possible_values <- c("1 (Very Often)", "2 (Rarely)", "3 (Never)") some_values <- sample(possible_values, 100, replace = TRUE) dat <- data.frame(Q1 = some_values[1:25], Q2 = some_values[26:50], Q3 = some_values[51:75], Q4 = some_values[76:100]) library(tidyr) library(janitor) dat %>% gather(question, response) %>% crosstab(question, response) #> question 1 (Very Often) 2 (Rarely) 3 (Never) #> 1 Q1 8 8 9 #> 2 Q2 4 11 10 #> 3 Q3 8 12 5 #> 4 Q4 7 7 11 

From there you can format as interest, etc. using janitor::adorn_crosstab() .

+1
source share

You can use a custom function to use rbind() in multiple tables, for example:

 multitab <- function(...){ tabs<-list(...) tablist<-lapply(tabs,table) bigtab<-t(sapply(tablist,rbind)) bigtab } 
0
source share

xtabs has a formula interface that can get used to practice, but it can be done. If you have data in a dataframe df , and your variables are called ques and resp , you can use:

 xtabs(~ques+resp,data=df) 

For example:

 > t1 <- rep(c("A","B","C"),5) > t2 <- rpois(15,4) > df <- data.frame(ques=t1,resp=t2) > xtabs(~ques+resp,data=df) resp names 2 3 4 5 6 7 9 A 1 0 2 1 0 0 1 B 1 0 0 2 1 1 0 C 1 2 0 1 0 1 0 
0
source share

All Articles