[英]How do I get a table as output instead of a list when using the table-function and one of my factors is missing a level?
I am processing the results of a questionnaire and want to present the responses on several related Yes/No questions in one single table. 我正在处理调查表的结果,并希望在一个表格中显示几个相关的是/否问题的答案。 On one of these questions all respondents answered "No". 对于这些问题之一,所有受访者都回答“否”。 It appears that as a result of missing one response level in this question, the table-function in R returns a list instead of a table. 似乎由于此问题中缺少一个答复级别,R中的表函数返回一个列表而不是一个表。
Adding factor level "Yes" to the question with zero "Yes" responses, does not solve the problem. 以零个“是”响应向问题中添加因子级别“是”并不能解决问题。 The code I present below, by the way, works fine for questions where respondents answered either "Yes" or "No". 顺便说一句,我在下面提供的代码对于回答者回答“是”或“否”的问题非常有效。
This code relates to three questions answered by five respondents and illustrates my questionnaire. 该代码涉及五个答复者回答的三个问题,并说明了我的调查问卷。
q1 <- c("Yes", "Yes", "Yes", "Yes", "No")
q2 <- c("No", "No", "No", "No", "No") # our culprit
q3 <- c("Yes", "Yes", "No", "No", "No")
From this data I wish to create the following table: 我希望根据这些数据创建下表:
q1 q2 q3
Yes 4 0 2
No 1 5 3
Realizing that factor q2 has only one level I wrote: 意识到因子q2只写了一个级别:
q2 <- factor(q2, levels = c("Yes", "No"))
I combine the question vectors in a dataframe and apply the table-function to its columns: 我将问题向量组合到一个数据框中,并将表函数应用于其列:
df <- data.frame(q1, q2, q3)
apply(df, 2, table)
The actual output of the table-function is not the table above but the list below: table-function的实际输出不是上面的表,而是下面的列表:
$q1
No Yes
1 4
$q2
No
5
$q3
No Yes
3 2
Using dplyr
, you can count each grouping then use fill=0
when spreading to get the desired table. 使用dplyr
,您可以计算每个分组,然后在扩展时使用fill=0
以获得所需的表。
df %>% gather(k,v) %>%
count(k,v) %>% spread(k,n,fill=0)
# A tibble: 2 x 4
v q1 q2 q3
<chr> <dbl> <dbl> <dbl>
1 No 1 5 3
2 Yes 4 0 2
table(stack(list(q1=q1,q2=q2,q3=q3)))
ind
values q1 q2 q3
No 1 5 3
Yes 4 0 2
I think it's better to explicitly list the labels across all the values you want to measure. 我认为最好在要度量的所有值中明确列出标签。 Then you can convert to factor and do the sum 然后,您可以将其转换为因子并求和
vals <- c("Yes", "No")
df <- data.frame(q1, q2, q3)
sapply(lapply(df, factor, levels=vals), table)
Here the inner lapply
creates the factors with the proper levels for all lists, and the sapply
runs the table()
在这里,内部lapply
为所有列表创建具有适当级别的因子,然后sapply
运行table()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.