[R] could we use ":" to represent multiple matrice in a list or sequential chracter names
David Winsemius
dwinsemius at comcast.net
Wed Jan 20 05:17:18 CET 2010
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
More information about the R-help
mailing list