我有一个调查数据集,我需要检查几个问题替代答案(列),以确定受访者是否没有回答这些项目。我可以通过查看受访者在一组特定项目中有多少“否”来推断这一点。 例如,通过以下示例,我想从第2列到第6列检查是否:(1)受访者对所有替代方案都回答“否”。如果是这样,(2)对于每一行,No必须成为该范围内的NA。
mydat <- data.frame(ID=sample(n, n, FALSE),
Q01=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q02=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q03=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q04=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q05=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q06=factor(sample(c('Yes', 'No', 'NA'), n, TRUE, prob = c(.5,.3,.2))),
Q07=factor(sample(c('Female', 'Male'), n, TRUE)))
答案 0 :(得分:1)
仅检查Q1-Q7以查看被访者是否回答否。如果所有Q1-Q7都为否,则该行中的NA设置为NA
set.seed(321)
n <- 20
mydat <- data.frame(ID=sample(n, n, FALSE),
Q01=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q02=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q03=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q04=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q05=factor(sample(c('No', 'Yes'), n, TRUE, prob = c(.6,.3))),
Q06=factor(sample(c('Yes', 'No', 'NA'), n, TRUE, prob = c(.5,.3,.2))),
Q07=factor(sample(c('Female', 'Male'), n, TRUE)))
检查哪些是否
chk_answer <- rowSums(mydat[, 2:7] == "No") == 6
which(chk_answer)
[1] 10
并将该行分配给NA
mydat[chk_answer, 2:7] <- NA
mydat
ID Q01 Q02 Q03 Q04 Q05 Q06 Q07
1 20 No Yes No No No NA Female
2 18 Yes No No No No Yes Female
3 5 Yes No No No No No Female
4 19 No Yes No Yes Yes No Male
5 7 No Yes Yes No No NA Female
6 6 Yes No No No Yes No Male
7 16 Yes No Yes No No Yes Male
8 4 No Yes Yes Yes Yes No Female
9 15 No No No Yes No NA Male
10 9 <NA> <NA> <NA> <NA> <NA> <NA> Female
11 14 No No No No No Yes Female
12 13 Yes No No No No Yes Male
13 10 Yes No No No No Yes Male
14 1 Yes No Yes Yes No No Male
15 11 No No Yes Yes No Yes Male
16 2 No Yes Yes No Yes NA Female
17 3 Yes No No Yes Yes No Male
18 17 No No Yes Yes No Yes Male
19 8 Yes No No No No Yes Male