[英]How to I use a loop to change categorical data into binary data?
I have a data set that compares improvement in student's performance to their studying method.我有一个数据集,可以将学生表现的改进与他们的学习方法进行比较。 The categorical parameter is the studying method and the three options are NO (no assistance), FULL (yes assistance), and CHECK (assistance as needed).
分类参数是学习方法,三个选项是NO(无协助)、FULL(是协助)和CHECK(根据需要协助)。 To set up my regression, I need to set x1=1 if a student had FULL assistance else x1=0.
要设置我的回归,我需要设置 x1=1,如果学生有充分的帮助,否则 x1=0。 I also need to set an x2=1 if a student was labeled as CHECK, else x2=0.
如果学生被标记为 CHECK,我还需要设置 x2=1,否则设置 x2=0。 I will also need to do the same for NO help (x3=1 if ASSIST='NO' else x3=0).
对于没有帮助,我也需要做同样的事情(如果 ASSIST='NO',则 x3=1,否则 x3=0)。 Here's what I'm trying to do:
这是我正在尝试做的事情:
if (ACCHW$ASSIST<-"FULL") { x1=1
} else {
x1=0
}
But I get the ERROR: Error in if (ACCHW$ASSIST <- "FULL") {: argument is not interpretable as logical.但我得到错误:if (ACCHW$ASSIST <- "FULL") {: 参数中的错误不能解释为逻辑。
I'm new to creating loops in R, advice is greatly appreciated!我是在 R 中创建循环的新手,非常感谢您的建议!
It would be enough to create a new data frame comparing ASSIST
to each of it's values, and cbind
that to the origiginal data frame.创建一个新的数据框就足够了,将
ASSIST
与它的每个值进行比较,并将cbind
到原始数据框。 Using the following data:使用以下数据:
ACCHW <- data.frame(ASSIST = c("NO", "FULL", "CHECK"), stringsAsFactors = F)
Try this:尝试这个:
cbind(ACCHW,
data.frame(x1 = ACCHW$ASSIST == "NO",
x2 = ACCHW$ASSIST == "FULL",
x3 = ACCHW$ASSIST == "CHECK"
)
)
#### OUTPUT ####
ASSIST x1 x2 x3
1 NO TRUE FALSE FALSE
2 FULL FALSE TRUE FALSE
3 CHECK FALSE FALSE TRUE
Remember that TRUE == 1
and FALSE == 0
.请记住
TRUE == 1
和FALSE == 0
。 If you really want 0s and 1s do the same as above, but with as.integer
:如果您真的想要 0 和 1,请执行与上述相同的操作,但使用
as.integer
:
cbind(ACCHW,
data.frame(x1 = as.integer(ACCHW$ASSIST == "NO"),
x2 = as.integer(ACCHW$ASSIST == "FULL"),
x3 = as.integer(ACCHW$ASSIST == "CHECK")
)
)
Here is a dplyr alternative!这是dplyr替代品!
library(dplyr)
df <- tibble(ASSIST = c("NO", "FULL", "CHECK"))
df %>%
mutate(x1 = ASSIST == "NO",
x2 = ASSIST == "FULL",
x3 = ASSIST == "CHECK") %>%
mutate_at(c("x1", "x2", "x3"), as.integer) # Converts to binary
Base R Solution:底座 R 解决方案:
cbind(ASSIST = ACCHW$ASSIST, data.frame(lapply(data.frame(setNames(
sapply(ACCHW$ASSIST, `==`, ACCHW$ASSIST),
c(names(ACCHW), ACCHW$ASSIST))), as.integer)))
Data thanks (@ gersht):数据感谢(@gersht):
ACCHW <- data.frame(ASSIST = c("NO", "FULL", "CHECK"), stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.