lundi 9 mars 2020

R if returns list in data frame column need simple character

Problem context: list returned need chr (character). Created function, fun_grade(), to conditionally select numeric value (as character) to return and update inside data frame. A list was returned in the data frame df_credit_status$grade field. Perhaps I needed to cast to as.character, or is there a better function method to change the character column?

list returned need simple chr

fun_grade <- function(grade) {
  if (grade == "A") return("100")
  if (grade == "B") return("80")
  if (grade == "C") return("60")
  if (grade == "D") return("40")
  if (grade == "D") return("20")
  if (grade == "F") return("10")
  return
}
df_credit_status$grade <- sapply(df_credit_status$grade, FUN=fun_grade)

Aucun commentaire:

Enregistrer un commentaire