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 On Sun, Jul 26, 2009 at 3:54 PM, Poersching<poerschin...@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 < ggrothendi...@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.s...@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@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@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)) > } > } > } > return(ind) > } > > Regards, > Christian Porsche > [[alternative HTML version deleted]] ______________________________________________ R-help@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.