HI,
Your second function works:
fun2<-function(y){
if(y==2){
print("yes")}
else {print("no")}}
fun2(3)
#[1] "no"
fun2(4)
#[1] "no"
fun2(2)
#[1] "yes"
But, in the first case, it is a vector.
So, you can use:
fun3<-function(y){
numbers1<-c(4,5,9,11,17)
if(y %in% numbers1){
print("yes")}
HI,
Try ifelse
fun1<-function(y){
ifelse(y==c(4,5,9,11,17),"yes","no")
}
> fun1(9)
#[1] "no" "no" "yes" "no" "no"
A.K.
- Original Message -
From: Dominic Roye
To: r-help@r-project.org
Cc:
Sent: Sunday, August 12, 2012 6:12 AM
Subject: [R] Error in if-command
Hello everybody,
Take a look at ?ifelse
Cheers,
Michael
On Aug 12, 2012, at 3:12 AM, Dominic Roye wrote:
> Hello everybody,
>
> I don't understand what I'm doing wrong. But it isn't possible that each
> element of the if-condition is tested for each vector element?
>
> y <- c(1:20)
>> y
> [1] 1 2 3 4 5
3 matches
Mail list logo