1

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

请告诉我,如果我想成为社区中有价值的一员,我能否以某种方式更清楚地说明我的问题。

4

2 回答 2

1

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

R - 将各种虚拟/逻辑变量从其名称转换为单个分类变量/因子

在您的情况下,您可以尝试以下操作:

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
于 2020-09-08T13:17:29.970 回答
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
于 2020-09-08T13:46:37.710 回答