mercredi 29 août 2018

Function with IF ELSE doesn't work

I have a simple function:

new_function <- function(x)
 {
letters <- c("A","B","C")
new_letters<- c("D","E","F")

 if (x %in% letters) {"Correct"}
 else if (x %in% new_letters) {"Also Correct"}
 else   {x} 
 }

I make a dataframe with letters:

df <- as.data.frame(LETTERS[seq( from = 1, to = 10 )])
names(df)<- c("Letters")

I want to apply the function on the dataframe:

  df$result <- new_function(df$Letters)

And it doesent work (the function only writes "Correct")

i get this warning:

Warning message: In if (x %in% letters) { : the condition has length 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire