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
Eleni, this question appears about every month on this list, try using the RSiteSearch command before posting. Thanks. RSiteSearch("replace NA") http://finzi.psych.upenn.edu/R/Rhelp02a/archive/109176.html Gabor On Wed, Nov 21, 2007 at 01:15:32PM +0200, 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 > > ______________________________________________ > R-help at r-project.org 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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
?is.na On Nov 21, 2007 12:15 PM, Eleni Christodoulou <elenichri@gmail.com> 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 > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
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! > EleniAs 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 ------------------------------
Yes, thanks a lot! It works fine! Eleni On Nov 21, 2007 2:03 PM, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:> > 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 ------------------------------ >