mercredi 28 novembre 2018

How can i apply a function with an ifelse stricture

I make a new function:

  newFunction <- function (varX) {

  lst_a <- c("aa", "bb", "cc")
  lst_b <- c("dd", "ee", "ff")
  lst_c <- c("gg", "hh", "ii")

  ifelse(varX %in% lst_a , x.out <- "List A" , x.out <- "Other List" )
  ifelse(varX %in% lst_b , x.out <- "List B" , x.out )
  ifelse(varX %in%  lst_c , x.out <- "List C" , x.out)

  return(x.out)

 }

When I test the function it works well. But when I apply it to a dataframe it does not work:

     df <- as.data.frame(c("aa","cc","kk", "nn"))
    df$new <- newFunction((df[,1]))

I expected to get "List A", "List A", "Other list", "Other List" - but no such luck have I, and left with me is "Other list", "Other List", "Other list", "Other List"

Aucun commentaire:

Enregistrer un commentaire