[R] NA values

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jun 12 15:24:58 CEST 2006


On Mon, 2006-06-12 at 18:18 +0530, Arun Kumar Saha wrote:
> Dear all R users,
> 
> I am wondering whether there is any way to replace all "NA" values in a data
> frame by some numerical value, suppose 1000?
> 
> Thanks and Regards

Hi, Arun,

sapply(dat, function(x) {x[is.na(x)] <- 1000; x})

e.g.:

## some dummy data
dat <- as.data.frame(matrix(rnorm(100), ncol = 10))
## add some NAs
samp <- sample(1:10, 4)
dat[samp, -samp] <- NA
dat
## replace NAs with 1000
dat <- sapply(dat, function(x) {x[is.na(x)] <- 1000; x})
dat

HTH

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
*  Note new Address, Telephone & Fax numbers from 6th April 2006  *
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson
ECRC & ENSIS                  [t] +44 (0)20 7679 0522
UCL Department of Geography   [f] +44 (0)20 7679 0565
Pearson Building              [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street                  [w] http://www.ucl.ac.uk/~ucfagls/cv/
London, UK.                   [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list