【发布时间】:2017-06-30 06:22:17
【问题描述】:
我做了一些搜索,但找不到与我的问题完全一样的东西。我之前已经多次完成几乎完全相同的循环,除了它通常更像是“只要你有 Y(某个其他因素)就使 X(一个因素)”。
这是我的代码:
for(i in 1:nrow(age_cont)){
if(age_cont$Choice[i] == 1){
age_cont$Rank[i] = "Never"
}
else if(age_cont$Choice[i] == 2){
age_cont$Rank[i] = "Sometimes"
}
else if(age_cont[i] == 3){
age_cont$Rank[i] = "Frequently"
}
else {
age_cont$Rank[i] = "Always"
}
}
但我收到此错误:
> Error in `[.data.frame`(age_cont, i) : undefined columns selected
In addition: Warning messages:
1: In if (age_cont[i] == 3) { :
the condition has length > 1 and only the first element will be used
2: In if (age_cont[i] == 4) { :
the condition has length > 1 and only the first element will be used
3: In if (age_cont[i] == 3) { :
the condition has length > 1 and only the first element will be used
4: In if (age_cont[i] == 4) { :
the condition has length > 1 and only the first element will be used
>
Choice 变量是序数 (1-4),我正在尝试创建一个与之平行的新列,但使用名称而不是数字。我尝试将 Choice 变量更改为一个因子并重新运行循环,甚至尝试简单地将每个数字重命名为一个名称(在同一列中),但这也不起作用。
有什么想法吗?
【问题讨论】: