Instead of creating a lot of variables, work with a list. Since you
did not supply any test data, I will make a guess at the code would be
like:
result <- lapply(split(newdataset, newdataset$target_species),
function(.spec){
tappy(.spec$total_hook_num, .spec$year, sum)
})
You can summarize from this list to the species easily by something like this:
sapply(result, sum)
but I would have to see the data to be sure.
On Mon, Oct 12, 2009 at 4:26 PM, jimdare <jamesdare26 at gmail.com>
wrote:>
> Dear R users,
>
> The code below splits a dataset by "target_species" and then
further splits
> each target species group by year. ?Finally the sum of a variable called
> "total_hook_num" is calculated and then assigned a name based on
> (paste(a$target_species[1],a$year[1],sep="").
>
> newsplit<-split(newdataset,list(newdataset$target_species))
> for(i in newsplit){
>
> by.year<-split(i,list(i$year))
> for(a in by.year){
>
assign(paste(a$target_species[1],a$year[1],sep=""),sum(a$total_hook_num))
> }
> }
>
> The end result is a single value for the number of hooks in a particular
> year for a particular species (e.g. YFN2005<-2320) for every
species/year.
> This results in number of individual objects being created, as you can see
> from ls(). ?I need to bind objects that have the same
"target_speices" name,
> in order of year e.g. ALBtimeseries<-cbind(ALB1989,
ALB2990,.....,ALB2009).
>
> My questions are: is there a way to do this by working cbind into the loop;
> and if not, is there a way to bind objects that have a common name e.g. all
> ALB****?
>
> Thank you very much for your time, it's much appreciated.
> James
>
>> ls()
> ?[1] "a" ? ? ? ? ?"ALB1989" ? ?"ALB1990" ?
?"ALB1991" ? ?"ALB1992"
> ?[6] "ALB1993" ? ?"ALB1994" ? ?"ALB1995" ?
?"ALB1996" ? ?"ALB1997"
> ?[11] "ALB1998" ? ?"ALB1999" ? ?"ALB2000" ?
?"ALB2001" ? ?"ALB2002"
> ?[16] "ALB2003" ? ?"ALB2004" ? ?"ALB2005" ?
?"ALB2006" ? ?"ALB2007"
> ?[21] "ALB2008" ? ?"ALB2009" ? ?"BIG1990" ?
?"BIG1991" ? ?"BIG1992"
> ?[26] "BIG1993" ? ?"BIG1994" ? ?"BIG1995" ?
?"BIG1996" ? ?"BIG1997"
> ?[31] "BIG1998" ? ?"BIG1999" ? ?"BIG2000" ?
?"BIG2001" ? ?"BIG2002"
> ?[36] "BIG2003" ? ?"BIG2004" ? ?"BIG2005" ?
?"BIG2006" ? ?"BIG2007"
> ?[41] "BIG2008" ? ?"BIG2009" ? ?"BTU1992" ?
?"BTU2001" ? ?"BTU2004"
> ?[46] "BWS2000" ? ?"by.year" ? ?"data1" ? ?
?"data2" ? ? ?"datacomb"
> ?[51] "i" ? ? ? ? ?"logic" ? ? ?"logic2" ? ?
"MAK1992" ? ?"MOO1992"
> ?[56] "MOO2000" ? ?"MOO2002" ? ?"newdataset"
"newsplit" ? "NTU1994"
> ?[61] "NTU1995" ? ?"NTU1996" ? ?"NTU1997" ?
?"NTU1998" ? ?"NTU1999"
> ?[66] "NTU2000" ? ?"NTU2001" ? ?"NTU2002" ?
?"NTU2003" ? ?"NTU2004"
> ?[71] "NTU2005" ? ?"NTU2007" ? ?"NTU2008" ?
?"NTU2009" ? ?"SKJ1990"
> ?[76] "SKJ1996" ? ?"SKJ1998" ? ?"SKJ2003" ?
?"STM1992" ? ?"STM1995"
> ?[81] "STM1996" ? ?"STM1999" ? ?"STM2000" ?
?"STM2007" ? ?"STM2008"
> ?[86] "STN1990" ? ?"STN1991" ? ?"STN1992" ?
?"STN1993" ? ?"STN1994"
> ?[91] "STN1995" ? ?"STN1996" ? ?"STN1997" ?
?"STN1998" ? ?"STN1999"
> ?[96] "STN2000" ? ?"STN2001" ? ?"STN2002" ?
?"STN2003" ? ?"STN2004"
> [101] "STN2005" ? ?"STN2006" ? ?"STN2007" ?
?"STN2008" ? ?"STN2009"
> [106] "STU1994" ? ?"STU1996" ? ?"STU2000" ?
?"STU2002" ? ?"STU2004"
> [111] "swordfish" ?"TOR2001" ? ?"TOR2002" ?
?"TOR2003" ? ?"TOR2004"
> [116] "TOR2005" ? ?"TOR2006" ? ?"TOR2007" ?
?"TOR2008" ? ?"TOR2009"
> [121] "TUN1997" ? ?"TUN1998" ? ?"TUN1999" ?
?"TUN2001" ? ?"YFN1990"
> [126] "YFN1991" ? ?"YFN1992" ? ?"YFN1993" ?
?"YFN1994" ? ?"YFN1995"
> [131] "YFN1996" ? ?"YFN1997" ? ?"YFN1998" ?
?"YFN1999" ? ?"YFN2000"
> [136] "YFN2001" ? ?"YFN2002" ? ?"YFN2003" ?
?"YFN2004" ? ?"YFN2005"
> [141] "YFN2006" ? ?"YFN2007" ? ?"YFN2008"
>
>
>
>
> --
> View this message in context:
http://www.nabble.com/Binding-objects-with-a-similar-name-tp25862663p25862663.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?