Re: [R] for loop

From: Petr Savicky <>
Date: Fri, 06 May 2011 10:24:56 +0200

On Fri, May 06, 2011 at 02:28:57PM +1000, andre bedon wrote:
> Hi,
> I'm hoping someone can offer some advice:I have a matrix "x" of dimensions 160 by 10000. I need to create a matrix "y", where the first 7 elements are equal to x[1]^1/7, then the next 6 equal to x[2]^1/6, next seven x[3]^1/7 and so on all the way to the 10400000th element. I have implemented this with a for loop an hour ago and it is still loading, can anyone offer any suggestions as to how I can create this matrix without using loops? I would really appreciate any suggestions.


Since indexing x[1], x[2], ... is used and also the description of y corresponds more to a vector, let me first suggest a solution for vectors.

  x <- rep(42, times=4) # any vector of even length   x <- x/c(7, 6)
  rep(x, times=rep(c(7, 6), length=length(x)))   [1] 6 6 6 6 6 6 6 7 7 7 7 7 7 6 6 6 6 6 6 6 7 7 7 7 7 7

The input vector may be obtained using c() from a matrix. The output vector may be reformatted using matrix(). However, for a matrix solution, a more precise description of the question is needed.

Hope this helps.

Petr Savicky. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 06 May 2011 - 08:28:25 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 06 May 2011 - 14:20:05 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive