我做了一些搜索,无法找到与我的问题完全相同的内容。我之前已经多次完成了几乎完全相同的循环,除了它通常更像是"每当你有Y(某些其他因素)&#34时,制作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变量更改为一个因子并重新运行循环,甚至只是尝试将每个数字重命名为一个名称(在同一列中),但这也不起作用。
有什么想法吗?
答案 0 :(得分:1)
似乎行中有错误
else if(age_cont[i] == 3){
我认为你的意思是
else if(age_cont$Choice[i] == 3){
这应该解释错误消息。
答案 1 :(得分:1)
这是一个答案,允许您通过与提供排名的表进行连接来删除for循环和几个for item,count in C.items():
print( [item]*count )
。我做左连接然后填充na以模仿else子句。
if
答案 2 :(得分:0)
到处使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/ddlSsid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="50dp" />
<include layout="@layout/information" />
<include layout="@layout/network" />
<include layout="@layout/ping_ip_setting" />
<include layout="@layout/scan_information" />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
而不是
age_cont$Choice[i] %in% #your_number
另外,将age_cont$Choice[i] == #your_number
更改为age_cont[i] == 3
这可以解决您的问题。