Re: [R] Dimnames of array inside loop

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Wed, 23 Jun 2010 17:44:28 -0400

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".


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. Received on Wed 23 Jun 2010 - 21:46:48 GMT

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 Wed 23 Jun 2010 - 22:00:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive