library(procs)
# Turn off printing for CRAN checks
options("procs.print" = FALSE)
# Create sample data
df <- as.data.frame(HairEyeColor, stringsAsFactors = FALSE)
# Assign labels
labels(df) <- list(Hair = "Hair Color",
Eye = "Eye Color",
Sex = "Sex at Birth")
# Example #1: One way frequencies on Hair and Eye color with weight option.
res <- proc_freq(df,
tables = v(Hair, Eye),
options = outcum,
weight = Freq)
# View result data
res
# $Hair
# VAR CAT N CNT PCT CUMSUM CUMPCT
# 1 Hair Black 592 108 18.24324 108 18.24324
# 2 Hair Blond 592 127 21.45270 235 39.69595
# 3 Hair Brown 592 286 48.31081 521 88.00676
# 4 Hair Red 592 71 11.99324 592 100.00000
#
# $Eye
# VAR CAT N CNT PCT CUMSUM CUMPCT
# 1 Eye Blue 592 215 36.31757 215 36.31757
# 2 Eye Brown 592 220 37.16216 435 73.47973
# 3 Eye Green 592 64 10.81081 499 84.29054
# 4 Eye Hazel 592 93 15.70946 592 100.00000
# Example #2: 2 x 2 Crosstabulation table with Chi-Square statistic
res <- proc_freq(df, tables = Hair * Eye,
weight = Freq,
options = v(crosstab, chisq))
# View result data
res
#$`Hair * Eye`
# Category Statistic Blue Brown Green Hazel Total
#1 Black Frequency 20.000000 68.000000 5.0000000 15.000000 108.00000
#2 Black Percent 3.378378 11.486486 0.8445946 2.533784 18.24324
#3 Black Row Pct 18.518519 62.962963 4.6296296 13.888889 NA
#4 Black Col Pct 9.302326 30.909091 7.8125000 16.129032 NA
#5 Blond Frequency 94.000000 7.000000 16.0000000 10.000000 127.00000
#6 Blond Percent 15.878378 1.182432 2.7027027 1.689189 21.45270
#7 Blond Row Pct 74.015748 5.511811 12.5984252 7.874016 NA
#8 Blond Col Pct 43.720930 3.181818 25.0000000 10.752688 NA
#9 Brown Frequency 84.000000 119.000000 29.0000000 54.000000 286.00000
#10 Brown Percent 14.189189 20.101351 4.8986486 9.121622 48.31081
#11 Brown Row Pct 29.370629 41.608392 10.1398601 18.881119 NA
#12 Brown Col Pct 39.069767 54.090909 45.3125000 58.064516 NA
#13 Red Frequency 17.000000 26.000000 14.0000000 14.000000 71.00000
#14 Red Percent 2.871622 4.391892 2.3648649 2.364865 11.99324
#15 Red Row Pct 23.943662 36.619718 19.7183099 19.718310 NA
#16 Red Col Pct 7.906977 11.818182 21.8750000 15.053763 NA
#17 Total Frequency 215.000000 220.000000 64.0000000 93.000000 592.00000
#18 Total Percent 36.317568 37.162162 10.8108108 15.709459 100.00000
# $`chisq:Hair * Eye`
# STAT DF VAL PROB
# 1 Chi-Square 9 138.2898 2.325287e-25
# 2 Continuity Adj. Chi-Square 9 138.2898 2.325287e-25
#' # Example #3: By variable with named table request
res <- proc_freq(df, tables = v(Hair, Eye, Cross = Hair * Eye),
by = Sex,
weight = Freq)
# View result data
res
# $Hair
# BY VAR CAT N CNT PCT
# 1 Female Hair Black 313 52 16.61342
# 2 Female Hair Blond 313 81 25.87859
# 3 Female Hair Brown 313 143 45.68690
# 4 Female Hair Red 313 37 11.82109
# 5 Male Hair Black 279 56 20.07168
# 6 Male Hair Blond 279 46 16.48746
# 7 Male Hair Brown 279 143 51.25448
# 8 Male Hair Red 279 34 12.18638
#
# $Eye
# BY VAR CAT N CNT PCT
# 1 Female Eye Blue 313 114 36.421725
# 2 Female Eye Brown 313 122 38.977636
# 3 Female Eye Green 313 31 9.904153
# 4 Female Eye Hazel 313 46 14.696486
# 5 Male Eye Blue 279 101 36.200717
# 6 Male Eye Brown 279 98 35.125448
# 7 Male Eye Green 279 33 11.827957
# 8 Male Eye Hazel 279 47 16.845878
#
# $Cross
# BY VAR1 VAR2 CAT1 CAT2 N CNT PCT
# 1 Female Hair Eye Black Blue 313 9 2.8753994
# 2 Female Hair Eye Black Brown 313 36 11.5015974
# 3 Female Hair Eye Black Green 313 2 0.6389776
# 4 Female Hair Eye Black Hazel 313 5 1.5974441
# 5 Female Hair Eye Blond Blue 313 64 20.4472843
# 6 Female Hair Eye Blond Brown 313 4 1.2779553
# 7 Female Hair Eye Blond Green 313 8 2.5559105
# 8 Female Hair Eye Blond Hazel 313 5 1.5974441
# 9 Female Hair Eye Brown Blue 313 34 10.8626198
# 10 Female Hair Eye Brown Brown 313 66 21.0862620
# 11 Female Hair Eye Brown Green 313 14 4.4728435
# 12 Female Hair Eye Brown Hazel 313 29 9.2651757
# 13 Female Hair Eye Red Blue 313 7 2.2364217
# 14 Female Hair Eye Red Brown 313 16 5.1118211
# 15 Female Hair Eye Red Green 313 7 2.2364217
# 16 Female Hair Eye Red Hazel 313 7 2.2364217
# 17 Male Hair Eye Black Blue 279 11 3.9426523
# 18 Male Hair Eye Black Brown 279 32 11.4695341
# 19 Male Hair Eye Black Green 279 3 1.0752688
# 20 Male Hair Eye Black Hazel 279 10 3.5842294
# 21 Male Hair Eye Blond Blue 279 30 10.7526882
# 22 Male Hair Eye Blond Brown 279 3 1.0752688
# 23 Male Hair Eye Blond Green 279 8 2.8673835
# 24 Male Hair Eye Blond Hazel 279 5 1.7921147
# 25 Male Hair Eye Brown Blue 279 50 17.9211470
# 26 Male Hair Eye Brown Brown 279 53 18.9964158
# 27 Male Hair Eye Brown Green 279 15 5.3763441
# 28 Male Hair Eye Brown Hazel 279 25 8.9605735
# 29 Male Hair Eye Red Blue 279 10 3.5842294
# 30 Male Hair Eye Red Brown 279 10 3.5842294
# 31 Male Hair Eye Red Green 279 7 2.5089606
# 32 Male Hair Eye Red Hazel 279 7 2.5089606
Run the code above in your browser using DataLab