[R] adding zeros to dataframe

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri May 1 19:40:01 CEST 2009


On 01-May-09 17:20:08, Collins, Cathy wrote:
> Greetings,
> I am new to R and am hoping to get some tips from experienced
> R-programmers.
>  
> I have a dataset that I've read into R as a dataframe. There are 5
> columns: Plot location,species name, a species number code (unique
> to each species name), abundance, and treatment. There are 272 plots in
> each treatment, but only the plots in which the species was recorded
> have an abundance value.  For all species in the dataset, I would like
> to add zeros to the abundance column for any plots in which the species
> was not recorded, so that each species has 272 rows.  The data are
> sorted by species and then abundance, so all of the zeros can
> presumably just be tacked on to the last (272-occupied plots) row for
> each species.
>  
> My programming skills are still somewhat rudimentary (and biased toward
> VBA-style looping...which seems to be leading me astray). Though I have
> searched, I have not yet seen this particular problem addressed in the
> help files.
>  
> Many thanks for any suggestions,
> Cathy
> <mailto:ccollins at ku.edu>

Suppose we call your dataframe "abun.df". Then its columns will be
something like abun.df$location, abun.df$name, abun.df$abundance,
abun.df$trtmt (depending on what you called them in the first place).

>From your description, I am presuming that abundence values where
a species was not recorded have no value entered. In that case,
presumably they have gone into abun.df$abundance as "NA". You can
check this with a command like

  sum(is.na(abun.df$abundance))

If you get a positive result, then that is likely to be the case.
As a cross-check:

  sum(abun.df$abundance > 0, na.rm=TRUE)

should give another number which, together with the first, should
add up to the total number of rows in the dataframe.

Assuming, then, that this is the case, the simplest method to set
the non-recorded values to 0 is on the lines of

  ix <- (is.na(abun.df$abundance))
  abun.df$abundance[ix] <- 0

Then you can run the check

  sum(abun.df$abundance == 0)

and you should get a number which is the same as you got from

  sum(is.na(abun.df$abundance))

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-May-09                                       Time: 18:39:58
------------------------------ XFMail ------------------------------




More information about the R-help mailing list