[R] avoiding eval parse with indexing

arun smartpink111 at yahoo.com
Sun May 26 17:32:20 CEST 2013



  the code returns Error:
 do.call("[", c(list(z),1,1,ix))
#Error in 1:24[1, 1, 1:3, 1:4] : incorrect number of dimensions
May be something is missing.
A.K.



----- Original Message -----
From: Bert Gunter <gunter.berton at gene.com>
To: Martin Ivanov <tramni at abv.bg>
Cc: r-help at r-project.org
Sent: Sunday, May 26, 2013 10:43 AM
Subject: Re: [R] avoiding eval parse with indexing

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

______________________________________________
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.




More information about the R-help mailing list