I have the following dataframe:
ID Value
1 0
2 100
3 200
4 0
我想编写一个函数,如果“Value”列中的值等于0,则生成“Hi”,否则生成“Bye”。结果应为:
Answer
Hi
Bye
Bye
Hi
这是我尝试的代码:
Type <- function(df){
if(df$Value == 0)
"Hi"
else
"Bye"
}
apply(X = df, MARGIN = 1, FUN = Type)
然而,我遇到了这个错误:
Error in Test_Customer$Pipe : $ operator is invalid for atomic vectors
有人能帮我修复这个错误吗?谢谢!
ifelse
。if
函数不适用于向量。 - IRTFM