[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 15:08:48 CEST 2012


row <- c("a","b","c","d","e","f","g") #rows from larger data frame

row.1 <- c("a","b","c","g") #rows of smaller data frame because d, e, 
and f don't contain any of the species, but the zeros are important

x <- data.frame(sp1=rnorm(4), sp2=rnorm(4), sp3=rnorm(4), sp4=rnorm(4))

rownames(x) <- row.1

#I would like to make z as if I had y, but I only have the rownames of y

y <- data.frame(sp1=c(0,0,0), sp2=c(0,0,0), sp3=c(0,0,0), sp4=c(0,0,0))

rownames(y) <- c("d", "e", "f")

z <- rbind(x,y)

z <- z[order(row.names(z)),]

#I know I am missing something
#many thanks,

-- 
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