[R] dimnames in array
David Winsemius
dwinsemius at comcast.net
Wed Aug 8 18:10:14 CEST 2012
On Aug 8, 2012, at 8:43 AM, aleksandr russell wrote:
> Hi again,
>
> Thanks for your suggestion.
> I think I can clarify the thing by looking at the bare bones:
>
> data11 = array(0,c(41,2,2))
>
> m8<-cbind(.29,1:41)
> m9<-as.array(m8,dim=c(41,2))
> data11[,1,]=m9
> data11[,2,]=m9
>
> varnames=c("V","R")
> colnames(data11)=c("one","two")
> colnames(data11)=c("one","two"); str(data11)
num [1:41, 1:2, 1:2] 0.29 0.29 0.29 0.29 0.29 0.29 0.29 0.29 0.29
0.29 ...
- attr(*, "dimnames")=List of 3
..$ : NULL
..$ : chr [1:2] "one" "two"
..$ : NULL
> dimnames(data11)=list(rownames(data11),varnames,colnames(data11))
> data11a<-as.array(data11,dimnames=dimnames(data11))
> str(data11)
num [1:41, 1:2, 1:2] 0.29 0.29 0.29 0.29 0.29 0.29 0.29 0.29 0.29
0.29 ...
- attr(*, "dimnames")=List of 3
..$ : NULL
..$ : chr [1:2] "V" "R"
..$ : chr [1:2] "one" "two"
Dimnames is a list with three lists.
>
>
> Just running the above, some artificial data, I come out with
> an array that seems to be have the right attributes: typing
> "attributes(data11a)" I get dimnames for each dimension.
>
> The problem is that [package 'colloc infer' says]
> (and I quote):
> Error in `colnames<-`(`*tmp*`, value = c("V", "R")) :
> length of 'dimnames' [2] not equal to array extent
You are not offering code and data that would help understand the
problem.
>
> It seems that the names for the second dimension of the array must
> have length of 2 -- rather than the present "1" for the analysis to
> work.
>
> Again, typing
>
> length(dimnames(data11a)[2])
> I get
>
> [1] 1
>
> I've tried your suggestion for the dimnames but I'm getting an error
> "could not find function paste0"
> Maybe you're using R later than 2.41?
Yes. R 2.15.1 has added paste0() which is basically paste(... , sep="").
--
>
> thanks,
> Alexander
>
> On Wed, Aug 8, 2012 at 10:52 AM, David Winsemius <dwinsemius at comcast.net
> > wrote:
>>
>> On Aug 8, 2012, at 3:19 AM, aleksandr russell wrote:
>>
>>> Hello,
>>>
>>> I'm working with an array; I'm trying to make it so that an array of
>>> dim(42,2,2) has names whose length corresponds to that of the array,
>>> and am hoping someone with experience with this can see what I'm not
>>> doing correctly:
>>>
>>>
>>>
>>>
>>> data11 = array(0,c(41,2,2))
>>>
>>> y = lsoda(x0,times,fhn$fn.ode,pars)#This is make.fhn() from colloc
>>> infer package#
>>
>>
>>> y = y[,2:3]
>>>
>>> data11<-array(0,c(41,2,2))
>>>
>>>
>>> m8<-cbind(.29,1:41)
>>> m9<-as.array(m8,dim=c(41,2))
>>>
>>> data11[,1,] = y+m9
>>> data11[,2,] = y+m9
>>
>>
>> I didn't run that code since your difficulties did not seem to have
>> anything
>> to do with the values inside the array but only with the dimension
>> labeling.
>>
>>>
>>> rownames(data2, do.NULL = FALSE, prefix="row")
>>
>>
>> What is 'data2'?
>>
>>
>>> varnames=c("V","R")
>>> colnames(data6)=c("one","two")
>>
>>
>> Ditto... what is 'data6'?
>>
>>
>>> colnames(data10)=colnames(data6)
>>> dimnames(data11)=list(rownames(data2),varnames,colnames(data10))
>>> data11a<-as.array(data11,dimnames=dimnames(data11))
>>> attributes(data11a)
>>>
>>> #so this seems to print out the array attributes as they
>>> should be:
>>
>>
>> For which object?
>>
>>
>>> two sets of two columns each, and the first column of
>>> each set is called 'v' and the second 'r'- no problem#
>>
>>
>> The data11 object had three dimenstions.
>>
>>
>>>
>>> now the analysis from colloc infer(I've put it in below*)
>>> takes this array but will not complete the analysis, saying
>>>
>>> "Error in `colnames<-`(`*tmp*`, value = c("V", "R")) :
>>> length of 'dimnames' [2] not equal to array extent"
>>
>>
>> Here's one way to label a 41 x 2 x 2 array:
>>
>>> dimnames(data11)<- c(list(X=paste0("X",1:41)), list(Y=paste0("Y",
>>> 1:2)),
>>> list(Z=paste0("Z",1:2)) )
>>> attributes(data11)
>> $dim
>> [1] 41 2 2
>>
>> $dimnames
>> $dimnames$X
>> [1] "X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10"
>> "X11" "X12"
>> "X13" "X14" "X15" "X16" "X17" "X18" "X19"
>> [20] "X20" "X21" "X22" "X23" "X24" "X25" "X26" "X27" "X28" "X29"
>> "X30" "X31"
>> "X32" "X33" "X34" "X35" "X36" "X37" "X38"
>> [39] "X39" "X40" "X41"
>>
>> $dimnames$Y
>> [1] "Y1" "Y2"
>>
>> $dimnames$Z
>> [1] "Z1" "Z2"
>>
>>>
>>> When I type in
>>>
>>> length(dimnames(data11a)[2])
>>>
>>> I get the answer:
>>>
>>> [1] 1
>>>
>>> which seems odd when
>>>
>>> dimnames(data11a)[2]
>>>
>>> gives
>>>
>>> [[1]]
>>> [1] "V" "R"
>>
>>
>> It is a one element list whose single element is a 2 element vector.
>>
>>
>>>
>>> I would like to ask "How can I get the names of the second dimension
>>> of the array(data11a) to have length "2" ?
>>>
>>> It seems to me that the analysis will run if I can make this array
>>> have the correct length dimnames
>>>
>>> I thank you for your help.
>>>
>>> ____________
>>>
>>>
>>> *
>>>
>>
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list