[R] set the bahavior that R deal with missing values?

Ferdinand Alimadhi falimadhi at iq.harvard.edu
Thu Jul 13 17:38:24 CEST 2006


You can deal with the missing values before performing any regression model.
if you dataset is :

 > D
  X1 X2 X3
1 NA  2  4
2  1 NA NA
3  2  2  6
4 NA 34 NA
5  3 NA  7

then the following will replace all NA with the mean value

 > f<-function(col){
+ col[is.na(col)]<-mean(col,na.rm=TRUE)
+ col}
 > sapply(D,f)
     X1       X2       X3
[1,]  2  2.00000 4.000000
[2,]  1 12.66667 5.666667
[3,]  2  2.00000 6.000000
[4,]  2 34.00000 5.666667
[5,]  3 12.66667 7.000000


Hope this is what you want.

zhijie zhang wrote:

>Dear Rusers,
>The default behavior in R when performing a regression model with missing
>values is to exclude any case that contains a
>missing value? How could i set the bahavior that R deal with missing values?
>e.g.:
>exclude cases listwise
>exclude cases pairwise
>replace with mean
>
>Thanks very much!
>
>  
>


-- 
Ferdinand Alimadhi
Programmer / Analyst
Harvard University
The Institute for Quantitative Social Science
(617) 496-0187
falimadhi at iq.harvard.edu
www.iq.harvard.edu



More information about the R-help mailing list