[R] Dimnames of array inside loop

David Winsemius dwinsemius at comcast.net
Wed Jun 23 23:44:28 CEST 2010


On Jun 23, 2010, at 11:17 AM, M.Ribeiro wrote:

>
> any clue??
>
> hello R-helpers,
>
> I have an array
>
> acuracia <- array(NA, dim = c(1, 1, A, B, C))
>
> which is first defined and in the example above with dimensions  
> 1x1xAxBxC.
> My array is then filled using 3 loops (I am not well familiar yet with
> lapply or sapply functions so I am still a loop-user):
>
> for (i in 1:A){
> for (j in 1:B){
> for (k in 1:C){
> acuracia[,,i,j,k] <- correlacao / (sqrt(ac))
> }
> }
> }
>

I'm not sure what structure correlacao has, but wonder if you couldn't  
have done:

acuracia[,,1:i,1:j,1:k] <- correlacao / (sqrt(ac))  # no loop needed?


> then I want to put names using dimnames but filling the names each  
> at a time
> after each iteration:
>
> My first idea was to try
>
> acuracia <- array(NA, dim = c(1, 1, A,B,C),dimnames = list("acc",NULL,
> paste("rep.", i, sep = " "), paste("variable: ", j), paste("mark.val  
> = ",
> k))

It is possible, but seems unwise to use " " as your separator in  
dimnames or variable names or to include colons in dimnames, since you  
generally would interpret those as having special syntactic  
significance. Try instead:

acuracia <- array(NA, dim = c(1, 1, A,B,C), dimnames = list("acc",NULL,
paste("rep.", 1:i, sep = ""), paste("variable.", 1:j),  
paste("mark.val.", 1:k, sep=""))

You may want to acquaint yourself with the proper care and handling of  
arrays that have some of there dimensions = 1. Indexing will drop  
dimension if you fail to use drop=FALSE:

tt <- array(,dim=c(1,1,3,3))

 > tt[,,,1]
[1] NA NA NA


>
> but it did not work.

You should report the full error codes rather than saying "did not  
work".

-- David.



More information about the R-help mailing list