R-help, In the following loop : for(i in 1:8) { cc[[i]]<-tapply(test[,i+6],list(puntar=test$puntar),sum)/tapply(test[,5],list(puntar=test$puntar),sum) cbind.data.frame(cc[[1]],cc[[2]],cc[[3]],cc[[4]], cc[[5]],c[[6]],cc[[7]],cc[[8]]) } Is there anyway I can 'cbind.data.frame' the objects cc[[ i ]] without actually writing every single element?? In this case there are 8 but what if it is a large number?? Thank you
Take a look at "do.call". In your case, 'do.call("cbind", cc)' should work. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Luis Rideau Cruz Sent: Friday, August 27, 2004 9:12 AM To: r-help at stat.math.ethz.ch Subject: [R] for (i in ...) { df[[i]]<- .....} R-help, In the following loop : for(i in 1:8) { cc[[i]]<-tapply(test[,i+6],list(puntar=test$puntar),sum)/tapply(test[,5],lis t(puntar=test$puntar),sum) cbind.data.frame(cc[[1]],cc[[2]],cc[[3]],cc[[4]], cc[[5]],c[[6]],cc[[7]],cc[[8]]) } Is there anyway I can 'cbind.data.frame' the objects cc[[ i ]] without actually writing every single element?? In this case there are 8 but what if it is a large number?? Thank you ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
?do.call do.call(cc,"cbind") Hope it helps Eryk *********** REPLY SEPARATOR *********** On 8/27/2004 at 2:11 PM Luis Rideau Cruz wrote:>>>R-help, >>> >>>In the following loop : >>> >>>for(i in 1:8) >>>{ >>>cc[[i]]<-tapply(test[,i+6],list(puntar=test$puntar),sum)/tapply(test[,5],list(puntar=test$puntar),sum) >>> >>>cbind.data.frame(cc[[1]],cc[[2]],cc[[3]],cc[[4]], >>>cc[[5]],c[[6]],cc[[7]],cc[[8]]) >>> >>>} >>> >>>Is there anyway I can 'cbind.data.frame' the objects cc[[ i ]] without >>>actually writing every single element?? >>>In this case there are 8 but what if it is a large number?? >>> >>> >>>Thank you >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
PLEASE don't call methods explicitly. I think do.call("cbind", cc) is what you are looking for, although probably you should be calling data.frame not cbind here. (Your lines are too long and contain no spaces so I am not going to try to fathom out what cc[[i]] really is. But I am sure you do not intend the cbind to be inside the loop.) On Fri, 27 Aug 2004, Luis Rideau Cruz wrote:> In the following loop : > > for(i in 1:8) > { > cc[[i]]<-tapply(test[,i+6],list(puntar=test$puntar),sum)/tapply(test[,5],list(puntar=test$puntar),sum) > > cbind.data.frame(cc[[1]],cc[[2]],cc[[3]],cc[[4]], > cc[[5]],c[[6]],cc[[7]],cc[[8]]) > > } > > Is there anyway I can 'cbind.data.frame' the objects cc[[ i ]] without > actually writing every single element?? > In this case there are 8 but what if it is a large number??-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
"Luis Rideau Cruz" <Luisr at frs.fo> writes:> R-help, > > In the following loop : > > for(i in 1:8) > { > cc[[i]]<-tapply(test[,i+6],list(puntar=test$puntar),sum)/tapply(test[,5],list(puntar=test$puntar),sum) > > cbind.data.frame(cc[[1]],cc[[2]],cc[[3]],cc[[4]], > cc[[5]],c[[6]],cc[[7]],cc[[8]]) > > } > > Is there anyway I can 'cbind.data.frame' the objects cc[[ i ]] without > actually writing every single element?? > In this case there are 8 but what if it is a large number??Whatever did as.data.frame(cc) do to you to deserve getting overlooked that badly? (and I suspect that there's a much neater solution along the lines of dd <- lapply(test[5:14],tapply,test$puntar,sum) dd <- as.data.frame(lapply(dd,as.vector)) dd <- dd[-1]/dd[[1]] ) As for the generic question, have a look at do.call(). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907