如何重新编码李克特量表调查响应,以便在向量中合并类似响应?
我的李克特量表反应范围为 1:7,其中 8 表示“不知道”。我想将 1:3、4、5:7 重新编码为一个新变量,而不是具有 8 个不同响应的向量,我有一个新变量来合并 1:3、4 和 5:7 并忽略“don”不知道”的回应。我想将其称为“pid3”。该向量来自导入的轮询数据。它被称为“pid7”。它很长,所以我无法手动重新输入它。抱歉 - 这是我第一次在这里提问。我对 R 不太流利。
library(dplyr)
class(pop$pid7)
pid3 <- data.frame(x = c("DEMOCRAT", "INDEPENDENT", "REPUBLICAN"))
pid7 <- recode(pop$pid7, x_recoded = recode(x, "DEMOCRAT" = 1:3, "INDEPENDENT" = 4, "REPUBLICAN" = 5:7, "NA"= 8))
dplyr::recode(pop$pid7, "DEMOCRAT" = 1,2,3, "INDI" = 4, "REPUBLICAN" = 5,6,7, "NA" = 8)
这些是我尝试过的事情。我不明白我需要按照什么顺序做事。
I have likert scale responses ranging from 1:7 where 8 is "don't know". I'd like to recode 1:3, 4, 5:7 as a new variable where instead of a vector with 8 different responses, I have a new variable that consolidates 1:3, 4, and 5:7 and ignores "don't know" responses. I want to call it "pid3." The vector comes from imported polling data. It is called "pid7." It is very long so I cannot manually re-type it. Sorry- this is my first time asking a question here. I am not fluent in R.
library(dplyr)
class(pop$pid7)
pid3 <- data.frame(x = c("DEMOCRAT", "INDEPENDENT", "REPUBLICAN"))
pid7 <- recode(pop$pid7, x_recoded = recode(x, "DEMOCRAT" = 1:3, "INDEPENDENT" = 4, "REPUBLICAN" = 5:7, "NA"= 8))
dplyr::recode(pop$pid7, "DEMOCRAT" = 1,2,3, "INDI" = 4, "REPUBLICAN" = 5,6,7, "NA" = 8)
these are the things I've tried. I don't understand what order I need to do things in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从这里获取建议:如何重新编码向量中的多个值变成一个值?
Taking suggestions from here : How to recode multiple values in vector into one value?
我建议使用 dplyr 中的
case_when
。下面的示例将 8(或者实际上任何与之前的 1 到 7 不匹配的任何内容)重新编码为
NA
符号,而不是字符值“NA”。但是,如果您确实想使用“NA”,则可以用它替换。输出
I would recommend using
case_when
from `dplyr.The below example recodes 8 (or really anything that doesn't match 1 through 7 before it) to
NA
symbol, and not character value "NA". However, if you did want to use "NA" you can substitute with that.Output