Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a three dimensional array, myarray

myarray<- array(dim=c(4,6,5))

and a matrix, mymatrix

     > mymatrix
        Bias  TS PC   H    F     FAR
 red     1.0 1.0  1 1.0 0.00 0.00000
 orange  1.0 1.0  1 1.0 0.00 0.00000
 yellow  0.5 0.5  1 0.5 0.00 0.00000
 medium  1.0 1.0  1 1.0 0.25 0.00037

Now I want to assign mymatrix as one of the element in myarray. So I am doing the following

> myarray[,,1]<-mymatrix
> myarray[,,1]
     [,1] [,2] [,3] [,4] [,5]    [,6]
[1,]  1.0  1.0    1  1.0 0.00 0.00000
[2,]  1.0  1.0    1  1.0 0.00 0.00000
[3,]  0.5  0.5    1  0.5 0.00 0.00000
[4,]  1.0  1.0    1  1.0 0.25 0.00037

But my problem is that I want to have the column and row names assigned to the array (from my matrix)as well. How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can explicitly set row and column names on your array. Note they span all slices of the array:

Empty array (note you really should make your code cut n pastable)

> myarray=array(dim=c(4,6,10))
> myarray[,,1]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA
[3,]   NA   NA   NA   NA   NA   NA
[4,]   NA   NA   NA   NA   NA   NA

Assign mymatrix to a slice:

> myarray[,,1]=mymatrix
> myarray[,,1]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    5    0    0    1    1    2
[2,]   10   10   11    1   11    0
[3,]    0    8   11    2    2    8
[4,]    9    6    2    5    3    0

Set row and column names:

> rownames(myarray)=rownames(mymatrix)
> colnames(myarray)=colnames(mymatrix)
> myarray[,,1]
       Bias TS PC H  F FAR
red       5  0  0 1  1   2
orange   10 10 11 1 11   0
yellow    0  8 11 2  2   8
medium    9  6  2 5  3   0

Note that all the slices have the same row and column names now:

> myarray[,,2]
       Bias TS PC  H  F FAR
red      NA NA NA NA NA  NA
orange   NA NA NA NA NA  NA
yellow   NA NA NA NA NA  NA
medium   NA NA NA NA NA  NA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...