[R] missing values

Charilaos Skiadas cskiadas at gmail.com
Sat Nov 24 07:03:16 CET 2007


Here's my take on it, don't know if you cared at all about optimizing  
the first couple of lines:

data <- data.frame(x=rep(c("A","B"),5), y=c(NA,NA,rnorm(8)))
means <- with(data,ave(y, as.character(x), FUN=function(x) mean(x,  
na.rm=TRUE)))
data$y <- ifelse(is.na(data$y),means,data$y)

I tend to not use intermediate variables when they are only used once  
in the sequel (though they do often enhance readability). Also, I  
like using "with" when I tend to use a data frame more than once  
(perhaps an overkill in this case). It is a pity that "ave" doesn't  
pass its arguments to the FUN, so, the "na.rm" could be added as just  
another argument (though I see why it can't easily be done; It would  
be nice to be able to do that though, like the apply family works).

The ifelse call at the end is perhaps an overkill, probably the  
assignment that Jim proposes is better in that case.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

On Nov 23, 2007, at 3:37 PM, jim holtman wrote:

> you could use this instead of the last two statements; don't know if
> it makes any simpler since it is just combining into one statement
> what you had in two:
>
> data$y[is.na(data$y)] <- means[is.na(data$y)]
>
>
> On Nov 23, 2007 1:49 PM, lamack lamack <lamac_k at hotmail.com> wrote:
>>
>>
>>
>> Dear all, there is a best way to do the following task?
>>
>> x               = rep(c("A","B"),5)
>> y               =  rnorm(10)
>> data           = data.frame(x,y)
>> data$y[1:2]   = c(NA,NA)
>> means         = ave(data$y,as.character(data$x),FUN=function(x)mean 
>> (x,na.rm=T))
>> aux           = which(is.na(data$y))
>> data[aux,"y"] = means[aux]
>> _________________________________________________________________
>> Encontre o que procura com mais eficiência! Instale já a Barra de  
>> Ferra[[replacing trailing spam]]
>>
>>        [[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.
>>
>>
>
>
>
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>



More information about the R-help mailing list