[R] problem creating an array

Bert Gunter gunter.berton at gene.com
Fri Sep 14 07:36:12 CEST 2012


Well, Rui -- I'll bet that that gobSmacked him (and probably others)!
So here's just a tiny extra effort at enlightenment.

The keys are understanding that:

1. Matrices are (formally) just 2-d arrays.
2. All arrays are just vectors, indexed in "column major" order --
i.e. rows vary the fastest, then columns, then layers, then ...  And
this is documented (tersely) in ?array as

"The values in data are taken to be those in the array with the
leftmost subscript moving fastest."

If you parse this out carefully, you'll see why Rui's magic works.

-- Bert

On Thu, Sep 13, 2012 at 7:00 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> Hello,
>
> You don't need the loop, it can be done with one much simpler instruction.
> Since the number of rows is not important, the example uses a 10x603 matrix.
>
> x <- matrix(seq_len(10*603), ncol = 603)
> y <- array(x, dim = c(10, 3, 201))  # that's it
>
> # See the first two
> y[, , 1:2]
>
> Hope this helps,
>
> Rui Barradas
> Em 13-09-2012 23:04, Curtis Burkhalter escreveu:
>>
>> Hello,
>>
>> I am having trouble creating a (1000,3,201) array in R from a data set
>> created within the same script.  My problem is that I want to take a
>> matrix
>> that is 1000x603
>> (called "landmat") and separate this into 201 separate (1000x3) matrices.
>>   My problem is when I try to do particular combinations of the columns.
>> I
>> want to take the first 3 columns of "landmat" and create the first 1000x3
>> matrix, then I want to take columns 4,5,6 of "landmat" and create the
>> second matrix, then continue this process until all 603 columns have been
>> used resulting in the 201 unique 1000x3 matrices.  Any help would be
>> appreciated and my code is pasted below:
>>
>> #========================================================================
>> #landscape changes
>> #========================================================================
>> ##generate 3 sequences, each of length n=21; sequences named theta"n"_set1
>> theta1_set1=c(seq(0,1,0.005))
>> seqa=c(seq(0,0.50,0.005))
>> seqb=c(seq(0.495,0,-0.005))
>> theta2_set1=c(seqa,seqb)
>> seqc=c(seq(1,0,-0.01))
>> seqd=c(rep(0,100))
>> theta3_set1=c(seqc,seqd)
>> sum=numeric()
>> for (i in 1:201)
>> { sum[i]= theta1_set1[i]+theta2_set1[i]+theta3_set1[i]
>> }
>> sum
>> #"n" designates the number of random #'s to be generated from rnorm
>> #"x"combines all three strings of landscape scenarios into one vector
>> #"sd" tells the std dev. used when generating #'s from rnorm
>> #"p"indicates that x is 63 observation in length
>> #mat" is a vector of randomly generated numbers from the normal dist'bn
>> #using n, the first value of x and sd
>> #designate a "for loop" that loops over the remaining observations in x
>> #and inserts them as the mean of the normal distribution from which the
>> #random numbers are generated
>> n=1000
>> x=c(theta1_set1,theta2_set1,theta3_set1)
>> p = length(x)
>>
>> mat <- rep(x[1],n)
>> for (i in 2:p) {
>>
>>          mat <- cbind(mat,rep(x[i],n))
>> }
>> #sort the matrix columns so that 201 unique landscape combinations are
>> #created; each set of 3 columns should sum to one
>> landmat=cbind(mat[,c(1)],mat[,c(202)],mat[,c(403)])
>>
>> for (i in 2:201)
>> {
>> landmat=cbind(landmat,(cbind(mat[,c(i)],mat[,c(i+201)],mat[,c(i+402)])))
>> }
>> ##this loop is a check to ensure the triplets all sum to 1
>> #sum=numeric()
>> #n=1
>> #landmat_vec=landmat[1,]
>> #for (i in 1:201)
>> #{
>> #sum[i]=landmat_vec[n]+landmat_vec[n+1]+landmat_vec[n+2]
>> #n=n+3
>> #}
>>
>>
>> #create vector of column names in "hab" for each matrix and attach using
>> #"colnames(matrix)"
>> hab<-c("theta1","theta2","theta3")
>> colnames(landmat)=rep(hab,201)
>>
>> *********The following for loop is not working******
>> ## separate the columns of landmat(dim:1000x603)
>> # into 201 separate 1000x3 matrices
>>
>> dims=c(1000,3,201)
>> n=1
>> landscape=array(dim=dims)
>> for (i in 1:201)
>> {
>> landscape[i]=landmat[ ,c(n,n+1,n+2)]
>> n=n+3
>> }
>>
>>
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm




More information about the R-help mailing list