[R] NA values

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Nov 21 13:03:37 CET 2007


On 21-Nov-07 11:15:32, Eleni Christodoulou wrote:
> Hi all!
> I am new to R and I would like to ask you the following question:
> How can I substitute the NA values with 0 in a data frame?
> I cannot find a command to check if a value is NA...
> 
> Thank you very much!
> Eleni

As has been said, is.na() is the function which determines
whether something has "value" NA (result=TRUE) or not (result=FALSE).

is.na() will work nicely with dataframes (also, of course, with
structures such as vectors, matrices and arrays). Example:

  dummy<-data.frame(X1=c(101,102,103,104,NA,106),
                    X2=c(201,202,203,NA,205,206))

  dummy
#   X1  X2
#1 101 201
#2 102 202
#3 103 203
#4 104  NA
#5  NA 205
#6 106 206

  dummy[is.na(dummy)] <- 0

  dummy
#   X1  X2
#1 101 201
#2 102 202
#3 103 203
#4 104   0
#5   0 205
#6 106 206

Hoping this makes it clear!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Nov-07                                       Time: 12:03:33
------------------------------ XFMail ------------------------------



More information about the R-help mailing list