[R] Imputing missing values

Manoj - Hachibushu Capital Wanzare at HCJP.com
Wed Sep 1 11:01:36 CEST 2004

How about the following code below?

Price[is.na(price)] = mean(Price[-which(is.na(price))]);



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jan Smit
Sent: Wednesday, September 01, 2004 5:44 PM
To: R-help at stat.math.ethz.ch
Subject: [R] Imputing missing values

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

Any help would be much appreciated. 

Jan Smit

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list