[R] for loop
Petr Savicky
savicky at praha1.ff.cuni.cz
Fri May 6 10:24:56 CEST 2011
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.
Hi.
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.
More information about the R-help
mailing list