汇总多个回答问题

编程入门 行业动态 更新时间:2024-10-26 06:26:48
本文介绍了汇总多个回答问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

想象一下,我有一个问题,其中有四个选项,而受访者可以选择零或这四个选项的任意组合.这些变量分别命名为A,B,C和D,并且响应存储在data.frame中,如下所示.

Imagine that I have a question for which there are four options, and a respondent can select zero or any combination of the four. The variables are named A, B, C, and D and the responses are stored in a data.frame as below.

set.seed(1) dat = data.frame(A = sample(c(0, 1), 20, replace=TRUE), B = sample(c(0, 1), 20, replace=TRUE), C = sample(c(0, 1), 20, replace=TRUE), D = sample(c(0, 1), 20, replace=TRUE))

我可以通过以下方法将响应的组合制成表格(例如,单独使用A或A + B或C + D等等,有多少响应):

I can tabulate the combination of responses (for example, how many responded with A alone, or A+B, or C+D, and so on) by doing the following:

data.frame(table(dat)) # A B C D Freq # 1 0 0 0 0 2 # 2 1 0 0 0 2 # 3 0 1 0 0 0 # 4 1 1 0 0 1 # 5 0 0 1 0 1 # 6 1 0 1 0 3 # 7 0 1 1 0 0 # 8 1 1 1 0 2 # 9 0 0 0 1 0 # 10 1 0 0 1 2 # 11 0 1 0 1 1 # 12 1 1 0 1 1 # 13 0 0 1 1 2 # 14 1 0 1 1 0 # 15 0 1 1 1 3 # 16 1 1 1 1 0

我现在想创建一个新列,以显示此输出表示的字母组合.例如,第4行代表A + B响应的计数,第14行代表A + C + D响应的计数.

I would like to now create a new column that shows the letter combination that is being represented by this output. For example, row 4 represents the count of A+B responses, and row 14 represents the count of A+C+D responses.

我认为apply函数之一在这里很有用,但是我不确定如何进行.

I think that one of the apply functions would be useful here, but I'm not sure how to proceed.

推荐答案

dat.t <- data.frame(table(dat)) dat.t$combn <- apply(dat.t[,1:4] == 1, 1, function(x) paste(names(dat)[x], collapse=' + ')) > dat.t A B C D Freq combn 1 0 0 0 0 2 2 1 0 0 0 2 A 3 0 1 0 0 0 B 4 1 1 0 0 1 A + B 5 0 0 1 0 1 C 6 1 0 1 0 3 A + C 7 0 1 1 0 0 B + C 8 1 1 1 0 2 A + B + C 9 0 0 0 1 0 D 10 1 0 0 1 2 A + D 11 0 1 0 1 1 B + D 12 1 1 0 1 1 A + B + D 13 0 0 1 1 2 C + D 14 1 0 1 1 0 A + C + D 15 0 1 1 1 3 B + C + D 16 1 1 1 1 0 A + B + C + D >

更多推荐

汇总多个回答问题

本文发布于:2023-11-25 18:35:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!