熔化和转换标签不当的李克特量表 R

Melting and converting badly labeled likert Scale R

在我的调查中,我犯了一个 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

请告诉我能否以某种方式更清楚地表达我的问题风格,因为我想成为社区中有价值的成员。

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

对于你的情况,你可以尝试类似的方法:

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
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