简体   繁体   English

如何使用循环将分类数据更改为二进制数据?

[英]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 == 1FALSE == 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.

相关问题 R:如何可视化随时间变化的二进制/分类数据 - R: How to visualize change in binary/categorical data over time 如何将多列中的二进制数据“合并”到具有分类数据的新列中? - How can I "merge" binary data from multiple columns into a new column with categorical data? 如何在 ggplot2 中使用带有分类数据的 stat() 命令? - How do I use the stat()-command with categorical data in ggplot2? 如何使用不同的颜色来表示 R 中的几列分类二进制数据? - How to use different color to represent several column of categorical binary data in R? 如何使用样本权重和分类数据进行可视化? - How to use sample weights with categorical data for visualization? 如何在R中使用二进制响应和两个分类变量分析数据 - How to analyze data with a binary response and two categorical variables in R 将二进制“多重响应”数据重组为分类 - Restructure binary "multiple response" data to categorical 在 R Studio 中将分类数据转换为二进制 (1&0) - Convert Categorical Data into binary (1&0) in R Studio 分类/二进制数据的和的直方图 - Histogram of Sums from Categorical/Binary Data 如何根据数据的整体顺序更改特定的分类变量 - How to change a specific categorical variable based on overall sequence of the data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM