Abdul Rahman bin Kassim (Dr.)
2013-May-31 06:36 UTC
[R] Returning combine output from R loop
Dear R-User, Appreciate any advice on producing combine output from an R loop. Planning to project the growth (grow) of plant species (sp) from its initia size (initial_size). Using the follwing example, how can I produce the results combining the output from 3 iteration. The ouput a2 produce only the last iteration. Thanks in advanced for your advice. --------- a0 <- data.frame(initial_size=sample(30,10),sp=gl(2,5,10), grow=as.numeric(as.character(gl(2,5,10)))*0.5) a2<-c(0) for( i in 1:3) { a1 <- a0$initial_size+a0$grow a2<- cbind(sp=a0$sp,initial_size=a1+i,yr=i) a2} a2 --------- Abd Rahman Dr. Abd Rahman Kassim Program Kualiti Air Bahagian Perhutanan & Alam Sekitar Institut Penyelidikan Perhutanan Malaysia Kepong 52109 Selangor Fax:03-62729852 Tel: 013-3380905/03-62797179 Email: rahmank@frim.gov.my ________________________________ For E-Mail Disclaimer, please click here<http://www.frim.gov.my/v1/support/general/email_disclaimer.cfm> [[alternative HTML version deleted]]
In line with what you are doing now, but saving the intermediate results you could use something like this. a3 <- a0 for( i in 1:3) { a1 <- a0$initial_size+a0$grow a2<- cbind(sp=a0$sp,initial_size=a1+i,yr=i) a3 <- cbind(a3, a2) } a3 However, I would suggest one of the following solutions instead. Which one is best depends on what you want to do with the data after. res1 <- do.call(cbind,c(a0,lapply(1:maxYears, function(k){ data.frame(yr=k, size=a0$initial_size+k*a0$grow) }))) res2 <- do.call(cbind,c(a0,lapply(1:maxYears, function(k){ ans <- data.frame(a0$initial_size+k*a0$grow) names(ans) <- paste0("size.", k) ans }))) res3 <- do.call(rbind,lapply(1:maxYears, function(k){ cbind(id=1:nrow(a0), sp=a0$sp, yr=k, size=a0$initial_size+k*a0$grow) })) res3 <- res3[order(res3[,"id"], res3[,"yr"]),] Best, Nello -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Abdul Rahman bin Kassim (Dr.) Sent: Freitag, 31. Mai 2013 08:37 To: r-help at r-project.org Subject: [R] Returning combine output from R loop Dear R-User, Appreciate any advice on producing combine output from an R loop. Planning to project the growth (grow) of plant species (sp) from its initia size (initial_size). Using the follwing example, how can I produce the results combining the output from 3 iteration. The ouput a2 produce only the last iteration. Thanks in advanced for your advice. --------- a0 <- data.frame(initial_size=sample(30,10),sp=gl(2,5,10), grow=as.numeric(as.character(gl(2,5,10)))*0.5) a2<-c(0) for( i in 1:3) { a1 <- a0$initial_size+a0$grow a2<- cbind(sp=a0$sp,initial_size=a1+i,yr=i) a2} a2 --------- Abd Rahman Dr. Abd Rahman Kassim Program Kualiti Air Bahagian Perhutanan & Alam Sekitar Institut Penyelidikan Perhutanan Malaysia Kepong 52109 Selangor Fax:03-62729852 Tel: 013-3380905/03-62797179 Email: rahmank at frim.gov.my ________________________________ For E-Mail Disclaimer, please click here<http://www.frim.gov.my/v1/support/general/email_disclaimer.cfm> [[alternative HTML version deleted]] ______________________________________________ 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.
One way is to put them in a list. Does this help? a0 <- data.frame(initial_size=sample(30,10),sp=gl(2,5,10), grow=as.numeric(as.character(gl(2,5,10)))*0.5) a2<- list() for( i in 1:3){ a1 <- a0$initial_size+a0$grow a2[[i]] <- cbind(sp=a0$sp,initial_size=a1+i,yr=i) } John Kane Kingston ON Canada> -----Original Message----- > From: rahmank at frim.gov.my > Sent: Fri, 31 May 2013 06:36:58 +0000 > To: r-help at r-project.org > Subject: [R] Returning combine output from R loop > > Dear R-User, > > Appreciate any advice on producing combine output from an R loop. > Planning to project the growth (grow) of plant species (sp) from its > initia size (initial_size). Using the follwing example, how can I produce > the results combining the output from 3 iteration. The ouput a2 produce > only the last iteration. > > Thanks in advanced for your advice. > --------- > a0 <- data.frame(initial_size=sample(30,10),sp=gl(2,5,10), > grow=as.numeric(as.character(gl(2,5,10)))*0.5) > > a2<-c(0) > for( i in 1:3) > { > a1 <- a0$initial_size+a0$grow > a2<- cbind(sp=a0$sp,initial_size=a1+i,yr=i) > a2} > > a2 > --------- > > Abd Rahman > > > > > Dr. Abd Rahman Kassim > Program Kualiti Air > Bahagian Perhutanan & Alam Sekitar > Institut Penyelidikan Perhutanan Malaysia > Kepong 52109 > Selangor > > Fax:03-62729852 > Tel: 013-3380905/03-62797179 > Email: rahmank at frim.gov.my > > ________________________________ > For E-Mail Disclaimer, please click > here<http://www.frim.gov.my/v1/support/general/email_disclaimer.cfm> > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!