An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121002/b621ebb0/attachment.pl>
Problem with mutli-dimensional array
6 messages · Berend Hasselman, Rui Barradas, Loukia Spineli
On 02-10-2012, at 16:20, Loukia Spineli <spineliloukia26 at gmail.com> wrote:
I want to make a multi-dimensional array. To be specific I want to make the
following array
results<-array(0,dim=c(2,2,64,7))
This is the code I have created but it gives no result due to the error
"subscript out of bound".
x<-rep(7,7) # Missingness in intervention
y<-rep(7,7) # Missingness in control
arraynames<-list(Group=c("Success","Failure"),Outcome=c("Intervention","Control"),Trial=c("1":"7"))
mat.stat<-array(c(9,16,10,15,66,12,44,23,102,88,66,104,277,60,247,119,23,43,20,41,201,162,122,
263,14,41,4,41),dim=c(2,2,7),dimnames=arraynames);mat.stat
Nx<-rep(0,length(x))
Ny<-rep(0,length(y))
n<-(x+1)*(y+1)
results<-array(0,dim=c(2,2,64,7))
l<-1
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
results[,,l,i]<-mat.stat[,,i]+matrix(c(c(0:x[i])[j],c(0:y[i])[k],-c(0:x[i])[j],
-c(0:y[i])[k]),nrow=2,ncol=2,byrow=T)
l<-l+1
}
}
}
results
If you had inserted this line
cat("i=",i,"l=",l,"j=",j,"k=",k,"\n")
before the assignment to results[,,l,i] in the inner loop you could have quite easily seen what the problem is.
The counter l (letter el) keeps on increasing in your code and when it reaches 65 R will complain.
So at an appropriate place in the code you need to reset l to 1.
I suggest you move the assignment l<-1 to just before the line with for( j in 1:Nx[i]) ...
Whether the results are what you desire is up to you to decide.
Please use some more whitespace and do some indenting in your code. As presented it is unreadable.
Berend
Hello,
See if this is it.
Nx <- rep(0,length(x))
Ny <- rep(0,length(y))
n <- (x+1)*(y+1)
results <- array(0, dim=c(2,2,64,7))
# l <- 1 # <--------------------------- This changed place
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
l <- 1 # <----------------------- To here
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
Hope this helps,
Rui Barradas
Em 02-10-2012 15:20, Loukia Spineli escreveu:
I want to make a multi-dimensional array. To be specific I want to make the
following array
results<-array(0,dim=c(2,2,64,7))
This is the code I have created but it gives no result due to the error
"subscript out of bound".
x<-rep(7,7) # Missingness in intervention
y<-rep(7,7) # Missingness in control
arraynames<-list(Group=c("Success","Failure"),Outcome=c("Intervention","Control"),Trial=c("1":"7"))
mat.stat<-array(c(9,16,10,15,66,12,44,23,102,88,66,104,277,60,247,119,23,43,20,41,201,162,122,
263,14,41,4,41),dim=c(2,2,7),dimnames=arraynames);mat.stat
Nx<-rep(0,length(x))
Ny<-rep(0,length(y))
n<-(x+1)*(y+1)
results<-array(0,dim=c(2,2,64,7))
l<-1
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
results[,,l,i]<-mat.stat[,,i]+matrix(c(c(0:x[i])[j],c(0:y[i])[k],-c(0:x[i])[j],
-c(0:y[i])[k]),nrow=2,ncol=2,byrow=T)
l<-l+1
}
}
}
results
Any suggestion would be really welcome!
Thank you very much!
Loukia
[[alternative HTML version deleted]]
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121003/2e6e4053/attachment.pl>
Hello,
I'm glad it helped.
Now you have what is a data structure problem. The computations are the
same but in a different output.
First of all a terminology issue. In R the correct names for the data
structures you've refered to are 'list', 'matrix' and 'array'. To make
it short,
vector ---> 1-dim
matrix ---> 2-dim
array ---> n-dim
and
list ---> no dim attribute, can hold any type of data structure.
Your first sentence of this last post would read (changes in uppercase)
Actually I am trying to make an LIST of length 7, where each LIST ELEMENT
IS AN ARRAY containing a different number of 2 by 2 matrices.
In what follows, for the 3-dim array case I'll use the naming convention
[row, column, slice].
Each of your output list elements is an array with a possibly varying
number of slices. Right now all of them have 64 slices.
1. You need a list of 7 elements, each of them indexed by your current 'i';
2. Each of those elements is a 3-dim array, each slice is a 2x2 matrix
and each slice is indexed by your current 'l'.
3. Let's try it.
Nx <- rep(0, length(x))
Ny <- rep(0, length(y))
n <- (x+1)*(y+1)
results <- vector("list", 7)
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
results[[i]] <- array(dim = c(2, 2, n[i]))
l <- 1
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
tmp <- c((0:x[i])[j], (0:y[i])[k],-(0:x[i])[j], -(0:y[i])[k])
results[[i]][,, l] <- mat.stat[,,i] + matrix(tmp, nrow=2,
ncol=2,byrow=T)
l <- l + 1
}
}
}
results
I think this is it. Let us now if not.
Rui Barradas
Em 03-10-2012 08:48, Loukia Spineli escreveu:
Thank you very much for your suggestions. They both worked! Actually I am trying to make an array of length 7, where each array contains a different number of 2 by 2 matrices. The vector "n" defines the different number of matrices across the arrays. Therefore, the parameter "l" has to change in every array i. Of course I cannot put the vector "n" directly into the dimensions of the array (under the name "results"). First, I wanted to see if the code gives rational results assuming that each array has the same number of 2 by 2 matrices. Fortunately, the results are rational (thanks to your help, as well). Now I am taking the problem to the next level and I want to construct this array of different number of 2 by 2 matrices! Any suggestions would be really very helpful! On Tue, Oct 2, 2012 at 6:23 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
Hello,
See if this is it.
Nx <- rep(0,length(x))
Ny <- rep(0,length(y))
n <- (x+1)*(y+1)
results <- array(0, dim=c(2,2,64,7))
# l <- 1 # <--------------------------- This changed place
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
l <- 1 # <----------------------- To here
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
Hope this helps,
Rui Barradas
Em 02-10-2012 15:20, Loukia Spineli escreveu:
I want to make a multi-dimensional array. To be specific I want to make
the
following array
results<-array(0,dim=c(2,2,64,**7))
This is the code I have created but it gives no result due to the error
"subscript out of bound".
x<-rep(7,7) # Missingness in intervention
y<-rep(7,7) # Missingness in control
arraynames<-list(Group=c("**Success","Failure"),Outcome=c(**
"Intervention","Control"),**Trial=c("1":"7"))
mat.stat<-array(c(9,16,10,15,**66,12,44,23,102,88,66,104,277,**
60,247,119,23,43,20,41,201,**162,122,
263,14,41,4,41),dim=c(2,2,7),**dimnames=arraynames);mat.stat
Nx<-rep(0,length(x))
Ny<-rep(0,length(y))
n<-(x+1)*(y+1)
results<-array(0,dim=c(2,2,64,**7))
l<-1
for(i in 1:length(x)){
Nx[i] <- length(1:(x[i]+1))
Ny[i] <- length(1:(y[i]+1))
for(j in 1:(Nx[i])){
for(k in 1:(Ny[i])){
results[,,l,i]<-mat.stat[,,i]+**matrix(c(c(0:x[i])[j],c(0:y[i]**
)[k],-c(0:x[i])[j],
-c(0:y[i])[k]),nrow=2,ncol=2,**byrow=T)
l<-l+1
}
}
}
results
Any suggestion would be really welcome!
Thank you very much!
Loukia
[[alternative HTML version deleted]]
______________________________**________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html <http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121004/d453fd93/attachment.pl>