Dear Arun, Thank You very much, your suggestion also works and seems to also be more convenient. I think though, that Mr Gunter's suggestion should be more efficient, as it uses directly the Extract operator.
Thank You all very much for Your responsiveness. R does have a wonderful community! Best regards, Martin >-------- Оригинално писмо -------- >От: arun >Относно: Re: [R] avoiding eval parse with indexing >До: Martin Ivanov >Изпратено на: Неделя, 2013, Май 26 18:48:33 EEST > > >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 >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.