I want to replace the NA with mean of each single group collembola
and mite
in multiple columns. Here it is an example with 3 columns however I want to apply this a data frame with 5000 columns
dat <- read.table(text =
"id ID length width extra
101 collembola 2.1 0.9 1
102 mite NA 0.7 NA
103 mite 1.1 0.8 2
104 collembola 1 NA 3
105 collembola 1.5 0.5 4
106 mite NA NA NA
106 mite 1.9 NA 4",
header=TRUE)
It works if I enter each column
library(plyr)
impute.mean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))
data2 <- ddply(dat, ~ ID, transform, length = impute.mean(length))
I want to apply the function that calculates the mean of each single group ID
collembola
and mite
across multiple columns, below is what I tried (it does not work):
dat2 <- ddply(dat, ~ ID, transform, impute.mean(dat[,3:ncol(dat)]))