0

I have a several categorical variables with values from 1-3. I would like to create a table similar to those generated by the tabm feature in Stata which shows me the number of observations for each categorical variable.

In Stata, I would simply write:

  tabm Variable1 Variable2 Variable3

This is how the table should look (obs=observations):

             Value=1    Value=2   Value=3
Variable1      5 obs     10 obs    12 obs
Variable2      2 obs     9 obs     0 obs
Variable3      12 obs    9 obs     3 obs

I read some posts about Cross.Table but my version of R won't accept gmodels (R Version 3.4.4).

Student
  • 73
  • 8
  • 1
    `tabm` is not an official Stata command. –  Jul 27 '18 at 14:18
  • Why not using `?table`? Or perhaps a look into this might help: https://stackoverflow.com/questions/25721884/how-should-i-deal-with-package-xxx-is-not-available-for-r-version-x-y-z-wa – jay.sf Jul 27 '18 at 14:36
  • 1
    When asking for help, you should 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 Jul 27 '18 at 14:38

3 Answers3

0

Assuming each variable has each of the category values and is of equal length we can use rbind and table:

x <- c(1,1,1,1,2,2,3,3)
y <- c(1,2,2,2,3,3,3,3)
z <- c(1,1,2,3,2,2,2,2)

rbind(table(x), table(y), table(z)) #gives us:

      1 2 3
 [1,] 4 2 2
 [2,] 1 3 4
 [3,] 2 5 1
CClarke
  • 503
  • 7
  • 18
0

We can apply table() whithin a sapply().

Example

t(sapply(df1[, c("Variable1", "Variable2", "Variable3")], table))

Yielding

> t(sapply(df1[, c("Variable1", "Variable2", "Variable3")], table))
           1  2  3
Variable1 10 17 23
Variable2 20 14 16
Variable3 11 19 20

Data

set.seed(42)
df1 <- setNames(data.frame(replicate(5, sample(3, 50, replace = TRUE))), 
                paste0("Variable", 1:5))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

Or a tidyverse alternative with the same data as above:

library(dplyr)
library(tidyr)

gather(df1, var, value) %>%
  count(var, value) %>%
  spread(value, n)

yielding

# A tibble: 5 x 4
  var         `1`   `2`   `3`
  <chr>     <int> <int> <int>
1 Variable1    10    17    23
2 Variable2    20    14    16
3 Variable3    11    19    20
4 Variable4    20    11    19
5 Variable5    19    20    11
gmason
  • 95
  • 8