[R] imputing the numerical columns of a dataframe, returning the rest unchanged
Mark Heckmann
mark.heckmann at gmx.de
Mon Dec 22 16:38:54 CET 2008
Hi R-experts,
how can I apply a function to each numeric column of a data frame and return
the whole data frame with changes in numeric columns only?
In my case I want to do a median imputation of the numeric columns and
retain the other columns. My dataframe (DF) contains factors, characters and
numerics.
I tried the following but that does not work:
foo <- function(x){
if(is.numeric(x)==TRUE) return(impute(x))
else(return(x))
}
sapply(DF, foo)
day version ID V1 V2 V3
[1,] "4" "A" "1a" "1" "5" "5"
[2,] "4" "A" "2a" "2" "3" "5"
[3,] "4" "B" "3a" "3" "5" "5"
All the variables are coerced to characters now ("day" and "version" were
factors, "id" a character). I only want imputations on the numerics, but the
rest to be returned unchanged.
Is there a function available. If not, how can I do it?
TIA and merry x-mas,
Mark
More information about the R-help
mailing list