每个人。我的任务是将带有8列的三个数据集连接成一个带R的一个,然后: 1)从第1-5栏开始 - 所有问题都应转换为NA 2)从第1-5栏开始 - 所有字符串和非数字答案都应转换为-2。
问题在于,当我正在读取文件时,它会自动将所有内容转换为字符,当我应用as.numeric函数时,它会将所有不可能转换为数字的字符转换为NA,因此将字符串转换为NA,同时无法跟踪后面的内容。
例如,我有:
Answer.1 Answer.2 Answer.3
1 don't know <Unanswered> 5
2 4 a-1 <Unanswered>
3 1 5 4
我想要的输出是:
Answer.1 Answer.2 Answer.3
1 -2 NA 5
2 4 -2 NA
3 1 5 4
我确实通过输入这个丑陋的代码找到了解决方案:
copy <- bind_rows(group1, group2, group3)
copy[, 1:5] <- gsub("<Unanswered>", "5000", copy[,1:6])
copy$Answer.1 <- gsub("<Unanswered>", "5000", copy$Answer.1)
copy$Answer.2 <- gsub("<Unanswered>", "5000", copy$Answer.2)
copy$Answer.3 <- gsub("<Unanswered>", "5000", copy$Answer.3)
copy$Answer.4 <- gsub("<Unanswered>", "5000", copy$Answer.4)
copy$Answer.5 <- gsub("<Unanswered>", "5000", copy$Answer.5)
copy$Answer.1 <- as.numeric(copy$Answer.1)
copy$Answer.2 <- as.numeric(copy$Answer.2)
copy$Answer.3 <- as.numeric(copy$Answer.3)
copy$Answer.4 <- as.numeric(copy$Answer.4)
copy$Answer.5 <- as.numeric(copy$Answer.5)
copy[is.na(copy)] <- -2
copy[copy == 5000] <- NA
然而,我很好奇是否有更有效的方法来做到这一点。请记住,我们的任务不允许我们使用除dplyr和tidyr之外的任何其他软件包。
答案 0 :(得分:0)
在评论中提出问题的解决方案。
n <- cbind(apply(n[,1:5], 2 , function(x) ifelse(x > 24, -2, x)), n[,6])
#Output
n1 n2 n3 n4 n5
[1,] 2 2 2 2 2 2
[2,] 4 4 4 4 4 4
[3,] 6 6 6 6 6 6
[4,] 8 8 8 8 8 8
[5,] 10 10 10 10 10 10
[6,] 12 12 12 12 12 12
[7,] 14 14 14 14 14 14
[8,] 16 16 16 16 16 16
[9,] 18 18 18 18 18 18
[10,] 20 20 20 20 20 20
[11,] 22 22 22 22 22 22
[12,] 24 24 24 24 24 24
[13,] -2 -2 -2 -2 -2 26
[14,] -2 -2 -2 -2 -2 28
[15,] -2 -2 -2 -2 -2 30
[16,] -2 -2 -2 -2 -2 32
[17,] -2 -2 -2 -2 -2 34
[18,] -2 -2 -2 -2 -2 36
[19,] -2 -2 -2 -2 -2 38
[20,] -2 -2 -2 -2 -2 40
[21,] -2 -2 -2 -2 -2 42
[22,] -2 -2 -2 -2 -2 44
[23,] -2 -2 -2 -2 -2 46
[24,] -2 -2 -2 -2 -2 48
[25,] -2 -2 -2 -2 -2 50
这里n是数据集的名称,我假设只有6列。您可以根据数据集进行更改。
更简单的解决方案是:
n[1:5] <- lapply(n[1:5], function(x) ifelse(x > 24, -2, x))