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
On 26-05-2013, at 15:56, Martin Ivanov <tramni at abv.bg> wrote:> 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?I tried this x1 <- array(runif(9),dim=c(3,3)) x2 <- array(runif(8),dim=c(2,2,2)) and then x1[1] and x2[1] gave me what you wanted. I don't know if it is the coRRect way to do what you want. Berend
Martin: Well, assuming I understand, one approach would be to first get the dim attribute of the array and then create the appropriate call using that:> z <- array(1:24,dim=2:4) > d <- dim(z) > ix <-lapply(d[-c(1,1)],seq_len) > do.call("[", c(list(z),1,1,ix))[1] 1 7 13 19 Is that what you want? -- Bert On Sun, May 26, 2013 at 6:56 AM, Martin Ivanov <tramni at abv.bg> wrote:> 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 at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
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 <tramni at abv.bg> To: r-help at 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 at 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.
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 at 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 at 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. > >