Dear R users, I have a quick quesiton. Here is a vector "a". a<- c(1,2,3,4,5,6,7,8). (In fact, I have a huge vector.) With "a", I'd like to create new vectors, for example, new1 = (1+2, 3, 4+5+6, 7+8) new2 = (1, 2+3+4+5+6+7, 8) new3 = (1+2+3+4+5+6+7, 8) How could I make the above vectors using R? Any suggestion will be greatly appreciated. Best, Kathryn Lord [[alternative HTML version deleted]]
Hi Kathryn, I think this might do the trick: make_group_sums<-function(x,maxgroups) { lenx<-length(x) runlengths<-sample(1:lenx,1) for(i in 2:(maxgroups-1)) { lenx<-lenx-runlengths[i-1] runlengths[i]<-ifelse(lenx,sample(1:lenx,1),0) } runlengths[maxgroups]<-length(x)-sum(runlengths) groups<-rep(1:maxgroups,runlengths) groupsums<-by(x,groups,sum) return(as.vector(groupsums)) } Jim On Fri, Jan 23, 2015 at 7:45 PM, Kathryn Lord <kathryn.lord2000 at gmail.com> wrote:> Dear R users, > > I have a quick quesiton. > > Here is a vector "a". > > a<- c(1,2,3,4,5,6,7,8). > > (In fact, I have a huge vector.) > > > With "a", I'd like to create new vectors, for example, > > new1 = (1+2, 3, 4+5+6, 7+8) > new2 = (1, 2+3+4+5+6+7, 8) > new3 = (1+2+3+4+5+6+7, 8) > > > How could I make the above vectors using R? > > > Any suggestion will be greatly appreciated. > > Best, > > Kathryn Lord > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
If Jim's answer is not what you want, then I would say it is because your question is too vague to be answered. In particular, how do you specify the elements of the vector that are to be summed to create the new vectors? ?tapply might then be relevant here, but that's just a guess. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Fri, Jan 23, 2015 at 12:45 AM, Kathryn Lord <kathryn.lord2000 at gmail.com> wrote:> Dear R users, > > I have a quick quesiton. > > Here is a vector "a". > > a<- c(1,2,3,4,5,6,7,8). > > (In fact, I have a huge vector.) > > > With "a", I'd like to create new vectors, for example, > > new1 = (1+2, 3, 4+5+6, 7+8) > new2 = (1, 2+3+4+5+6+7, 8) > new3 = (1+2+3+4+5+6+7, 8) > > > How could I make the above vectors using R? > > > Any suggestion will be greatly appreciated. > > Best, > > Kathryn Lord > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hi Kathryn, another solution would be to use tapply function. So the code to create new1 vector would be: a <- 1:8 fc <- c('g1','g1','g2','g3','g3','g3','g4','g4') # definitions of groups to sum over tapply(a,fc,sum) ivan On Fri, Jan 23, 2015 at 10:32 AM, Bert Gunter <gunter.berton at gene.com> wrote:> If Jim's answer is not what you want, then I would say it is because > your question is too vague to be answered. In particular, how do you > specify the elements of the vector that are to be summed to create the > new vectors? ?tapply might then be relevant here, but that's just a > guess. > > -- Bert > > > > > > Bert Gunter > Genentech Nonclinical Biostatistics > (650) 467-7374 > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > Clifford Stoll > > > > > On Fri, Jan 23, 2015 at 12:45 AM, Kathryn Lord > <kathryn.lord2000 at gmail.com> wrote: >> Dear R users, >> >> I have a quick quesiton. >> >> Here is a vector "a". >> >> a<- c(1,2,3,4,5,6,7,8). >> >> (In fact, I have a huge vector.) >> >> >> With "a", I'd like to create new vectors, for example, >> >> new1 = (1+2, 3, 4+5+6, 7+8) >> new2 = (1, 2+3+4+5+6+7, 8) >> new3 = (1+2+3+4+5+6+7, 8) >> >> >> How could I make the above vectors using R? >> >> >> Any suggestion will be greatly appreciated. >> >> Best, >> >> Kathryn Lord >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Here is some examples using functions 'tapply()' as suggested by Bert Gunter in the previous post, 'aggregate()', and 'xtabs()'. Note that 'grp.id' means 'group indicator'. > a <- c(1,2,3,4,5,6,7,8) > new1 <- c(1+2, 3, 4+5+6, 7+8) > new1 [1] 3 3 15 15 > > grp.id <- c(1,1, 2, 3,3,3, 4,4) > tapply(X=a, INDEX=grp.id, FUN=sum) 1 2 3 4 3 3 15 15 > aggregate(x=a, by=list(grp.id), FUN=sum) Group.1 x 1 1 3 2 2 3 3 3 15 4 4 15 > xtabs(formula=a~grp.id) grp.id 1 2 3 4 3 3 15 15 > > I hope this helps. Chel Hee Lee On 1/23/2015 3:32 AM, Bert Gunter wrote:> If Jim's answer is not what you want, then I would say it is because > your question is too vague to be answered. In particular, how do you > specify the elements of the vector that are to be summed to create the > new vectors? ?tapply might then be relevant here, but that's just a > guess. > > -- Bert > > > > > > Bert Gunter > Genentech Nonclinical Biostatistics > (650) 467-7374 > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > Clifford Stoll > > > > > On Fri, Jan 23, 2015 at 12:45 AM, Kathryn Lord > <kathryn.lord2000 at gmail.com> wrote: >> Dear R users, >> >> I have a quick quesiton. >> >> Here is a vector "a". >> >> a<- c(1,2,3,4,5,6,7,8). >> >> (In fact, I have a huge vector.) >> >> >> With "a", I'd like to create new vectors, for example, >> >> new1 = (1+2, 3, 4+5+6, 7+8) >> new2 = (1, 2+3+4+5+6+7, 8) >> new3 = (1+2+3+4+5+6+7, 8) >> >> >> How could I make the above vectors using R? >> >> >> Any suggestion will be greatly appreciated. >> >> Best, >> >> Kathryn Lord >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >