Skip to contents

Counts the number of occurrences of the different values in each column of a data frame

Usage

frequencyTable(
  data,
  columns = names(data),
  orderByLeastLevels = TRUE,
  as.data.frame = TRUE,
  useNA = c("no", "ifany", "always")[2]
)

Arguments

data

data frame

columns

columns of data to be included in the frequency analysis. Default: all columns of data

orderByLeastLevels

if TRUE (default) the list elements in the output list each of which represents one column of data or the sections of rows in the output data frame are orderd by length(unique(data[[column]]))

as.data.frame

if TRUE (default) the result is a data frame, otherwise a list (see below)

useNA

passed to table see there. Default: "ifany"

Value

for as.data.frame = FALSE a list of data frames each of which represents the frequency statistics for one column of data. Each data frame has columns column (name of the column of data),

value (value occurring in column of data),

count (number of occurrences). For as.data.frame = TRUE one data frame being the result of rbind-ing together these data frames.

Examples

# Some example data
(data <- data.frame(
  A = c("a1", "a2", "a1", "a1", "a2", "", "a2", NA, "a1"),
  B = c("b1", "b1", NA, "b2", "b2", "b1", " ", "b3", "b2")
))
#>      A    B
#> 1   a1   b1
#> 2   a2   b1
#> 3   a1 <NA>
#> 4   a1   b2
#> 5   a2   b2
#> 6        b1
#> 7   a2     
#> 8 <NA>   b3
#> 9   a1   b2

frequencyTable(data) # results in a data frame
#>   column value count
#> 1      A    a1     4
#> 2      A    a2     3
#> 3      A           1
#> 4      A  <NA>     1
#> 5      B    b1     3
#> 6      B    b2     3
#> 7      B           1
#> 8      B    b3     1
#> 9      B  <NA>     1

frequencyTable(data, as.data.frame = FALSE) # results in a list
#> $A
#>   column value count
#> 1      A    a1     4
#> 2      A    a2     3
#> 3      A           1
#> 4      A  <NA>     1
#> 
#> $B
#>   column value count
#> 1      B    b1     3
#> 2      B    b2     3
#> 3      B           1
#> 4      B    b3     1
#> 5      B  <NA>     1
#>