I have a dataframe named data
and columns comfort
, condition
and few others.
I do one and the same manipulations with 5 columns and decided to write the following function:
replacing_na_999<-function(df, variable){
#variable<-as.name(variable)
levels <- levels(df$variable)
levels[length(levels) + 1] <- "999"
df$variable <- factor(df$variable, levels = levels)
df$variable[is.na(df$variable)] <- "999"
}
When I try:
replacing_na_999(data, comfort)
it returns an error:
Error in `$<-.data.frame`(`*tmp*`, variable, value = integer(0)) :
replacement has 0 rows, data has 44070
Can someone help me please with syntaxis?