count_each_column {plothelper} | R Documentation |
Counting Each Column and Summarizing in a Matrix
Description
This function counts the frequencies of each element of each column of a data frame or matrix. The frequencies of missing values and the 0 frequencies of non-existent values are also included in the final result.
Usage
count_each_column(x, answer = NULL, checks = TRUE)
Arguments
x |
a data frame or matrix with at least 1 row and
1 column. NOTE: all column should belong to the same
class (numeric, character).
However, if |
answer |
the values whose frequencies you want to know, e. g., "agree" and "disagree" in your survey data. Default is NULL which means all possible answers in the whole data will be used. |
checks |
whether to check the validity of the input data. Default is TRUE. Do not turn it off unless you are sure that your data has no logical variables or factor variables and each column has at least 1 non-missing value. |
Examples
# values that do not appear in
# the data can also be counted.
# a factor will be transformed into
# a character variable automatically.
x1=c("a", "b", "a", "b", NA)
x2=factor(x1)
x3=c("1", "3", "2", "1", "a")
dat=data.frame(x1, x2, x3, stringsAsFactors=FALSE)
res=count_each_column(dat, answer=c("c", "d", NA, "a"))
# logical value is OK.
x1=c(TRUE, TRUE, TRUE)
x2=c(TRUE, NA, NA)
dat=data.frame(x1, x2)
res=count_each_column(dat)
res=count_each_column(dat, c(TRUE, FALSE))