Crosstab with multiple items

cross tabulation with multiple variables
r crosstab multiple columns
dplyr crosstab
cross tabulation in r
crosstab function in r
r crosstab with totals
survey crosstabs in r
alteryx crosstab multiple values

In SPSS, it is (relatively) easy to create a cross tab with multiple variables using the factors (or values) as the table heading. So, something like the following (made up data, etc.). Q1, Q2, and Q3 each have either a 1, a 2, or a 3 for each person. I just left these as numbers, but they could be factors, neither seemed to 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, one can even request row, column, or total percentages.

I've tried table(), ftable(), xtab(), CrossTable() from gmodels, and CrossTable() from descr, and none of these can handle (afaik) multiple variables; they mostly seem to handle 1 variable crossed with another variable, and the 3rd 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'm missing something simple, so I appreciate you pointing out what I missed. Perhaps I have to generate each row as a separate list and then make a dataframe and print the dataframe?

UPDATE: I've now discovered ctab() in package catspec, which is also on the right track. It's interesting that R has no consistent equivalent to Ctables in SPSS, which is basically a "tabbing" tool ala the old tabulate tools used for survey research. ctab() is trying, and is an admirable 1st step... but you still can't make this table (above) with it.

The Hmisc package has the summary.formula function that can do something along the lines you want. It is very flexible, so look at the help page for examples, but here is an application to 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)|
 +------+-------+

The possible values are given in rows, because it has the flexibility of different sets of values for different variables. You might be able to play with the function parameters (like method and fun) to get the other direction.

Multiple Data Fields in Cross Tab, you could select multiple Data Fields in a single Crosstab For example. I found that using Option 1 above - transpose first, get all the values  The Multiple Response Crosstabs procedure crosstabulates defined multiple response sets, elementary variables, or a combination. You can also obtain cell percentages based on cases or responses, modify the handling of missing values, or get paired crosstabulations.

Modifying a 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 cross looks like this

> 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

Crosstab query with multiple value fields, I'm trying to create a crosstab query with multiple values and need some help. I have two crosstab queries (SM and RM) with the following  In SPSS, it is (relatively) easy to create a cross tab with multiple variables using the factors (or values) as the table heading. So, something like the following (made up data, etc.). Q1, Q2, and Q3 each have either a 1, a 2, or a 3 for each person.

The underlying issue is that this data is not in tidy format. Crosstabbing multiple variables will be easier when the data is reshaped into "long" form. We can do that with gather from the tidyr package.

After reshaping, many crosstab functions will work; I'll use tabyl from the janitor package (since - full disclosure - I maintain that package and built the 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) %>% 
  tabyl(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 with functions like janitor::adorn_percentages().

Cross tab: Multiple Rows/Values, Is there any way of producing a crosstab query with multiple value fields? I'm producing a report that will list the subjects for each day of the  We want to plan ahead for how to treat these multiple response items in SPSS. We learn how to create a response set that we can cross tabulate with another (yes or no) item. Crosstabs Link to

just check Hadley Wickham's reshape package. AFAIS, you need cast function from the package.

Crosstab with multiple measure only displays one total column, In the calculations, add the measure you wish to display the total of from the query item list 5. Under Data Item properties, change the  When selecting multiple items, the selected items are placed in the exploration sheet in the order that you click them. To avoid rearranging items after you drag and drop them into the crosstab, click the items in the desired order of placement. To add an item to a multiple set, hold down the Ctrl key while dragging the item.

xtabs has a formula interface that can take some practice to get used to, but this can be done. If you have the 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

can crosstab show multiple objects in top header?, can crosstab show multiple objects in top header? for example, the data showed as : \x3C![CDATA[customer city name city value\t product  pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)¶. Compute a simple cross tabulation of two (or more) factors.

Cross-tabulation (CROSSTAB) of Multiple Response Findings in , I am trying to create a quick overview of several measures, and show the values for each KPI on three columns: Today, Yesterday, A week ago. Create a Crosstab in SPSS. To create a crosstab, click Analyze > Descriptive Statistics > Crosstabs. A Row(s): One or more variables to use in the rows of the crosstab(s). You must enter at least one Row variable. B Column(s): One or more variables to use in the columns of the crosstab(s).

Crosstab for multiple measures on several colum |Tableau , If one set of headings has date values, the wizard will help you group the records by standard intervals – such as  Use crosstab reports, also known as matrix reports, to show the relationships between three or more query items. Crosstab reports show data in rows and columns with information summarized at the intersection points. For example, the crosstab below shows the gross profit and revenue by product line for each year.

Make summary data easier to read by using a crosstab query, Qtip: You can select multiple variables by holding down the command key on Mac or the Above, you see a crosstab composed of the following elements:. Crosstab node 4 contains a crosstab node member that refers to the data item Order year. This member has two nested crosstab nodes. The first node contains a crosstab node member that refers to the data item Order month. The second node contains a crosstab node member for the total.

Comments
  • This is a powerful function; the only problem is the lack of control over output.
  • True, but the output leaves something to be desired. x<-c(1,3,1,3,1,3,1,3,4,4);y<-c(2,4,1,4,2,4,1,4,2,4); z<-c(3,5,2,5,3,5,2,5,3,5) multitab(x,y,z) [,1] [,2] [,3] [1,] 4 4 2 [2,] 2 3 5 [3,] 2 3 5 But it's a good start...