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? thanks for your help! best, Simone
you could try something along these lines:
DF <- data.frame(V1 = c(0, 0.9, " ", 0.8, 0.1, 0),
V2 = c(0.9, " ", 0.8, 0.7, " ", 1))
DF
sapply(DF, function (x)
as.numeric(gsub("(^ +)|( +$)", "0", x)))
I hope it helps.
Best,
Dimitris
On 4/19/2011 11:39 AM, 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?
>
> thanks for your help!
>
> best,
> Simone
> ______________________________________________
> 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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
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 Jim
On 19/04/11 21:39, 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? > > thanks for your help!In general, filling empty cells with zeros is ***very bad practice***. Zero is not the same thing as ``missing''. So basically: Don't do this! I don't care how badly you want to do it, or who told you to do it, just ***don't***. cheers, Rolf Turner