问题
The goal is to produce a frequency table of all my selected variables (about reading habits for 4 Newspapers) which in essence have the same possible values:
1= Subscribed
2= Every week
3= Sometimes
4= Never
0= NA (No Answers)
The problem arises if one of the variables does not contain one of the possible value. For example, if no one is subscribed to that particular Newspaper.
a <- c(1,2,3,4,3,1,2,3,4,3)
b <- c(2,2,3,4,3,0,0,3,4,1)
d <- c(2,2,3,4,3,0,0,0,0,0)
e <- c(3,3,3,3,3,3,3,3,3,3)
ta <- table(a)
tb <- table(b)
td <- table(d)
te <- table(e)
abde <- cbind(ta,tb,td,te)
ta tb td te
0 2 2 5 10
1 2 1 2 10
2 4 2 2 10
3 2 3 1 10
4 2 2 5 10
Zero Frequencies are replaced by a duplicate of the last value.
How can this be acheived in a better way?
回答1:
I think you are looking for factor
:
> L <- list(a, b, d, e)
> A <- sort(unique(unlist(L, use.names = FALSE)))
> sapply(L, function(x) table(factor(x, A)))
[,1] [,2] [,3] [,4]
0 0 2 5 0
1 2 1 0 0
2 2 2 2 0
3 4 3 2 10
4 2 2 1 0
Update
Here's an approach in base R that might even be more direct:
> L <- mget(c("a", "b", "d", "e"))
> table(stack(L))
ind
values a b d e
0 0 2 5 0
1 2 1 0 0
2 2 2 2 0
3 4 3 2 10
4 2 2 1 0
回答2:
You could use mtabulate
from qdapTools
library(qdapTools)
t(mtabulate(list(a,b,d,e)))
# [,1] [,2] [,3] [,4]
#0 0 2 5 0
#1 2 1 0 0
#2 2 2 2 0
#3 4 3 2 10
#4 2 2 1 0
Or
t(mtabulate(data.frame(a,b,d,e)))
# a b d e
#0 0 2 5 0
#1 2 1 0 0
#2 2 2 2 0
#3 4 3 2 10
#4 2 2 1 0
回答3:
This is similar to @Anandas solution (I will post it because was already in middle of writing)
df <- data.frame(a, b, d, e)
do.call(cbind, lapply(df, function(x) table(factor(x, levels = 0:4))))
# a b d e
# 0 0 2 5 0
# 1 2 1 0 0
# 2 2 2 2 0
# 3 4 3 2 10
# 4 2 2 1 0
来源:https://stackoverflow.com/questions/28485667/appending-frequency-tables-with-missing-values