vendredi 20 août 2021

R: Function() - How to check x y values with if else and sapply

This is just an example of what I wanna do.. i'm trying to check if "Numbers2" is greater than "Numbers" .. with the code below.. am i doing it wrongly? it's a small problem but i'm new to R.. Im not getting the output I want and I cannot find the problem. output with current code

df = data.frame(YesNo = c("yes","no","yes","no"), 
            Numbers = c(4, 5, 10, 10),
            Numbers2 = c(5,1,11,11))
df

rownames(df) = c("Test1","Test2","Test3","Test4")
df


improvement1 = function(x,y) {
  if(x >= y){
    return("Improve")
  } else {
    return("No")
  }
}

df$Improvement=sapply(df$Numbers, df$Numbers2, FUN=improvement1)
View(df)

Aucun commentaire:

Enregistrer un commentaire