I wrote the following function to add "-1" as a level to factors of my dataframe, and afterwards set NA's to "-1":
fun <- function(df) {
add_na_level <- function(x){
if(is.factor(x) & !"-1" %in% levels(x)) return(factor(x, levels=c(levels(x), "-1")))
x[is.na(x)]<-"-1"
return(x)
}
df<-sapply(df,add_na_level)
return(df)
}
, but when I use it on my dataframe, it runs really really slow. Is it something with the sapply line?
df<-sapply(df,add_na_level)