Generate table with multiple weighted crossresult (full sample is first column). kable(), which is found in library(knitr), is recommended for use with RMarkdown.

tabs(
  df,
  x,
  y,
  type = "percent",
  percent = FALSE,
  weight = NULL,
  normwt = FALSE,
  na.rm = TRUE,
  na.show = FALSE,
  exclude = NULL,
  digits = 1
)

Arguments

df

A data.frame that contains x and (optionally) y and weight.

x

variable name (found in df). tabs(my.data, x = 'q1').

y

one (or more) variable names. tabs(my.data, x = 'q1', y = c('sex', 'job')).

type

'percent' (default ranges 0-100), 'proportion', or 'counts' (type of table returned).

percent

if TRUE, add a percent sign after the values when printing

weight

variable name for weight (found in df).

normwt

if TRUE, normalize weights so that the total weighted count is the same as the unweighted one

na.rm

if TRUE, remove NA values before computation

na.show

if TRUE, show NA count in table output

exclude

values to remove from x and y. To exclude NA, use na.rm argument.

digits

Number of digits to display; ?format.proptab for formatting details.

Details

tabs calls wtd.table on `x` and, as applicable, each variable named by `y`.

Author

Pete Mohanty

Examples

data(hdv2003) 
tabs(hdv2003, x = "relig", y = c("qualif", "trav.imp"), weight = "poids")
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
#>                             Overall Ouvrier specialise Ouvrier qualifie
#> Pratiquant regulier         12.5     1.5                1.2            
#> Pratiquant occasionnel      21.8     2.1                3.2            
#> Appartenance sans pratique  37.9     4.7                7.0            
#> Ni croyance ni appartenance 21.3     2.9                4.0            
#> Rejet                        4.7     0.2                1.4            
#> NSP ou NVPR                  1.9     0.3                0.2            
#>                             Technicien Profession intermediaire Cadre Employe
#> Pratiquant regulier          0.2        1.2                      1.7   4.8   
#> Pratiquant occasionnel       0.8        2.3                      4.0   8.3   
#> Appartenance sans pratique   3.1        3.1                      6.5  13.3   
#> Ni croyance ni appartenance  1.2        2.1                      3.5   7.6   
#> Rejet                        0.4        0.3                      0.6   1.4   
#> NSP ou NVPR                  0.1        0.5                      0.1   0.9   
#>                             Autre Le plus important
#> Pratiquant regulier          0.3   0.1             
#> Pratiquant occasionnel       0.9   0.4             
#> Appartenance sans pratique   1.1   0.9             
#> Ni croyance ni appartenance  0.4   0.5             
#> Rejet                        0.4   0.1             
#> NSP ou NVPR                  0.1   0.0             
#>                             Aussi important que le reste
#> Pratiquant regulier          2.3                        
#> Pratiquant occasionnel       4.0                        
#> Appartenance sans pratique   9.9                        
#> Ni croyance ni appartenance  7.4                        
#> Rejet                        0.9                        
#> NSP ou NVPR                  0.2                        
#>                             Moins important que le reste Peu important
#> Pratiquant regulier          4.4                          0.3         
#> Pratiquant occasionnel      15.9                          0.7         
#> Appartenance sans pratique  28.0                          1.3         
#> Ni croyance ni appartenance 16.5                          1.9         
#> Rejet                        3.0                          0.4         
#> NSP ou NVPR                  0.8                          0.0         
result <- tabs(hdv2003, x = "relig", y = c("qualif", "trav.imp"), type = "counts")
#> Warning: no weights argument given, using uniform weights of 1
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
#> Warning: no weights argument given, using uniform weights of 1
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
#> Warning: no weights argument given, using uniform weights of 1
#> Warning: 'na.rm' and 'na.show' are ignored when 'useNA' is provided.
format(result, digits = 3)
#>                             Overall Ouvrier specialise Ouvrier qualifie
#> Pratiquant regulier         "266"   " 33"              " 22"           
#> Pratiquant occasionnel      "442"   " 45"              " 59"           
#> Appartenance sans pratique  "760"   " 75"              "120"           
#> Ni croyance ni appartenance "399"   " 41"              " 69"           
#> Rejet                       " 93"   "  4"              " 17"           
#> NSP ou NVPR                 " 40"   "  5"              "  5"           
#>                             Technicien Profession intermediaire Cadre Employe
#> Pratiquant regulier         "  5"      " 17"                    " 29" " 86"  
#> Pratiquant occasionnel      " 10"      " 34"                    " 59" "139"  
#> Appartenance sans pratique  " 44"      " 57"                    "106" "222"  
#> Ni croyance ni appartenance " 18"      " 40"                    " 52" "104"  
#> Rejet                       "  6"      "  7"                    " 12" " 27"  
#> NSP ou NVPR                 "  3"      "  5"                    "  2" " 16"  
#>                             Autre Le plus important
#> Pratiquant regulier         " 10" "  2"            
#> Pratiquant occasionnel      " 17" "  7"            
#> Appartenance sans pratique  " 20" " 12"            
#> Ni croyance ni appartenance "  5" "  6"            
#> Rejet                       "  4" "  2"            
#> NSP ou NVPR                 "  2" "  0"            
#>                             Aussi important que le reste
#> Pratiquant regulier         " 24"                       
#> Pratiquant occasionnel      " 49"                       
#> Appartenance sans pratique  "107"                       
#> Ni croyance ni appartenance " 66"                       
#> Rejet                       " 10"                       
#> NSP ou NVPR                 "  3"                       
#>                             Moins important que le reste Peu important
#> Pratiquant regulier         " 54"                        "  5"        
#> Pratiquant occasionnel      "163"                        "  9"        
#> Appartenance sans pratique  "283"                        " 16"        
#> Ni croyance ni appartenance "160"                        " 16"        
#> Rejet                       " 35"                        "  5"        
#> NSP ou NVPR                 " 13"                        "  1"        
# library(knitr)
# xt <- tabs(hdv2003, x = "relig", y = c("qualif", "trav.imp"), weight = "poids")
# kable(format(xt))                        # to use with RMarkdown...