R: Assign a value inside an application function

I am trying to remove the missing imputation using the apply functions. I am currently using the following method:

 data <- sapply(data,function(x) 
         replace(x,list=is.na(x) & class(x) == "numeric", values=mean(x,na.rm=T)))

But this is not very convenient, because I need to create a return value to make it work. Is there a way to assign a value inside the apply function?

An ideal way would be:

 sapply(data,function(x) if(class(x) == "numeric") x[is.na(x)] <- =mean(x,na.rm=T)))
+4
source share

All Articles