R:多个条件将两个调查问题(因素)归类为一个水平为None/Mild/Severe的因素
R: Multiple conditions to categorise two survey questions (factors) into one factor with levels None/Mild/Severe
我有两个因素 X 和 Y,有 5 个等级(5 分李克特量表)
"[1] Never" "[2] Rarely" "[3] Sometimes" "[4] Often" "[5] Very Often"
根据这两个答案,我想用以下编码创建一个新的因子 Z:
None:X = 从不,Y = 从不
轻度:X = 很少、有时、经常或非常经常,Y = 从不
严重:Y = 很少、有时、经常或非常经常
我尝试了很多不同的多重条件,但 none 成功了。
这是其中之一:
Z <- c("None", "Mild", "Severe")
factor(Z)
levels(Z) <- c("None", "Mild", "Severe")
if (!is.na((X == 1) && (Y == 1))) {
Z == "None"
} else if (!is.na((X != 1) && (Y == 1))) {
Z == "Mild"
} else (!is.na((X != 1) && (Y != 1))) {
Z == "Severe"
}
错误信息是:
Error: unexpected '{' in:
" Z== "Mild"
} else (!is.na((X != 1) && (Y != 1))) {"
> Z == "Severe"
[1] FALSE FALSE TRUE
> }
Error: unexpected '}' in " }"
>
样本由大约 4000 人组成,我想根据他或她对问题 X 和 Y 的评分知道哪个参与者属于哪个类别(例如抑郁症)。
我还是个 R 初学者,非常感谢您的帮助!
一切顺利
这就够了吗?
您可以使用三个逻辑向量对数据进行子集化。
如果您需要更多详细信息,使用您的一些数据会有所帮助。 (help)
lev <- c("Never", "Never", "Never", "Rarely", "Sometimes", "Often", "Very Often")
set.seed(1)
X <- factor(sample(c(lev, "Never"), 15, replace=TRUE), levels=unique(lev))
Y <- factor(sample(lev, 15, replace=TRUE), levels=unique(lev))
None <- X %in% c("Never") &
Y %in% c("Never")
Mild <- X %in% c("Rarely", "Sometimes", "Often", "Very Often") &
Y %in% c("Never")
Severe <- Y %in% c("Rarely", "Sometimes", "Often", "Very Often")
如果你想将逻辑向量组合成一个单一的因子向量,可以这样做:
Z0 <- rbind(None, Mild, Severe)
z <- which(Z0, arr.ind=TRUE)
Z <- factor(rownames(z), levels=rownames(Z0))
data.frame(X, Y, Z)
# X Y Z
# 1 Never Rarely Severe
# 2 Never Often Severe
# 3 Sometimes Very Often Severe
# 4 Never Never None
# 5 Never Often Severe
# 6 Never Very Often Severe
# 7 Never Never None
# 8 Often Sometimes Severe
# 9 Often Never Mild
# 10 Never Never None
# 11 Never Never None
# 12 Never Never None
# 13 Often Never Mild
# 14 Rarely Very Often Severe
# 15 Very Often Never Mild
我有两个因素 X 和 Y,有 5 个等级(5 分李克特量表)
"[1] Never" "[2] Rarely" "[3] Sometimes" "[4] Often" "[5] Very Often"
根据这两个答案,我想用以下编码创建一个新的因子 Z:
None:X = 从不,Y = 从不
轻度:X = 很少、有时、经常或非常经常,Y = 从不
严重:Y = 很少、有时、经常或非常经常
我尝试了很多不同的多重条件,但 none 成功了。
这是其中之一:
Z <- c("None", "Mild", "Severe")
factor(Z)
levels(Z) <- c("None", "Mild", "Severe")
if (!is.na((X == 1) && (Y == 1))) {
Z == "None"
} else if (!is.na((X != 1) && (Y == 1))) {
Z == "Mild"
} else (!is.na((X != 1) && (Y != 1))) {
Z == "Severe"
}
错误信息是:
Error: unexpected '{' in:
" Z== "Mild"
} else (!is.na((X != 1) && (Y != 1))) {"
> Z == "Severe"
[1] FALSE FALSE TRUE
> }
Error: unexpected '}' in " }"
>
样本由大约 4000 人组成,我想根据他或她对问题 X 和 Y 的评分知道哪个参与者属于哪个类别(例如抑郁症)。
我还是个 R 初学者,非常感谢您的帮助!
一切顺利
这就够了吗? 您可以使用三个逻辑向量对数据进行子集化。
如果您需要更多详细信息,使用您的一些数据会有所帮助。 (help)
lev <- c("Never", "Never", "Never", "Rarely", "Sometimes", "Often", "Very Often")
set.seed(1)
X <- factor(sample(c(lev, "Never"), 15, replace=TRUE), levels=unique(lev))
Y <- factor(sample(lev, 15, replace=TRUE), levels=unique(lev))
None <- X %in% c("Never") &
Y %in% c("Never")
Mild <- X %in% c("Rarely", "Sometimes", "Often", "Very Often") &
Y %in% c("Never")
Severe <- Y %in% c("Rarely", "Sometimes", "Often", "Very Often")
如果你想将逻辑向量组合成一个单一的因子向量,可以这样做:
Z0 <- rbind(None, Mild, Severe)
z <- which(Z0, arr.ind=TRUE)
Z <- factor(rownames(z), levels=rownames(Z0))
data.frame(X, Y, Z)
# X Y Z
# 1 Never Rarely Severe
# 2 Never Often Severe
# 3 Sometimes Very Often Severe
# 4 Never Never None
# 5 Never Often Severe
# 6 Never Very Often Severe
# 7 Never Never None
# 8 Often Sometimes Severe
# 9 Often Never Mild
# 10 Never Never None
# 11 Never Never None
# 12 Never Never None
# 13 Often Never Mild
# 14 Rarely Very Often Severe
# 15 Very Often Never Mild