[R] Replacing NAs with 0 for a list of data frames

Kang Min ngokangmin at gmail.com
Mon May 31 13:32:32 CEST 2010


Thanks, it works except that I had to add xx <- as.data.frame(xx)
into func.

I am trying to calculate diversity indices using the vegan package,
and the functions require zeroes instead of NAs.

Thanks.
Kang Min

On May 31, 5:09 pm, Tal Galili <tal.gal... at gmail.com> wrote:
> I would consider trying the plyr package using the llply function.
> With something like:
>
> require(plyr)
> func <- function(xx)
> {
> xx[is.na(xx)] <- 0
> return(xx)}
>
> llply(your.df.list, func)
>
> What I wondering is why you want to do this.
>
> Best,
> Tal
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Gal... at gmail.com |  972-52-7275845
> Read me:www.talgalili.com(Hebrew) |www.biostatistics.co.il(Hebrew) |www.r-statistics.com(English)
> ----------------------------------------------------------------------------------------------
>
>
>
> On Mon, May 31, 2010 at 11:21 AM, Kang Min <ngokang... at gmail.com> wrote:
> > Hi,
>
> > I have a list of 100 data frames, each data frame has 50 obs of 377
> > variables.
>
> > I would like to replace all the NAs with 0 in all the dataframes.
> > Should I have a for loop for every data frame?
>
> > Below is an extract of how the data looks like.
>
> > List of 100
> > $ :'data.frame':        50 obs. of  377 variables:
> >  ..$ ACHRPO: int [1:50] NA NA NA NA NA NA NA NA NA NA ...
> >  ..$ ACTEEX: int [1:50] NA NA NA NA NA NA NA NA NA NA ...
> >  ..$ ACTIML: int [1:50] NA NA NA NA NA NA NA NA NA NA ...
> >  ..$ ADENMA: int [1:50] NA NA NA 2 NA NA NA NA NA NA ...
>
> > $ :'data.frame':        50 obs. of  377 variables:
> >  ..$ ACHRPO: int [1:50] NA NA NA NA NA NA NA NA NA NA ...
> >  ..$ ACTEEX: int [1:50] NA NA NA NA 2 NA NA NA NA NA ...
> >  ..$ ACTIML: int [1:50] NA NA NA NA 1 NA NA NA NA NA ...
> >  ..$ ADENMA: int [1:50] NA NA NA NA NA NA NA NA NA NA ...
>
> > Thanks.
> > Kang Min
>
> > ______________________________________________
> > R-h... 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.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list