Re: [R] how to ignore "NA" or replace it by another value

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Thu 21 Sep 2006 - 19:17:05 GMT

On Thu, 2006-09-21 at 19:58 +0200, Thomas Preuth wrote:
> Hello,
>
> I`m a newbie to R so maybe this question is boring, but I have a large
> table with several empty missing values, which come out as "NA". How can
> i ignore them or replace them by another number?
>
> Greetings, Thomas
>
> ______________________________________________
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.

Ignoring NAs depends on what functions you want to ignore them in.

As for replacing NAs with another number, this will replace all NAs with 0

# some example data
dat <- as.data.frame(matrix(rnorm(100), nrow = 10)) # add some NAs
dat[sample(1:10, 3), sample(1:10, 3)] <- NA dat
# replace missing values with 0
dat <- sapply(dat, function(x) {x[is.na(x)] <- 0; x}) dat

HTH G

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

______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.
Received on Fri Sep 22 05:32:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 21 Sep 2006 - 20:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.