cro: Cross tabulation with support of labels, weights and multiple...

Description Usage Arguments Value See Also Examples

View source: R/cro.R

Description

You can combine tables with add_rows and merge.etable. For sorting table see tab_sort_asc. To provide multiple-response variables as arguments use mrset for multiples with category encoding and mdset for multiples with dichotomy (dummy) encoding. To compute statistics with nested variables/banners use nest. For more sophisticated interface with modern piping via magrittr see tables.

Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
cro(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

cro_cases(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

cro_cpct(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

cro_rpct(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

cro_tpct(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

cro_cpct_responses(
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_responses",
  total_row_position = c("below", "above", "none")
)

calc_cro(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

calc_cro_cases(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

calc_cro_cpct(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

calc_cro_rpct(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

calc_cro_tpct(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_cases",
  total_row_position = c("below", "above", "none")
)

calc_cro_cpct_responses(
  data,
  cell_vars,
  col_vars = total(),
  row_vars = NULL,
  weight = NULL,
  subgroup = NULL,
  total_label = NULL,
  total_statistic = "u_responses",
  total_row_position = c("below", "above", "none")
)

total(x = 1, label = "#Total")

Arguments

cell_vars

vector/data.frame/list. Variables on which percentage/cases will be computed. Use mrset/mdset for multiple-response variables.

col_vars

vector/data.frame/list. Variables which breaks table by columns. Use mrset/mdset for multiple-response variables.

row_vars

vector/data.frame/list. Variables which breaks table by rows. Use mrset/mdset for multiple-response variables.

weight

numeric vector. Optional cases weights. Cases with NA's, negative and zero weights are removed before calculations.

subgroup

logical vector. You can specify subgroup on which table will be computed.

total_label

By default "#Total". You can provide several names - each name for each total statistics.

total_statistic

By default it is "u_cases" (unweighted cases). Possible values are "u_cases", "u_responses", "u_cpct", "u_rpct", "u_tpct", "w_cases", "w_responses", "w_cpct", "w_rpct", "w_tpct". "u_" means unweighted statistics and "w_" means weighted statistics.

total_row_position

Position of total row in the resulting table. Can be one of "below", "above", "none".

data

data.frame in which context all other arguments will be evaluated (for calc_cro_*).

x

vector/data.frame of class 'category'/'dichotomy'.

label

character. Label for total variable.

Value

object of class 'etable'. Basically it's a data.frame but class is needed for custom methods.

See Also

tables, fre, cro_fun.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

calculate(mtcars, cro(am, vs))
calc_cro(mtcars, am, vs) # the same result

# column percent with multiple banners
calculate(mtcars, cro_cpct(cyl, list(total(), vs, am)))
calc_cro_cpct(mtcars, cyl, list(total(), vs, am)) # the same result

# nested banner
calculate(mtcars, cro_cpct(cyl, list(total(), vs %nest% am)))

# stacked variables
calculate(mtcars, cro(list(cyl, carb), list(total(), vs %nest% am)))

# nested variables
calculate(mtcars, cro_cpct(am %nest% cyl, list(total(), vs)))

# row variables
calculate(mtcars, cro_cpct(cyl, list(total(), vs), row_vars = am))

# several totals above table
calculate(mtcars, cro_cpct(cyl, 
              list(total(), vs), 
              row_vars = am,
              total_row_position = "above",
              total_label = c("number of cases", "row %"),
              total_statistic = c("u_cases", "u_rpct")
              ))

# multiple-choice variable
# brands - multiple response question
# Which brands do you use during last three months? 
set.seed(123)
brands = data.frame(t(replicate(20,sample(c(1:5,NA),4,replace = FALSE))))
# score - evaluation of tested product
score = sample(-1:1,20,replace = TRUE)
var_lab(brands) = "Used brands"
val_lab(brands) = make_labels("
                              1 Brand A
                              2 Brand B
                              3 Brand C
                              4 Brand D
                              5 Brand E
                              ")

var_lab(score) = "Evaluation of tested brand"
val_lab(score) = num_lab("
                             -1 Dislike it
                             0 So-so
                             1 Like it    
                             ")

cro_cpct(mrset(brands), list(total(), score))
# responses
cro_cpct_responses(mrset(brands), list(total(), score))

Example output

                                                             
 |              |              |   Engine |                 |
 |              |              | V-engine | Straight engine |
 | ------------ | ------------ | -------- | --------------- |
 | Transmission |    Automatic |       12 |               7 |
 |              |       Manual |        6 |               7 |
 |              | #Total cases |       18 |              14 |
                                                             
 |              |              |   Engine |                 |
 |              |              | V-engine | Straight engine |
 | ------------ | ------------ | -------- | --------------- |
 | Transmission |    Automatic |       12 |               7 |
 |              |       Manual |        6 |               7 |
 |              | #Total cases |       18 |              14 |
                                                                             
 |                     |              | #Total |   Engine |                 |
 |                     |              |        | V-engine | Straight engine |
 | ------------------- | ------------ | ------ | -------- | --------------- |
 | Number of cylinders |            4 |   34.4 |      5.6 |            71.4 |
 |                     |            6 |   21.9 |     16.7 |            28.6 |
 |                     |            8 |   43.8 |     77.8 |                 |
 |                     | #Total cases |   32.0 |     18.0 |            14.0 |
                        
 Transmission |        |
    Automatic | Manual |
 ------------ | ------ |
         15.8 |   61.5 |
         21.1 |   23.1 |
         63.2 |   15.4 |
         19.0 |   13.0 |
                                                                             
 |                     |              | #Total |   Engine |                 |
 |                     |              |        | V-engine | Straight engine |
 | ------------------- | ------------ | ------ | -------- | --------------- |
 | Number of cylinders |            4 |   34.4 |      5.6 |            71.4 |
 |                     |            6 |   21.9 |     16.7 |            28.6 |
 |                     |            8 |   43.8 |     77.8 |                 |
 |                     | #Total cases |   32.0 |     18.0 |            14.0 |
                        
 Transmission |        |
    Automatic | Manual |
 ------------ | ------ |
         15.8 |   61.5 |
         21.1 |   23.1 |
         63.2 |   15.4 |
         19.0 |   13.0 |
                                                                        
 |                     |              | #Total |       Engine |        |
 |                     |              |        |     V-engine |        |
 |                     |              |        | Transmission |        |
 |                     |              |        |    Automatic | Manual |
 | ------------------- | ------------ | ------ | ------------ | ------ |
 | Number of cylinders |            4 |   34.4 |              |   16.7 |
 |                     |            6 |   21.9 |              |   50.0 |
 |                     |            8 |   43.8 |          100 |   33.3 |
 |                     | #Total cases |   32.0 |           12 |    6.0 |
                           
                 |        |
 Straight engine |        |
    Transmission |        |
       Automatic | Manual |
 --------------- | ------ |
            42.9 |    100 |
            57.1 |        |
                 |        |
             7.0 |      7 |
                                                                          
 |                       |              | #Total |       Engine |        |
 |                       |              |        |     V-engine |        |
 |                       |              |        | Transmission |        |
 |                       |              |        |    Automatic | Manual |
 | --------------------- | ------------ | ------ | ------------ | ------ |
 |   Number of cylinders |            4 |     11 |              |      1 |
 |                       |            6 |      7 |              |      3 |
 |                       |            8 |     14 |           12 |      2 |
 |                       | #Total cases |     32 |           12 |      6 |
 | Number of carburetors |            1 |      7 |              |        |
 |                       |            2 |     10 |            4 |      1 |
 |                       |            3 |      3 |            3 |        |
 |                       |            4 |     10 |            5 |      3 |
 |                       |            6 |      1 |              |      1 |
 |                       |            8 |      1 |              |      1 |
 |                       | #Total cases |     32 |           12 |      6 |
                           
                 |        |
 Straight engine |        |
    Transmission |        |
       Automatic | Manual |
 --------------- | ------ |
               3 |      7 |
               4 |        |
                 |        |
               7 |      7 |
               3 |      4 |
               2 |      3 |
                 |        |
               2 |        |
                 |        |
                 |        |
               7 |      7 |
                                                                            
 |              |           |                     |    | #Total |   Engine |
 |              |           |                     |    |        | V-engine |
 | ------------ | --------- | ------------------- | -- | ------ | -------- |
 | Transmission | Automatic | Number of cylinders |  4 |    9.4 |          |
 |              |           |                     |  6 |   12.5 |          |
 |              |           |                     |  8 |   37.5 |     66.7 |
 |              |    Manual | Number of cylinders |  4 |   25.0 |      5.6 |
 |              |           |                     |  6 |    9.4 |     16.7 |
 |              |           |                     |  8 |    6.2 |     11.1 |
 | #Total cases |           |                     |    |   32.0 |     18.0 |
                  
                 |
 Straight engine |
 --------------- |
            21.4 |
            28.6 |
                 |
            50.0 |
                 |
                 |
            14.0 |
                                                                           
 |              |           |                     |              | #Total |
 |              |           |                     |              |        |
 | ------------ | --------- | ------------------- | ------------ | ------ |
 | Transmission | Automatic | Number of cylinders |            4 |   15.8 |
 |              |           |                     |            6 |   21.1 |
 |              |           |                     |            8 |   63.2 |
 |              |           |                     | #Total cases |   19.0 |
 |              |    Manual | Number of cylinders |            4 |   61.5 |
 |              |           |                     |            6 |   23.1 |
 |              |           |                     |            8 |   15.4 |
 |              |           |                     | #Total cases |   13.0 |
                             
   Engine |                 |
 V-engine | Straight engine |
 -------- | --------------- |
          |            42.9 |
          |            57.1 |
    100.0 |                 |
     12.0 |             7.0 |
     16.7 |           100.0 |
     50.0 |                 |
     33.3 |                 |
      6.0 |             7.0 |
                                                                               
 |              |           |                     |                  | #Total |
 |              |           |                     |                  |        |
 | ------------ | --------- | ------------------- | ---------------- | ------ |
 | Transmission | Automatic | Number of cylinders | #number of cases |   19.0 |
 |              |           |                     |           #row % |  100.0 |
 |              |           |                     |                4 |   15.8 |
 |              |           |                     |                6 |   21.1 |
 |              |           |                     |                8 |   63.2 |
 |              |    Manual | Number of cylinders | #number of cases |   13.0 |
 |              |           |                     |           #row % |  100.0 |
 |              |           |                     |                4 |   61.5 |
 |              |           |                     |                6 |   23.1 |
 |              |           |                     |                8 |   15.4 |
                             
   Engine |                 |
 V-engine | Straight engine |
 -------- | --------------- |
     12.0 |             7.0 |
     63.2 |            36.8 |
          |            42.9 |
          |            57.1 |
    100.0 |                 |
      6.0 |             7.0 |
     46.2 |            53.8 |
     16.7 |           100.0 |
     50.0 |                 |
     33.3 |                 |
                                                                             
 |             |              | #Total | Evaluation of tested brand |       |
 |             |              |        |                 Dislike it | So-so |
 | ----------- | ------------ | ------ | -------------------------- | ----- |
 | Used brands |      Brand A |     70 |                       71.4 |  71.4 |
 |             |      Brand B |     75 |                      100.0 |  71.4 |
 |             |      Brand C |     65 |                       57.1 |  57.1 |
 |             |      Brand D |     65 |                       71.4 |  42.9 |
 |             |      Brand E |     60 |                       57.1 |  71.4 |
 |             | #Total cases |     20 |                        7.0 |   7.0 |
          
         |
 Like it |
 ------- |
    66.7 |
    50.0 |
    83.3 |
    83.3 |
    50.0 |
     6.0 |
                                                                         
 |             |                  | #Total | Evaluation of tested brand |
 |             |                  |        |                 Dislike it |
 | ----------- | ---------------- | ------ | -------------------------- |
 | Used brands |          Brand A |   20.9 |                         20 |
 |             |          Brand B |   22.4 |                         28 |
 |             |          Brand C |   19.4 |                         16 |
 |             |          Brand D |   19.4 |                         20 |
 |             |          Brand E |   17.9 |                         16 |
 |             | #Total responses |   67.0 |                         25 |
                  
       |         |
 So-so | Like it |
 ----- | ------- |
  22.7 |      20 |
  22.7 |      15 |
  18.2 |      25 |
  13.6 |      25 |
  22.7 |      15 |
  22.0 |      20 |

expss documentation built on Jan. 8, 2021, 5:38 p.m.