Re: [R] zero fill empty cell in data.frame

From: Petr PIKAL <petr.pikal_at_precheza.cz>
Date: Tue, 19 Apr 2011 12:51:02 +0200

Hi

r-help-bounces_at_r-project.org napsal dne 19.04.2011 12:02:31:

> On 04/19/2011 07:39 PM, Simone Gabbriellini wrote:
> > Hello List,
> >
> > I have a data frame like:
> >
> > V130 V131 V132 V133 V134 V135 V136
> > 1 0 0 0.9 0 0.9 0 0
> > 2 0 0 0 0 0 0.8
> > 3 0 0 0 0 0.9 0 0
> > 4 0.9 0 0 0 0 0 0.9
> > 5 0 0 0
> > 6 0 0 0 0.9 0 0 0.9
> > 7 0 0 0.8 0 0 0 0
> > 8 0.9 0 0 0.9 0.8 0
> > 9 0 0 0 0.9 0.9 0 0
> > 10 0 0 0 0 0 0 0.9
> >
> > I would like to fill the empty cells with a 0... how to address those
empty cells?
> >
> Hi Simone,
> I'm somewhat surprised that the empty cells aren't displayed as "NA". If

> they are in fact NAs, you could use:
>
> mydataframe[is.na(mydataframe)]<-0

Many functions in R has straightforward way how to handle missing values. Therefore be careful with changing them to any value until you really need it for some reason.

x<-rnorm(10)
x[5]<-NA
sum(x)
[1] NA
sum(x, na.rm=TRUE)
[1] 3.264033
mean(x, na.rm=TRUE)
[1] 0.3626703

       ^^^
x[is.na(x)]<-0
mean(x)
[1] 0.3264033

       ^^^^

Regards
Petr
Regards
Petr

>
> Jim
>
> ______________________________________________
> 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.



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. Received on Tue 19 Apr 2011 - 11:00:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 19 Apr 2011 - 11:10:32 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.

list of date sections of archive