简体   繁体   English

使用表功能时如何获取表而不是列表作为输出,而我的因素之一是缺少水平?

[英]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.

相关问题 如何在r的表格中巧妙地呈现两个因子的不同组合的输出? - How do I neatly present the output of different combinations of two factors in a table in r? 我如何修复摘要/系数表 R 中缺失的 output (NA) - How do i fix the missing output (NA) in my summary/coefficients table R 我如何制作一个 latex 表,其中一组因素作为行,另一组因素作为列 - How would I make a latex table with one set of factors as the rows and the other set of factors as the columns 在 R 中使用 By 函数时,如何为每个组创建因子向量? - When using the function By in R, how do I create a vector of the factors for each group? 如何根据列中的级别对重复值求和并输出计数表? - How do I sum recurring values according to a level in a column and output a table of counts? 在 R 中使用 sf 包中的 st_read 时,如何从 Postgre 表中获得正确的编码 - How do I get correct encoding from my Postgre table when using st_read from the sf package in R 使用table()函数获取频率表时获取&lt;范围表0&gt; - getting < table of extent 0 > when using table() function to get table of frequency 具有因子名称而不是计数的列联表 - Contingency table with name of factors instead of counts 为什么在 R 中的 data.table 中使用“熔化”函数时会收到此错误消息“找不到函数”模式 - Why do I get this error message "could not find function "patterns" when using the "melt" function in data.table in R R:当使用data.table时,当我执行x [y]时如何获得y的列? - R: When using data.table how do I get columns of y when I do x[y]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM