我看过类似的帖子,但没有任何工作可做。
我有一个字符1,2,3,4,5的列,它是面试问题的答案 我想要一个新列,当响应为1或2时,新列为No,当响应为3时,新列为Partly,当响应为4或5为Yes时,所有其他都为NA。
data.frame': 405 obs. of 1 variables:
$ SQ023A : chr "-3" "-3" "-3" "-3" ...(this has -1, -2, -3, -4, 1, 2, 3, 4, 5, Yes, No, Partly)
新列应该保留Yes,No,Partly答案,但是将No和4和5替换为1和2,使用Partly替换3和3。其他一切都是NA。
我尝试了以下内容但没有成功
sq23$test <- ifelse(("1"|"2", sq23$SQ23A), "No",
ifelse("4"|"5", sq23$SQ23A), "Yes",
ifelse("3", sq23$SQ23A), "Partly","NA"))
答案 0 :(得分:2)
尝试:
sq23$test <- c("No","No","Partly","Yes","Yes")[as.numeric(sq23$SQ23A)]
编辑:
根据您的编辑,我将为这类问题提供更通用的解决方案。首先,我们构建一个包含我们要替换的旧值的向量。然后,我们定义另一个有替换的向量。然后我们通过match
函数来完成这个技巧。例如:
#create a sample of your data
sq23<-data.frame(SQ023A=sample(c(-4:5,"Yes","No","Partly"),size=405,replace=TRUE))
#define the old values to replace
oldValues<-c(1:5,"Yes","No","Partly")
#define the replacement (each value of newValues replace the corresponding of oldValues)
newValues<-c("No","No","Partly","Yes","Yes","Yes","No","Partly")
#create the test column
sq23$test<-newValues[match(sq23$SQ023A,oldValues)]
答案 1 :(得分:2)
为清楚起见,我会这样做:
sq23$test<- NA
sq23$test[sq23$SQ23A == 1 | sq23$SQ23A == 2]<- "No"
sq23$test[sq23$SQ23A == 4 | sq23$SQ23A == 5]<- "Yes"
sq23$test[sq23$SQ23A == 3]<- "Partly"
根据您的编辑,对于更一般的情况,您还可以使用字典类型解决方案:
values<- c("no", "no", "partly","yes","yes","yes","no","partly") # new value
names(values)<- c(1:5, "yes", "no", "partly") # keys
> values
1 2 3 4 5 yes no partly
"no" "no" "partly" "yes" "yes" "yes" "no" "partly"
sq23$test<- values[as.character(sq23$SQ23A)]
# as.character() used to make sure that the keys/old values are passed as
# characters, and not e.g. a factor