[R] Determine the dimension-names of an element in an array in R

Poersching Poersching79 at web.de
Tue Jul 28 15:58:50 CEST 2009


Hey,
I think I have a solution for your problem:

Correl<-apply(DataArray_1,1:3, function(d1)
  apply(DataArray_2,c(2,1,3), function(d) cor(d1,d))
)
Correl<-Correl[1:4,,,]
dimnames(Correl)[[1]]<-c
Correl<-aperm(Correl,c(2,3,1,4))

This one should work. :-)

Best Regards,
Christian

Sauvik De schrieb:
> Hi there,
>
> Thanks again for your reply. I know for-loop is always a solution to
> my problem and I had already coded using for-loop. But the number of
> levels for each dimension is large enough in actual problem and hence
> it was time-consuming.
> So, I was just wondering if there are any other alternative way-outs
> to solving my problem. That's why I tried with apply functions
> (sapply)assuming that this might work out faster even fractionally as
> compared to for-loop.
>
> Cheers,
> Sauvik
>
> On Mon, Jul 27, 2009 at 12:28 AM, Poersching <Poersching79 at web.de
> <mailto:Poersching79 at web.de>> wrote:
>
>     Sauvik De schrieb:
>>     Hi:
>>     Lots of thanks for your valuable time!
>>
>>     But I am not sure how you would like to use the function in this
>>     situation.
>>
>>     As I had mentioned that the first element of my output array
>>     should be like:
>>
>>     cor(DataArray_1[dimnames(Correl)[[1]][1],dimnames(Correl)[[2]][1],dimnames(Correl)[[4]][1],],DataArray_2[dimnames(Correl)[[1]][1],dimnames(Correl)[[3]][1],dimnames(Correl)[[4]][1],],use="pairwise.complete.obs")
>>
>>     in my below code.
>>
>>     and
>>
>>     the output array of correlation I wish to get using "sapply" as
>>     follows:
>>
>>     Correl = sapply(Correl,function(d)
>>     cor(DataArray_1[...],DataArray_2[...],
>>     use="pairwise.complete.obs"))
>>
>>     So it would be of great help if you could kindly specify how to
>>     utilise your function "findIndex" in ...
>>
>>     Apologies for all this!
>>
>>     Thanks & Regards,
>>     Sauvik
>>
>     Hey,
>     sorry, I haven't understood your problem last time, but now this
>     solution should solve your problem, so I hope. :-)
>     It's only a for to loop, but an apply function may work too. I
>     will think about this, but for now...  ;-)
>
>     la<-length(a)
>     lb<-length(b)
>     lc<-length(c)
>     ld<-length(d)
>     for (ia in 1:la) {
>       for (ib in 1:lb) {
>         for (ic in 1:lc) {
>           for (id in 1:ld) {
>             Correl[ia,ib,ic,id]<-cor(
>              DataArray_1[dimnames(Correl)[[1]][ia],
>              dimnames(Correl)[[2]][ib],
>              dimnames(Correl)[[4]][id],]
>              ,
>              DataArray_2[dimnames(Correl)[[1]][ia],
>               dimnames(Correl)[[3]][ic],
>               dimnames(Correl)[[4]][id],]
>              ,
>              use="pairwise.complete.obs")
>           }
>         }
>       }
>     }
>     ## with function findIndex you can find the dimensions with
>     ## i.e. cor values greater 0.5 or smaller -0.5, like:
>     findIndex(Correl,Correl[Correl>0.5])
>     findIndex(Correl,Correl[Correl<(-0.5)])
>
>     I have changed the code of the function findIndex in line which
>     contents: el[j]<-which(is.element(data,element[j]))
>
>     Rigards,
>     Christian
>
>>
>>     On Sun, Jul 26, 2009 at 3:54 PM, Poersching<Poersching79 at web.de
>>     <mailto:Poersching79 at web.de>> wrote:
>>     > Sauvik De schrieb:
>>     >
>>     > Hi Gabor:
>>     > Many thanks for your prompt reply!
>>     > The code is fine. But I need it in more general form as I had
>>     mentioned that
>>     > I need to input any 0 to find its dimension-names.
>>     >
>>     > Actually, I was using "sapply" to calculate correlation and
>>     this idea was
>>     > required in the middle of correlation calculation.
>>     > I am providing the way I tried my calculation.
>>     >
>>     > a= c("A1","A2","A3","A4","A5")
>>     > b= c("B1","B2","B3")
>>     > c= c("C1","C2","C3","C4")
>>     > d= c("D1","D2")
>>     > e= c("E1","E2","E3","E4","E5","E6","E7","E8")
>>     >
>>     > DataArray_1 = array(c(rnorm(240)),dim=c(length(a),length(b),
>>     > length(d),length(e)),dimnames=list(a,b,d,e))
>>     > DataArray_2 = array(c(rnorm(320)), dim=c(length(a),length(c),
>>     > length(d),length(e)),dimnames=list(a,c,d,e))
>>     >
>>     > #Defining an empty array which will contain the correlation
>>     values (output
>>     > array)
>>     > Correl = array(NA, dim=c(length(a),length(b),
>>     > length(c),length(d)),dimnames=list(a,b,c,d))
>>     >
>>     > #Calculating Correlation between attributes b & c over values of e
>>     > Correl = sapply(Correl,function(d)
>>     cor(DataArray_1[...],DataArray_2[...],
>>     > use="pairwise.complete.obs"))
>>     >
>>     > This is where I get stuck.
>>     > In the above, d is acting as an element in the "Correl" array.
>>     Hence I need
>>     > to get the dimension-names for d.
>>     >
>>     > #The first element of Correl will be:
>>     >
>>     cor(DataArray_1[dimnames(Correl)[[1]][1],dimnames(Correl)[[2]][1],dimnames(Correl)[[4]][1],],DataArray_2[dimnames(Correl)[[1]][1],dimnames(Correl)[[3]][1],dimnames(Correl)[[4]][1],],use="pairwise.complete.obs")
>>     >
>>     > So my problem boils down to extracting the dim-names in terms
>>     of element(d)
>>     > and not in terms of Correl (that I have mentioned as "..." in
>>     the above
>>     > code)
>>     >
>>     > My sincere thanks for your valuable time & suggestions.
>>     >
>>     > Many Thanks & Kind Regards,
>>     > Sauvik
>>     >
>>     >
>>     > On Sun, Jul 26, 2009 at 5:26 AM, Gabor Grothendieck
>>     <ggrothendieck at gmail.com <mailto:ggrothendieck at gmail.com>
>>     >  
>>     >
>>     > wrote:
>>     >    
>>     >
>>     >  
>>     >
>>     > Try this:
>>     >
>>     >    
>>     >
>>     > ix <- c(1, 3, 4, 2)
>>     > mapply("[", dimnames(mydatastructure), ix)
>>     >      
>>     >
>>     > [1] "S1" "T3" "U4" "V2"
>>     >
>>     >
>>     > On Sat, Jul 25, 2009 at 5:12 PM, Sauvik
>>     De<sauvik.stat at gmail.com <mailto:sauvik.stat at gmail.com>> wrote:
>>     >    
>>     >
>>     > Hi:
>>     > How can I extract the dimension-names of a pre-defined element in a
>>     > multidimensional array in R ?
>>     >
>>     > A toy example is provided below:
>>     > I have a 4-dimensional array with each dimension having certain
>>     length.
>>     >      
>>     >
>>     > In
>>     >    
>>     >
>>     > the below example, "mydatastructure" explains the structure of
>>     my data.
>>     >
>>     > mydatastructure = array(0,
>>     >      
>>     >
>>     > dim=c(length(b),length(z),length(x),length(d)),
>>     >    
>>     >
>>     > dimnames=list(b,z,x,d))
>>     >
>>     > where,
>>     > b=c("S1","S2","S3","S4","S5")
>>     > z=c("T1","T2", "T3")
>>     > x=c("U1","U2","U3","U4")
>>     > d=c("V1","V2")
>>     >
>>     > Clearly, "mydatastructure" contains many 0's.
>>     > Now how can I get the dimension-names of any particular 0 ?
>>     > That is, my input should be a particular 0 in the array
>>     "mydatastructure"
>>     > (Suppose this 0 corresponds to S1,T3,U4 & V2 in the array). Then my
>>     >      
>>     >
>>     > output
>>     >    
>>     >
>>     > should be S1,T3,U4 & V2.
>>     >
>>     > The function "dimnames" didn't help me with the solution.
>>     > Any idea will greatly be appreciated.
>>     >
>>     > Thanks for your time!
>>     >
>>     > Kind Regards,
>>     > Sauvik
>>     >
>>     >        [[alternative HTML version deleted]]
>>     >
>>     > ______________________________________________
>>     > R-help at r-project.org <mailto: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.
>>     >
>>     >      
>>     >
>>     > [[alternative HTML version deleted]]
>>     >
>>     > ______________________________________________
>>     > R-help at r-project.org <mailto: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.
>>     >
>>     >  
>>     >
>>     > Hey,
>>     >
>>     > I have spend some time to write a function, which should
>>     fulfill your needs.
>>     > so i hope ;-)
>>     >
>>     > findIndex<-function(data,element) {
>>     >   ld<-length(data)
>>     >   el<-which(is.element(data,element))
>>     >   lel<-length(el)
>>     >   ndim<-length(dim(data))
>>     >   ind<-array(,dim=c(lel,ndim),dimnames=list(el,1:ndim))
>>     >   precomma<-""
>>     >   tempdata<-data
>>     >   tempel<-el
>>     >   for (j in 1:lel) {
>>     >     data<-tempdata
>>     >     el<-tempel
>>     >     ld<-length(data)
>>     >     for (i in ndim:1) {
>>     >       ratio<-el[j]/(ld/dim(data)[i])
>>     >       if (ratio-trunc(ratio)>0) {
>>     >         ind[j,i]<-trunc(ratio)+1
>>     >       } else {
>>     >         ind[j,i]<-trunc(ratio)
>>     >       }
>>     >       if (length(dim(data))>1) {
>>     >         k<-1
>>     >         while (k>=1 & k<=(i-1)) {
>>     >           precomma<-paste(precomma,",",sep="")
>>     >           k<-k+1
>>     >         }
>>     >        
>>     >
>>     data<-as.array(eval(parse(text=paste("data[",precomma,ind[j,i],"]",sep=""))))
>>     >         precomma<-""
>>     >         ld<-length(data)
>>     >         el[j]<-which(is.element(data,element[j]))
>>
>>     >       }
>>     >     }
>>     >   }
>>     >   return(ind)
>>     > }
>>     >
>>     > Regards,
>>     > Christian Porsche
>>     >
>>
>
>




More information about the R-help mailing list