Dear Ruser's
I want to substitute each "NA" by the group mean of which the
"NA" is
belonging to. For example, substitute the first record of traits "NA"
by the
mean of "BSPy01-10" in the dummy dataframe.
I have ever tried to solve this problem by using doBy package. But, I
failed. I ask for the advice on how to use "lapplyBy" function of
"doBy"
package.
The commands used and the output I got are as followed:
library(doBy)
df<-orderBy(~group,data=df) # succeeded
f1<-function(x){m<-mean(x, na.ram=TRUE); x[is.na(x)]<-m; x} # succeeded
datatraits<-lapplyBy(traits~group,data=df, FUN=f1(traits)) # failed
errors: mean(x, na.ram = TRUE), can not find 'traits'.
dummy dataframe:> df
group traits
1 BSPy01-10 NA
2 BSPy01-10 7.3
3 BSPy01-10 7.3
4 BSPy01-11 5.3
5 BSPy01-11 5.4
6 BSPy01-11 5.6
7 BSPy01-11 NA
8 BSPy01-11 NA
9 BSPy01-11 4.8
10 BSPy01-12 8.1
11 BSPy01-12 6.0
12 BSPy01-12 6.0
13 BSPy01-13 6.1
Thanks in advance.
Sincerely,
Mao J-F
[[alternative HTML version deleted]]
Gabor Grothendieck
2009-Jun-09 02:34 UTC
[R] how to use "lapplyBy" function of "doBy" package
Here are four ways:
# using lapplyBy - DF is already sorted by group
library(doBy)
f <- function(x) {
x$traits[is.na(x$traits)] <- mean(x$traits, na.rm = TRUE)
x
}
do.call(rbind, lapplyBy(~ group, DF, f))
# using by - same f as before
do.call(rbind, by(DF, DF$group, f))
# using ave - nondestructive
g <- function(x) {
x[is.na(x)] <- mean(x, na.rm = TRUE)
x
}
transform(DF, traits = ave(DF$traits, DF$group, FUN = g))
# using ave - same but modifies DF - same g as before
DF$traits <- ave(DF$traits, DF$group, FUN = mean.na)
On Mon, Jun 8, 2009 at 10:01 PM, Mao Jianfeng<jianfeng.mao at gmail.com>
wrote:> Dear Ruser's
>
> I want to substitute each "NA" by the group mean of which the
"NA" is
> belonging to. For example, substitute the first record of traits
"NA" by the
> mean of "BSPy01-10" in the dummy dataframe.
>
> I have ever tried to solve this problem by using doBy package. But, I
> failed. I ask for the advice on how to use "lapplyBy" function of
"doBy"
> package.
>
> The commands used and the output I got are as followed:
>
> library(doBy)
> df<-orderBy(~group,data=df) ? # succeeded
> f1<-function(x){m<-mean(x, na.ram=TRUE); x[is.na(x)]<-m; x} #
succeeded
> datatraits<-lapplyBy(traits~group,data=df, FUN=f1(traits)) # failed
> errors: mean(x, na.ram = TRUE), can not find 'traits'.
>
> dummy dataframe:
>> df
> ? ? ? group traits
> 1 ?BSPy01-10 ? ? NA
> 2 ?BSPy01-10 ? ?7.3
> 3 ?BSPy01-10 ? ?7.3
> 4 ?BSPy01-11 ? ?5.3
> 5 ?BSPy01-11 ? ?5.4
> 6 ?BSPy01-11 ? ?5.6
> 7 ?BSPy01-11 ? ? NA
> 8 ?BSPy01-11 ? ? NA
> 9 ?BSPy01-11 ? ?4.8
> 10 BSPy01-12 ? ?8.1
> 11 BSPy01-12 ? ?6.0
> 12 BSPy01-12 ? ?6.0
> 13 BSPy01-13 ? ?6.1
>
> Thanks in advance.
>
> Sincerely,
>
> Mao J-F
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>