看起来好像您tab
是使用as.data.frame(table(...)))
or创建的data.frame(table(...))
。
as.data.frame.table
并将data.frame(table(...))
强制 dimnames 为factor
.
# an example
foo <- data.frame(a = sample(10,size=100,replace=TRUE),b = sample(10, size = 50, replace = TRUE))
str(data.frame(table(foo))
# 'data.frame': 100 obs. of 3 variables:
# $ a : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# $ b : Factor w/ 10 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
# $ Freq: int 1 2 2 2 3 0 4 0 0 0 ..
str(as.data.frame(table(foo))
# 'data.frame': 100 obs. of 3 variables:
# $ a : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# $ b : Factor w/ 10 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
# $ Freq: int 1 2 2 2 3 0 4 0 0 0 ..
您可以使用reshape2
创建频率表表,这将保留变量的类型
library(reshape2)
str(dcast(a+b~'Freq',data = foo,value.var = 'b', fun = length))
# 'data.frame': 56 obs. of 3 variables:
# $ a : int 1 1 1 1 1 1 1 1 2 2 ...
# $ b : int 1 2 3 5 6 7 8 10 1 2 ...
# $ Freq: int 1 1 2 4 2 2 1 2 2 5 ...
或使用data.table
library(data.table)
DT <- data.table(foo)
str(DT[, .N, by = list(a,b)])
# Classes ‘data.table’ and 'data.frame': 56 obs. of 3 variables:
# $ a: int 1 4 5 10 2 1 5 4 4 7 ...
# $ b: int 5 6 1 3 2 6 4 4 5 1 ...
# $ N: int 4 1 3 1 5 2 3 3 1 4 ...
# - attr(*, ".internal.selfref")=<externalptr>