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

Stephen Sefick sas0025 at auburn.edu
Wed Apr 25 19:40:26 CEST 2012


Thank you all very much, that did the trick.

Stephen

On Wed 25 Apr 2012 09:59:38 AM CDT, Sarah Goslee wrote:
> 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
>>

--
Stephen Sefick
**************************************************
Auburn University
Biological Sciences
331 Funchess Hall
Auburn, Alabama
36849
**************************************************
sas0025 at auburn.edu
http://www.auburn.edu/~sas0025
**************************************************

Let's not spend our time and resources thinking about things that are 
so little or so large that all they really do for us is puff us up and 
make us feel like gods.  We are mammals, and have not exhausted the 
annoying little problems of being mammals.

                                -K. Mullis

"A big computer, a complex algorithm and a long time does not equal 
science."

                              -Robert Gentleman



More information about the R-help mailing list