Hi: Here's an attempt to use merge() on your data, but I don't know if it satisfies your needs.
# dput(bs) bs <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Site1", "Site2"), class = "factor"), data = c(2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 0L, 2004L, 2005L, 2006L, 0L, 2008L, 0L, 0L), sequence = c(2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L)), .Names = c("site", "data", "sequence" ), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14")) # > dput(brep) brep <- structure(list(site = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("Site1", "Site2"), class = "factor"), class = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Reptilia", class = "factor"), classcount = c(3L, 1L, 7L, 4L, 4L), year = c(2004L, 2006L, 2005L, 2006L, 2008L)), .Names = c("site", "class", "classcount", "year"), class = "data.frame", row.names = c("6", "7", "11", "12", "13")) # Merge the two data frames together bmrg <- merge(bs, brep, by.x = c('site', 'sequence'), by.y = c('site', 'year'), all.x = TRUE) # Replace NAs in classcount with zeros bmrg$classcount[is.na(bmrg$classcount)] <- 0 bmrg If you have multiple classes of fauna, they should be represented as a factor in the larger data.frame, perhaps by use of expand.grid(). HTH, Dennis On Mon, Jan 17, 2011 at 3:54 PM, James Smith < james.sm...@australianwildlife.org> wrote: > g'day all, > > > > I need help with this please. > > I have a dataset of site names and years they were trapped in (a subset of > it is bs, below) and a dataset of sites that actually caught animals > (subsetted as brep, below). I need to add to brep, a row with a zero in the > classcount column, for every year each site was trapped but caught nothing. > > > > so for Site 1, I need to add rows that say "Site1 Reptilia 0 2005", "Site1 > Reptilia 0 2007", "Site1 Reptilia 0 2008" etc to 2010. > > > > > > years trapped (bs) > > (the data column is when they were trapped (so not in 2010 for Site1), > sequence is the range of years (2004:2010) repeated) > > > > site data sequence > > 1 Site1 2004 2004 > > 2 Site1 2005 2005 > > 3 Site1 2006 2006 > > 4 Site1 2007 2007 > > 5 Site1 2008 2008 > > 6 Site1 2009 2009 > > 7 Site1 0 2010 > > 8 Site2 2004 2004 > > 9 Site2 2005 2005 > > 10 Site2 2006 2006 > > 11 Site2 0 2007 > > 12 Site2 2008 2008 > > 13 Site2 0 2009 > > 14 Site2 0 2010 > > > > sites with captures (brep) > > > > site class classcount year > > 6 Site1 Reptilia 3 2004 > > 7 Site1 Reptilia 1 2006 > > 11 Site2 Reptilia 7 2005 > > 12 Site2 Reptilia 4 2006 > > 13 Site2 Reptilia 4 2008 > > > > > > I have got it to work, slightly (below) but the range of bs is longer than > the range of brep so it only does it twice (in this case) > > > > x<-range(bs$sequence) > > added<-c() > > for (i in 1:(x[2]-x[1]+1)) { > > > > ifelse(brep$year[i]==bs$data[i],print("blah"),added<-cbind(as.character(brep > $site[i]),as.character(brep$class[i]),0,bs$sequence[i])) > > added > > } > > > > > > any advice would be greatly appreciated > > many thanks > > > > James > > > > James Smith > > Wildlife Ecologist, Mornington Sanctuary > > Australian Wildlife Conservancy > > +61 8 9191 7014 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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-help@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.