Skip to contents

Generates a frequency table from a data frame, including number of records by a specified variable or variables in the data frame with optional totals and/or subtotals.

Usage

datFreq(
  x,
  xvar = NULL,
  total = FALSE,
  subtotal = FALSE,
  subtotalcol = NULL,
  savedata = FALSE,
  savedata_opts = NULL,
  gui = FALSE
)

Arguments

x

Data frame or comma-delimited file (*.csv). The table with the variable(s).

xvar

String (vector).* The name of the variable(s) to summarize.

total

Logical. If TRUE, a row is added to bottom of table with a total for the whole table.

subtotal

Logical. If TRUE, a row is added to bottom of each section for subtotals.

subtotalcol

Logical. If subtotal=TRUE, the column(s) to generate subtotals.

savedata

Logical. If TRUE, writes output data to outfolder.

savedata_opts

List. See help(savedata_options()) for a list of options. Only used when savedata = TRUE. If out_layer = NULL, default = 'datfreq'.

gui

Logical. If TRUE, pop-up windows will appear for user-interface.

Value

freqtable

Data frame. The frequency table.

If savedata=TRUE, a comma-delimited file of the frequency table is written to the outfolder.

Details

If no parameters, then user is prompted for input. If partial parameters, default parameter values are used.

Author

Tracey S. Frescino

Examples

# Set up data for example
tab <- data.frame(cbind(CONDCLASS = c(1, 1, 2, 1, 3, 3, 3, 1, 1, 1, 2, 1), 
                         FORTYPCD = c(182, 184, 201, 221, 221, 184, 221, 182,
                                      182, 201, 182, 221)))

# Frequency table with "FORTYPCD"       
datFreq(x = tab,
        xvar = "FORTYPCD")
#>   FORTYPCD FREQ
#> 1      182    4
#> 2      184    2
#> 3      201    2
#> 4      221    4
                                      
# Frequency table with "CONDCLASS" and "FORTYPCD"                                        
datFreq(x = tab,
        xvar = c("CONDCLASS", "FORTYPCD"))
#>   CONDCLASS FORTYPCD FREQ
#> 1         1      182    3
#> 2         1      184    1
#> 3         1      201    1
#> 4         1      221    2
#> 5         2      182    1
#> 6         2      201    1
#> 7         3      184    1
#> 8         3      221    2

# Frequency table with "CONDCLASS" and "FORTYPCD", adding total and subtotal
# rows                                        
datFreq(x = tab,
        xvar = c("CONDCLASS", "FORTYPCD"), 
        total = TRUE, 
        subtotal = TRUE)
#>    CONDCLASS FORTYPCD FREQ
#> 1          1      182    3
#> 5          2      182    1
#> 3            Subtotal    4
#> 2          1      184    1
#> 7          3      184    1
#> 6            Subtotal    2
#> 31         1      201    1
#> 61         2      201    1
#> 9            Subtotal    2
#> 4          1      221    2
#> 8          3      221    2
#> 12           Subtotal    4
#> 13              Total   12
        
# Frequency table for WYtree, multiple variables, subtotal options
datFreq(x = FIESTA::WYtree,
        xvar = c("SPGRPCD", "SPCD", "STATUSCD"), 
         subtotal = TRUE, subtotalcol = "SPCD")
#>     SPGRPCD SPCD STATUSCD FREQ
#> 1        10  202        1  856
#> 2        10  202        2  381
#> 3                Subtotal 1237
#> 31       11  122        1 1084
#> 4        11  122        2  185
#> 6                Subtotal 1269
#> 5        12   19        1 2687
#> 61       12   19        2  891
#> 9                Subtotal 3578
#> 7        18   93        1 1598
#> 8        18   93        2  677
#> 12               Subtotal 2275
#> 91       18   96        1   12
#> 14               Subtotal   12
#> 10       21  108        1 4424
#> 11       21  108        2 2110
#> 17               Subtotal 6534
#> 121      23   65        1  277
#> 13       23   65        2   46
#> 20               Subtotal  323
#> 141      23   66        1  405
#> 15       23   66        2   52
#> 23               Subtotal  457
#> 16       23  106        1    1
#> 25               Subtotal    1
#> 171      24  101        1  674
#> 18       24  101        2  637
#> 28               Subtotal 1311
#> 19       24  113        1  266
#> 201      24  113        2  311
#> 311              Subtotal  577
#> 21       44  745        1   16
#> 22       44  745        2    1
#> 34               Subtotal   17
#> 231      44  746        1  538
#> 24       44  746        2  283
#> 37               Subtotal  821
#> 251      44  749        1   26
#> 26       44  749        2    3
#> 40               Subtotal   29
#> 27       47  313        1    9
#> 281      47  313        2    4
#> 43               Subtotal   13
#> 29       47  375        1    1
#> 30       47  375        2    1
#> 46               Subtotal    2
#> 312      47  544        1   11
#> 48               Subtotal   11
#> 32       47  823        1   87
#> 33       47  823        2   14
#> 51               Subtotal  101
#> 341      48  475        1    4
#> 35       48  475        2    2
#> 54               Subtotal    6