[R] fill a dataframe with zeros where the rows are a smaller subset of a larger dataframe (species by site)

Sarah Goslee sarah.goslee at gmail.com
Wed Apr 25 16:59:38 CEST 2012


The clarification helps; the original description was rather terse.

What about:

> row <- c("a","b","c","d","e","f","g") #rows from larger data frame
>
> x <- data.frame(sp1=rnorm(4), sp2=rnorm(4), sp3=rnorm(4), sp4=rnorm(4))
>
> rownames(x) <- row.1
>
> merge(x, matrix(row, ncol=1), by.x=0, by.y=1, all=TRUE)
  Row.names        sp1        sp2        sp3        sp4
1         a  0.4964272  1.4989159  0.4302415  0.9648854
2         b  1.4137142  0.9430609  0.0728391 -0.6275084
3         c -0.8103023 -1.3375148 -0.3799518  0.4523287
4         d         NA         NA         NA         NA
5         e         NA         NA         NA         NA
6         f         NA         NA         NA         NA
7         g -0.1914184  0.5156566  0.5626614  0.8068154

Sarah

On Wed, Apr 25, 2012 at 10:42 AM, Stephen Sefick <sas0025 at auburn.edu> wrote:
> I am subsetting a larger data frame that contains macroinverterate taxa.  I
> am subsetting them at different levels of taxonomic resolution.  Some of the
> sites do not have say Tipulidae present, so the rows are removed completely
> for this site.  I would like to fill in the sites that were removed during
> the subsetting and fill these with 0.  I have the non-subsetted dataframe
> which contains all of the sites.  I would like to use the sites from this
> complete data set to "expand" the dataframe that is a subset of the sites in
> the original.  Is that more clear.
>
> Stephen
>
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list