Re: [R] how to substitute missing values (NAs) by the group means

2009-06-08 Thread hadley wickham
On Mon, Jun 8, 2009 at 8:56 PM, Mao Jianfeng wrote: > Dear Ruser's > > I ask for helps on how to substitute missing values (NAs) by mean of the > group it is belonging to. > > my dummy dataframe is: > >> df >       group traits > 1  BSPy01-10     NA > 2  BSPy01-10    7.3 > 3  BSPy01-10    7.3 > 4  

Re: [R] how to substitute missing values (NAs) by the group means

2009-06-08 Thread Jorge Ivan Velez
Dear Mao, Here is another way: yourdata$traits2 <- with(yourdata, do.call(c, tapply(traits, group, function(y){ ym <- mean(y,na.rm=TRUE) y[is.na(y)]<- ym

Re: [R] how to substitute missing values (NAs) by the group means

2009-06-08 Thread David Winsemius
On Jun 8, 2009, at 9:56 PM, Mao Jianfeng wrote: Dear Ruser's I ask for helps on how to substitute missing values (NAs) by mean of the group it is belonging to. my dummy dataframe is: df group traits 1 BSPy01-10 NA 2 BSPy01-107.3 3 BSPy01-107.3 4 BSPy01-115.3 5

Re: [R] how to substitute missing values (NAs) by the group means

2009-06-08 Thread Henrique Dallazuanna
Try this: d$traits[is.na(d$traits)] <- ave(d$traits, d$group, FUN=function(x)mean(x, na.rm = T))[is.na(d$traits)] On 6/8/09, Mao Jianfeng wrote: > Dear Ruser's > > I ask for helps on how to substitute missing

[R] how to substitute missing values (NAs) by the group means

2009-06-08 Thread Mao Jianfeng
Dear Ruser's I ask for helps on how to substitute missing values (NAs) by mean of the group it is belonging to. my dummy dataframe is: > df group traits 1 BSPy01-10 NA 2 BSPy01-107.3 3 BSPy01-107.3 4 BSPy01-115.3 5 BSPy01-115.4 6 BSPy01-115.6 7 BSPy01-11