Hi, Another way would be: library(arrayhelpers) slice(aperm(x1,c(2,1)),j=1) #[1] 0.6439062 0.7139397 0.6017418 slice(aperm(x2,c(2,1,3)),j=1) # [,1] [,2] #[1,] 0.026671344 0.2116831 #[2,] 0.003903368 0.1551140
slice(aperm(z,c(2,1,3)),j=1) # [,1] [,2] [,3] [,4] #[1,] 1 7 13 19 #[2,] 3 9 15 21 #[3,] 5 11 17 23 #or array(z[slice.index(z,1)==1],dim= dim(z)[2:3]) # [,1] [,2] [,3] [,4] #[1,] 1 7 13 19 #[2,] 3 9 15 21 #[3,] 5 11 17 23 array(x2[slice.index(x2,1)==1],dim= dim(x2)[2:3]) # [,1] [,2] #[1,] 0.026671344 0.2116831 #[2,] 0.003903368 0.1551140 array(x1[slice.index(x1,1)==1],dim= dim(x1)[1]) #[1] 0.6439062 0.7139397 0.6017418 A.K. ----- Original Message ----- From: arun <smartpink...@yahoo.com> To: Martin Ivanov <tra...@abv.bg> Cc: R help <r-help@r-project.org>; Berend Hasselman <b...@xs4all.nl>; Bert Gunter <gunter.ber...@gene.com> Sent: Sunday, May 26, 2013 11:48 AM Subject: Re: [R] avoiding eval parse with indexing Hi, You could use: library(abind) #using Berend's and Bert's example x1 <- array(runif(9),dim=c(3,3)) x2 <- array(runif(8),dim=c(2,2,2)) z <- array(1:24,dim=2:4) #applying your code: eval(parse(text=paste0("x1[1",paste(rep(",",length(dim(x1))-1),collapse=""),"]"))) #[1] 0.6439062 0.7139397 0.6017418 eval(parse(text=paste0("x2[1",paste(rep(",",length(dim(x2))-1),collapse=""),"]"))) # [,1] [,2] #[1,] 0.026671344 0.2116831 #[2,] 0.003903368 0.1551140 eval(parse(text=paste0("z[1",paste(rep(",",length(dim(z))-1),collapse=""),"]"))) # [,1] [,2] [,3] [,4] #[1,] 1 7 13 19 #[2,] 3 9 15 21 #[3,] 5 11 17 23 asub(x1,1,1,drop=TRUE) #[1] 0.6439062 0.7139397 0.6017418 asub(x2,1,1,drop=TRUE) # [,1] [,2] #[1,] 0.026671344 0.2116831 #[2,] 0.003903368 0.1551140 asub(z,1,1,drop=TRUE) # [,1] [,2] [,3] [,4] #[1,] 1 7 13 19 #[2,] 3 9 15 21 #[3,] 5 11 17 23 A.K. ----- Original Message ----- From: Martin Ivanov <tra...@abv.bg> To: r-help@r-project.org Cc: Sent: Sunday, May 26, 2013 9:56 AM Subject: [R] avoiding eval parse with indexing Hello, I would like to get an advice on how the notorious eval(parse()) construct could possibly be avoided in the following example. I have an array x, which can have different number of dimensions, but I am only interested in extracting, say, the first element of the first dimension. Currently I achieve this in this way: eval(parse(text=paste0("x[1", paste(rep(", ", length(dim(x)) - 1), collapse=""), "]"))) Is it possible to avoid the eval parse here? How? Best regards, Martin ______________________________________________ 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. ______________________________________________ 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.