> a<-c(1,4) > a[1] 1 4> b<-a*5 > b[1] 5 20 a is a very long vector , how can i get c(1:5,4:20)? i do not want to use a loop. thanks very much!
"a" looks pretty short to me, just two elements. You should spend some (more) time reading the "Introduction to R" document that is supplied with the software... particularly the parts on indexing. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "???" <hongwangli at sjtu.edu.cn> wrote:>> a<-c(1,4) >> a >[1] 1 4 >> b<-a*5 >> b >[1] 5 20 > >a is a very long vector , how can i get c(1:5,4:20)? i do not want to >use a loop. > >thanks very much! > >______________________________________________ >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.
On Mon, Jun 18, 2012 at 01:24:21AM +0800, ??? wrote:> > a<-c(1,4) > > a > [1] 1 4 > > b<-a*5 > > b > [1] 5 20 > > a is a very long vector , how can i get c(1:5,4:20)? i do not want to > use a loop.Hi. How large are the numbers in a? If max(a) is not too large, try the following. a <- c(1, 4, 3, 1) ma <- max(a) b <- matrix(a, nrow=4*ma+1, ncol=length(a), byrow=TRUE) + matrix(0:(4*ma), nrow=4*ma+1, ncol=length(a)) out <- c(b[b <= rep(5*a, each=4*ma+1)]) out [1] 1 2 3 4 5 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 3 4 5 [26] 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 Hope this helps. Petr Savicky.
On 17-Jun-2012 17:24:21 ?????? wrote:>> a<-c(1,4) >> a > [1] 1 4 >> b<-a*5 >> b > [1] 5 20 > > a is a very long vector , how can i get c(1:5,4:20)? > i do not want to use a loop. > > thanks very much!Your question is not quite clear, but I think you mean: a is a very long vector, and you want a vector with elements a[1],a[2],a[3],a[4],a[5],a[4],a[5],a[6],...,a[20] If that is the case, then a[c(1:5,4:20)] will do it. Example: a <- (1:100) a[c(1:5,4:20)] # [1] 1 2 3 4 5 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Does that help? If not, try to re-formulate the question! Ted. ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 17-Jun-2012 Time: 20:58:01 This message was sent by XFMail