jeudi 4 juin 2015

R : if-else error

I am having trouble with the creation of a variable using the following if-else statement :

# create data #

df<-as.data.frame(c(10:14))
colnames(df)<-"a"
df$m1<-c(1:5)
df$m2<-c(2,1,4,3,5)


# if-else statement #

if (df$m1 < df$m2) {
       df$a2 <- df$a - 1
} else if (df$m1 >= df$m2) {
       df$a2 <- df$a
} else {
       df$a2 <- 99
}

This produces the following error :

Warning message:
In if (df$m1 < df$m2) { :
  the condition has length > 1 and only the first element will be used

And so effectively for all values :

df$a2 <- df$a - 1

It seems that my curly brackets are in the right place so I do not understand what is happening.

Aucun commentaire:

Enregistrer un commentaire