Sample of the data I am working with below. I already omitted all rows with a "NA" in them.
我正在处理的数据样本如下。我已经省略了所有包含“NA”的行。
gss.s[1:5,]
abany advfront arrest
2 YES Agree NO
3 YES Strongly agree YES
10 YES Agree YES
16 YES Agree NO
21 NO Strongly agree NO
I am trying to replace the "YES" with a 1 in the abany column and "NO" with -1 in abany column. I tried using replace replace(gss.s[,1], "YES", 1) but got the error Invalid Factor Level, NA generated. I read a previous post about this same problem and couldn't figure out how to apply the solution to my problem.
我正在尝试用abany列中的1替换“是”,用abany列中的-1替换“否”。我尝试使用替换(gss)。s[,1],“是”,1)但得到错误无效因子水平,NA生成。我之前读过一篇关于这个问题的文章,但是我不知道如何将这个解决方案应用到我的问题上。
3 个解决方案
#1
2
You could use ifelse()
on the column, changing all values at once.
您可以在列上使用ifelse(),同时更改所有值。
gss.s$abany <- ifelse(gss.s$abany == "YES", 1, -1)
gss.s
## abany advfront arrest
## 2 1 Agree NO
## 3 1 Strongly agree YES
## 10 1 Agree YES
## 16 1 Agree NO
## 21 -1 Strongly agree NO
#2
2
y <- data.frame ("abany"=c("YES","YES","YES","YES","NO"),
"advfront"=c("Agree","Strongly Agree","Agree","Agree","Strongly Agree"),
"arrest"=c("NO","YES","YES","NO","NO"))
I would try another way: Convert factor column to character
我可以尝试另一种方法:将因子列转换为字符
y$abany <- as.character.factor (y$abany)
So now you don't have any problem to change values of the column
所以现在你没有任何问题来改变列的值。
y[y$abany=="YES", "abany"] <- 1
y[y$abany=="NO", "abany"] <- -1
#3
0
You can also create a vector in which you define the changes you would like to make. It is a lot quicker if you have big datasets compared to ifelse
statements.
您还可以创建一个向量,在其中定义您想要进行的更改。与ifelse语句相比,如果您有大型数据集,那么速度会快得多。
translate <- c(YES=1,NO=-1)
gss.s$abany2 <- translate[as.character(gss.s$abany)]
gss.s
# abany advfront arrest abany2
# 1 YES Agree NO 1
# 2 YES Strongly agree YES 1
# 3 YES Agree YES 1
# 4 YES Agree NO 1
# 5 NO Strongly agree NO -1
You also don't have to struggle with nested ifelse
functions if you have multiple changes to make:
如果要做多个更改,您也不必为嵌套的ifelse函数而烦恼:
translate2 <- c('Strongly agree'=2,Agree=1,Disagree=-1,'Strongly disagree'=-2 )
gss.s$advfront2 <- translate2[as.character(gss.s$advfront)]
gss.s
# abany advfront arrest abany2 advfront2
# 1 YES Agree NO 1 1
# 2 YES Strongly agree YES 1 2
# 3 YES Agree YES 1 1
# 4 YES Agree NO 1 1
# 5 NO Strongly agree NO -1 2
#1
2
You could use ifelse()
on the column, changing all values at once.
您可以在列上使用ifelse(),同时更改所有值。
gss.s$abany <- ifelse(gss.s$abany == "YES", 1, -1)
gss.s
## abany advfront arrest
## 2 1 Agree NO
## 3 1 Strongly agree YES
## 10 1 Agree YES
## 16 1 Agree NO
## 21 -1 Strongly agree NO
#2
2
y <- data.frame ("abany"=c("YES","YES","YES","YES","NO"),
"advfront"=c("Agree","Strongly Agree","Agree","Agree","Strongly Agree"),
"arrest"=c("NO","YES","YES","NO","NO"))
I would try another way: Convert factor column to character
我可以尝试另一种方法:将因子列转换为字符
y$abany <- as.character.factor (y$abany)
So now you don't have any problem to change values of the column
所以现在你没有任何问题来改变列的值。
y[y$abany=="YES", "abany"] <- 1
y[y$abany=="NO", "abany"] <- -1
#3
0
You can also create a vector in which you define the changes you would like to make. It is a lot quicker if you have big datasets compared to ifelse
statements.
您还可以创建一个向量,在其中定义您想要进行的更改。与ifelse语句相比,如果您有大型数据集,那么速度会快得多。
translate <- c(YES=1,NO=-1)
gss.s$abany2 <- translate[as.character(gss.s$abany)]
gss.s
# abany advfront arrest abany2
# 1 YES Agree NO 1
# 2 YES Strongly agree YES 1
# 3 YES Agree YES 1
# 4 YES Agree NO 1
# 5 NO Strongly agree NO -1
You also don't have to struggle with nested ifelse
functions if you have multiple changes to make:
如果要做多个更改,您也不必为嵌套的ifelse函数而烦恼:
translate2 <- c('Strongly agree'=2,Agree=1,Disagree=-1,'Strongly disagree'=-2 )
gss.s$advfront2 <- translate2[as.character(gss.s$advfront)]
gss.s
# abany advfront arrest abany2 advfront2
# 1 YES Agree NO 1 1
# 2 YES Strongly agree YES 1 2
# 3 YES Agree YES 1 1
# 4 YES Agree NO 1 1
# 5 NO Strongly agree NO -1 2