[R] Imputing missing values

Mahbub Latif ahmlatif at yahoo.com
Wed Sep 1 11:47:18 CEST 2004


Try this:

newPrice = unlist(sapply(Price, Crop:Season,
  function(x){
    x[is.na(x)]=mean(x,na.rm=T);
    return(x);
  }))  



--- Jan Smit <janpsmit at yahoo.co.uk> wrote:

> Dear all, 
> 
> Apologies for this beginner's question. I have a
> variable Price, which is associated with factors
> Season and Crop, each of which have several levels.
> The Price variable contains missing values (NA),
> which
> I want to substitute by the mean of the remaining
> (non-NA) Price values of the same Season-Crop
> combination of levels. 
> 
> Price     Crop    Season 
> 10        Rice    Summer 
> 12        Rice    Summer 
> NA        Rice    Summer 
> 8         Rice    Winter 
> 9         Wheat    Summer 
> 
> Price[is.na(Price)] gives me the missing values, and
> by(Price, list(Crop, Season), mean, na.rm = T) the
> values I want to impute. What I've not been able to
> figure out, by looking at by and the various
> incarnations of apply, is how to do the actual
> substitution. 
> 
> Any help would be much appreciated. 
> 
> Jan Smit
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list