我在数据框中有一个变量,其中一个字段通常有7-8个值.我想在数据框中的一个新变量中将它们拼写为3或4个新类别.什么是最好的方法?
如果我在类似SQL的工具中但是不确定如何在R中攻击它,我将使用CASE语句.
您将提供的任何帮助将不胜感激!
我理解编写"if - else if"语句的常用方法如下:
if (2==1) {
print("1")
} else if (2==2) {
print("2")
} else {
print("3")
}
Run Code Online (Sandbox Code Playgroud)
要么
if (2==1) {print("1")
} else if (2==2) {print("2")
} else print("3")
Run Code Online (Sandbox Code Playgroud)
相反,如果我这样写
if (2==1) {
print("1")
}
else if (2==2) {
print("2")
}
else (print("3"))
Run Code Online (Sandbox Code Playgroud)
或者这样:
if (2==1) print("1")
else if (2==2) print("2")
else print("3")
Run Code Online (Sandbox Code Playgroud)
声明不起作用.你能解释一下为什么}必须先行else或else if同一行吗?是否还有其他方法在R中编写if-else if-else语句,尤其是没有括号?