--- Begin Message ---
Dear Mr Gunter,
with a slight correction:
z <- array(1:24,dim=2:4)
d <- dim(z)
ix <- lapply(d[-1], seq_len)
do.call("[", c(list(z),1,ix))
[,1] [,2] [,3] [,4]
[1,] 1 7 13 19
[2,] 3 9 15 21
[3,] 5 11 17 23
your suggestion worked and is exactly what I wanted!
Thank You very much indeed!
I want to get rid of all eval(parse) constructs in my code,
and this was the greatest obstacle I had. Now I will be able to do this.
As they say, eval parse in the code is just lack on knowledge.
Best regards,
Martin
>-------- Оригинално писмо --------
>От: Florent D.
>Относно: Re: [R] avoiding eval parse with indexing
>До: Bert Gunter
>Изпратено на: Неделя, 2013, Май 26 18:07:26 EEST
>
>
>library(abind)
>asub(x, 1, 1)
>
>On Sun, May 26, 2013 at 10:43 AM, Bert Gunter wrote:
>> 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 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@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@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.
>
--- End Message ---
______________________________________________
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.