rusers.sh
2010-Jan-20 03:57 UTC
[R] could we use ":" to represent multiple matrice in a list or sequential chracter names
Hi, I know we can use 1:10 to represent the 1,2,3,...,10 numbers, but the following conditions are except. Anybody knows how to represent the following two cases with similar usage of ":" or others? Usually, i will get several hundred names for them, such as a1,a2,... or f[[1]],f[[2]],... #Example data a1<-array(1:12,c(2,3,2)); a2<-array(2,c(2,3,2)); a3<-array(0,c(2,3,2)) a1[1,2,1]:a3[1,2,1] [1] 3 2 1 0 #the correct result should be 3,2,0 So the method of using "a1[i,j,k]:a3[i,j,k]" to represent "c(a1[i,j,k],a2[i,j,k],a3[i,j,k])" is not correct? ##For lists with matrices as its elements f<-list() f[[1]]<-a1;f[[2]]<-a2;f[[3]]<-a3 f[[1]][1,2,1]:f[[3]][1,2,1] [1] 3 2 1 0 #the correct result should be 3,2,0 So the method of using "f[[1]][i,j,k]:f[[3]][i,j,k]" to represent "f[[1]][i,j,k],f[[2]][i,j,k],f[[3]][i,j,k])" is not correct? I noticed that the above two methods have got the same results, although not correct. So i guess i have made the same errors in them. Is there some method to represent them concisely? Any ideas about them? Thanks a lot. -- ----------------- Jane Chang Queen's [[alternative HTML version deleted]]
David Winsemius
2010-Jan-20 04:17 UTC
[R] could we use ":" to represent multiple matrice in a list or sequential chracter names
On Jan 19, 2010, at 10:57 PM, rusers.sh wrote:> Hi, > I know we can use 1:10 to represent the 1,2,3,...,10 numbers, but the > following conditions are except. > Anybody knows how to represent the following two cases with similar > usage > of ":" or others? Usually, i will get several hundred names for > them, such > as a1,a2,... or f[[1]],f[[2]],... > #Example data > a1<-array(1:12,c(2,3,2)); a2<-array(2,c(2,3,2)); a3<-array(0,c(2,3,2))> agb <-array( , dim=c(3,2,3,2)) > agb[1,,,] <- a1 > agb[2,,,] <- a2 > agb[3,,,] <- a3 > agb[1:3 , 1, 2, 1] [1] 3 2 0> a1[1,2,1]:a3[1,2,1] > [1] 3 2 1 0 > #the correct result should be 3,2,0 > So the method of using "a1[i,j,k]:a3[i,j,k]" to represent > "c(a1[i,j,k],a2[i,j,k],a3[i,j,k])" is not correct?Yes, not correct.> ##For lists with matrices as its elements > f<-list() > f[[1]]<-a1;f[[2]]<-a2;f[[3]]<-a3 > f[[1]][1,2,1]:f[[3]][1,2,1] > [1] 3 2 1 0 > #the correct result should be 3,2,0 > So the method of using "f[[1]][i,j,k]:f[[3]][i,j,k]" to represent > "f[[1]][i,j,k],f[[2]][i,j,k],f[[3]][i,j,k])" is not correct?In both your examples the interpreter reduces the expression to 3:0 and that results in 3 2 1 0> I noticed that the above two methods have got the same results, > although > not correct. So i guess i have made the same errors in them. Is > there some > method to represent them concisely? > Any ideas about them? > Thanks a lot.-- David Winsemius, MD Heritage Laboratories West Hartford, CT
Dennis Murphy
2010-Jan-20 04:19 UTC
[R] could we use ":" to represent multiple matrice in a list or sequential chracter names
Hi: The : operator is meant for numeric sequences; see ?':' On Tue, Jan 19, 2010 at 7:57 PM, rusers.sh <rusers.sh@gmail.com> wrote:> Hi, > I know we can use 1:10 to represent the 1,2,3,...,10 numbers, but the > following conditions are except. > Anybody knows how to represent the following two cases with similar usage > of ":" or others? Usually, i will get several hundred names for them, such > as a1,a2,... or f[[1]],f[[2]],... > #Example data > a1<-array(1:12,c(2,3,2)); a2<-array(2,c(2,3,2)); a3<-array(0,c(2,3,2)) > a1[1,2,1]:a3[1,2,1] > [1] 3 2 1 0 > #the correct result should be 3,2,0 >I tried this:> a <- list(a1, a2, a3) > unlist(lapply(a, '[', 1, 2, 1))[1] 3 2 0 If you're trying to get the same element out of a bunch of arrays, this might be one way to go. You might also want to investigate the iapply function in the iterators package, since it manipulates indices in 3D arrays rather easily. HTH, Dennis So the method of using "a1[i,j,k]:a3[i,j,k]" to represent> "c(a1[i,j,k],a2[i,j,k],a3[i,j,k])" is not correct? > ##For lists with matrices as its elements > f<-list() > f[[1]]<-a1;f[[2]]<-a2;f[[3]]<-a3 > f[[1]][1,2,1]:f[[3]][1,2,1] > [1] 3 2 1 0 > #the correct result should be 3,2,0 > So the method of using "f[[1]][i,j,k]:f[[3]][i,j,k]" to represent > "f[[1]][i,j,k],f[[2]][i,j,k],f[[3]][i,j,k])" is not correct? > I noticed that the above two methods have got the same results, although > not correct. So i guess i have made the same errors in them. Is there some > method to represent them concisely? > Any ideas about them? > Thanks a lot. > > > -- > ----------------- > Jane Chang > Queen's > > [[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]]