[R] replacing all NA's in a dataframe with zeros...
Jason Barnhart
jasoncbarnhart at msn.com
Thu Mar 15 01:41:31 CET 2007
This should work.
> test.df <- data.frame(x1=c(NA,2,3,NA), x2=c(1,2,3,4),
> x3=c(1,NA,NA,4))
> test.df
x1 x2 x3
1 NA 1 1
2 2 2 NA
3 3 3 NA
4 NA 4 4
> test.df[is.na(test.df)] <- 1000
> test.df
x1 x2 x3
1 1000 1 1
2 2 2 1000
3 3 3 1000
4 1000 4 4
The following search string "cran r replace data.frame NA" in Google
(as US user) yielded some good results (5th and 7th entry), but there
was another example that explicitly yielded this technique. I can't
seem to recall my exact search string.
----- Original Message -----
From: "David L. Van Brunt, Ph.D." <dlvanbrunt at gmail.com>
To: "R-Help List" <r-help at stat.math.ethz.ch>
Sent: Wednesday, March 14, 2007 5:22 PM
Subject: [R] replacing all NA's in a dataframe with zeros...
> I've seen how to replace the NA's in a single column with a data
> frame
>
> *> mydata$ncigs[is.na(mydata$ncigs)]<-0
>
> *But this is just one column... I have thousands of columns (!) that
> I need
> to do this, and I can't figure out a way, outside of the dreaded
> loop, do
> replace all NA's in an entire data frame (all vars) without naming
> each var
> separately. Yikes.
>
> I'm racking my brain on this, seems like I must be staring at the
> obvious,
> but it eludes me. Searches have come up CLOSE, but not quite what I
> need..
>
> Any pointers?
>
> --
> ---------------------------------------
> David L. Van Brunt, Ph.D.
> mailto:dlvanbrunt at gmail.com
>
> "If Tyranny and Oppression come to this land, it will be in the
> guise of
> fighting a foreign enemy."
> --James Madison
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list