如何重新编码李克特量表调查响应,以便在向量中合并类似响应?

发布于 2025-01-10 19:07:33 字数 566 浏览 1 评论 0原文

我的李克特量表反应范围为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2025-01-17 19:07:33

从这里获取建议:如何重新编码向量中的多个值变成一个值?

x <- sample(1:8, 15, replace = T)
x
#>  [1] 8 1 7 3 1 5 3 8 2 7 3 7 1 5 6

keysvals <- setNames(rep(c("DEMOCRAT", "INDEPENDENT", "REPUBLICAN", NA), c(3,1,3,1)), 1:8)
dplyr::recode(x, !!!keysvals)
#>  [1] NA           "DEMOCRAT"   "REPUBLICAN" "DEMOCRAT"   "DEMOCRAT"  
#>  [6] "REPUBLICAN" "DEMOCRAT"   NA           "DEMOCRAT"   "REPUBLICAN"
#> [11] "DEMOCRAT"   "REPUBLICAN" "DEMOCRAT"   "REPUBLICAN" "REPUBLICAN"

Taking suggestions from here : How to recode multiple values in vector into one value?

x <- sample(1:8, 15, replace = T)
x
#>  [1] 8 1 7 3 1 5 3 8 2 7 3 7 1 5 6

keysvals <- setNames(rep(c("DEMOCRAT", "INDEPENDENT", "REPUBLICAN", NA), c(3,1,3,1)), 1:8)
dplyr::recode(x, !!!keysvals)
#>  [1] NA           "DEMOCRAT"   "REPUBLICAN" "DEMOCRAT"   "DEMOCRAT"  
#>  [6] "REPUBLICAN" "DEMOCRAT"   NA           "DEMOCRAT"   "REPUBLICAN"
#> [11] "DEMOCRAT"   "REPUBLICAN" "DEMOCRAT"   "REPUBLICAN" "REPUBLICAN"
各自安好 2025-01-17 19:07:33

我建议使用 dplyr 中的 case_when

下面的示例将 8(或者实际上任何与之前的 1 到 7 不匹配的任何内容)重新编码为 NA 符号,而不是字符值“NA”。但是,如果您确实想使用“NA”,则可以用它替换。

set.seed(39)

df <- data.frame(
  x = sample(1:8, 10, replace = T)
)

library(dplyr)

df %>%
  mutate(x_recoded = case_when(
    x %in% 1:3 ~ "DEMOCRAT",
    x == 4 ~ "INDEPENDENT",
    x %in% 5:7 ~ "REPUBLICAN",
    TRUE ~ NA_character_
  ))

输出

   x   x_recoded
1  1    DEMOCRAT
2  3    DEMOCRAT
3  8        <NA>
4  8        <NA>
5  2    DEMOCRAT
6  5  REPUBLICAN
7  6  REPUBLICAN
8  8        <NA>
9  4 INDEPENDENT
10 5  REPUBLICAN

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.

set.seed(39)

df <- data.frame(
  x = sample(1:8, 10, replace = T)
)

library(dplyr)

df %>%
  mutate(x_recoded = case_when(
    x %in% 1:3 ~ "DEMOCRAT",
    x == 4 ~ "INDEPENDENT",
    x %in% 5:7 ~ "REPUBLICAN",
    TRUE ~ NA_character_
  ))

Output

   x   x_recoded
1  1    DEMOCRAT
2  3    DEMOCRAT
3  8        <NA>
4  8        <NA>
5  2    DEMOCRAT
6  5  REPUBLICAN
7  6  REPUBLICAN
8  8        <NA>
9  4 INDEPENDENT
10 5  REPUBLICAN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文