融化并转换标签错误的Likert Scale R

时间:2020-09-08 07:38:11

标签: r mutate reorderlist likert

在我的调查中,我对5点李克特量表犯了一个错误,如下所示:

dput(head(edu_data))
structure(list(Education.1. = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("", "Y"), class = "factor"), Education.2. = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), 
Education.3. = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"Y"), class = "factor"), Education.4. = structure(c(1L, 1L, 
1L, 2L, 2L, 1L), .Label = c("", "Y"), class = "factor"), 
Education.5. = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("", 
"Y"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")

我想将其更改为一个具有单个值的列,以便 answer_to_ls = 1:5

我要获取的输出将是一个具有单个数字的列,这意味着要摆脱该字母。当然,我确实有一个唯一的受访者ID

请告诉我我是否想以某种方式变得更清晰,因为我想成为社区的宝贵成员。

2 个答案:

答案 0 :(得分:1)

我认为有很多潜在的解决方案,请尝试将多个二元或二分列合并或折叠为一个列。例如:

R - Convert various dummy/logical variables into a single categorical variable/factor from their name

您可以尝试以下方法:

edu_data$answer_to_ls <- apply(edu_data[1:5] == "Y", 1, function(x) { if (any(x)) { as.numeric(gsub(".*(\\d+).", "\\1", names(which(x)))) } else NA })

这将从列名中为李克特量表响应1到5提取数字,使其为数字值,如果没有“ Y”响应,则包括NA。 edu_data[1:5]选择要考虑进行转换的列,在本例中为第1至5列。

  Education.1. Education.2. Education.3. Education.4. Education.5. answer_to_ls
1                                                                Y            5
2                                                                Y            5
3                                                                Y            5
4                                                   Y                         4
5                                                   Y                         4
6                                                                            NA

答案 1 :(得分:0)

d <- structure(list(Education.1. = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), 
               Education.2. = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"),
               Education.3. = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), 
               Education.4. = structure(c(1L, 1L, 1L, 2L, 2L, 1L), .Label = c("", "Y"), class = "factor"), 
               Education.5. = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor")), 
               row.names = c(NA, 6L), class = "data.frame")

d$item1 <- 1 * (d$Education.1 == "Y") +
           2 * (d$Education.2 == "Y") +
           3 * (d$Education.3 == "Y") +
           4 * (d$Education.4 == "Y") +
           5 * (d$Education.5 == "Y") 

print(d)

导致

> print(d)
  Education.1. Education.2. Education.3. Education.4. Education.5. item1
1                                                                Y     5
2                                                                Y     5
3                                                                Y     5
4                                                   Y                  4
5                                                   Y                  4
6                                                                      0