我正在设置一个变量描述表,使用getDescriptionStatsBy
from Gmisc
,但如果我想要一个描述两个级别的因子,它只显示一个级别的信息。我知道,关于另一个层面的信息是多余的,许多论文都要求它是这样的。但在这种特定情况下,我需要表中描述的两个级别。一点MRE:
a <- c(1,1,1,2,2,2,2)
b <- c("male","male","male","female","female","female","female")
c <- c("car","train","bike","bike","car","car","train")
df <- data.frame(a,b,c)
df$a <- factor(df$a)
df$b <- factor(df$b)
df$c <- factor(df$c)
仅使用两个级别(男性,女性)我得到:
getDescriptionStatsBy(df$b,by=df$a)
Female df$b
1 2
Female df$b "0 (0.0%)" "4 (100.0%)"
attr(,"class")
[1] "matrix" "array"
为什么使用三个级别的因素(自行车、火车、汽车)我得到:
getDescriptionStatsBy(df$b,by=df$a)
df$c
1 2
bike "1 (33.3%)" "1 (25.0%)"
car "1 (33.3%)" "2 (50.0%)"
train "1 (33.3%)" "1 (25.0%)"
attr(,"class")
[1] "matrix" "array"
有什么办法可以让第一张桌子看起来像第二张桌子?
getDescriptionStatsBy(df$b,by=df$a)
df$c
1 2
Male "3 (100.0%)" "0 (0.0%)"
Female "0 (0.0%)" "4 (100.0%)"
attr(,"class")
[1] "matrix" "array"