[R] Efficient nested loops
David Winsemius
dwinsemius at comcast.net
Thu Oct 21 16:30:14 CEST 2010
On Oct 21, 2010, at 8:39 AM, Petr PIKAL wrote:
> Hi
>
> fjsanala at gmail.com napsal dne 21.10.2010 12:44:30:
>
>> Hi Petr, thanks for your help.
>>
>> My array has four dimensions. My problem is the next:
>>
>> library(survival)
>> initial <- array(rnorm(1200000),c(40,30,20,50))
>> final <- array(0,
> dim=c(dim(initial)[1],dim(initial)[2],dim(initial)[3]))
>> for (i in 1:dim(initial)[1]){
>> for (j in 1:dim(initial)[2]){
>> for (k in 1:dim(initial)[3]){
>> final[i,j,k] <- function(initial[i,j,k,])
>> }
>> }
>> }
>>
>> My own function has a high computational cost, so Can I use the apply
> function?
You can, of course do so, but it may not yield improvements.
>
> Well it strongly depends what and how function "function" computes its
> result.
> Basically you can use apply for computing a result over margin of
> array
>
>> ar<-array(1:120, c(5,4,3,2))
>> apply(ar, 4, sum)
> [1] 1830 5430
>> apply(ar, 3, sum)
> [1] 1620 2420 3220
>> apply(ar, 2, sum)
> [1] 1590 1740 1890 2040
>> apply(ar, 1, sum)
> [1] 1404 1428 1452 1476 1500
>
>> apply(ar, 4, range)
> [,1] [,2]
> [1,] 1 61
> [2,] 60 120
>
I think that is not correct:
> initial <- array(rnorm(120),c(4,3,2,5))
> final1 <- array(0, dim=c(dim(initial)[1],dim(initial)
[2],dim(initial)[3]))
> for (i in 1:dim(initial)[1]){
+ for (j in 1:dim(initial)[2]){
+ for (k in 1:dim(initial)[3]){
+ final1[i,j,k] <- max(initial[i,j,k,])
+ }
+ }
+ }
> final2 <- array(0, dim=c(dim(initial)[1],dim(initial)
[2],dim(initial)[3]))
> final2 <- apply(initial, 1:3, max)
> # the proper indexing for apply is 1:3 rather than 4
> identical(final2, final1)
[1] TRUE
> final3 <- apply(initial, 4, max)
> identical(final3, final1)
[1] FALSE
--
David.
> Regards
> Petr
>
>>
>> Thank you!
>>
>>
>
>> 2010/10/21 Petr PIKAL <petr.pikal at precheza.cz>
>> Hi
>>
>> r-help-bounces at r-project.org napsal dne 21.10.2010 10:40:40:
>>
>>> Dear R community,
>>>
>>> I am working with huge arrays, so I spend a lot of time computing.
> This
>> is
>>> my code:
>>>
>>> for (x in 1:dim(variable)[1]){
>>> for (y in 1:dim(variable)[2]){
>>> for (z in 1:dim(variable)[3]){
>>> result <- max(variable[x,y,z,])
>>> }
>>> }
>>> }
>>>
>>> Is there a more efficient procedure to do this task?
>
>> is this what you are looking for?
>>
>> ar<-array(rnorm(24),c(4,3,2))
>> apply(ar, 3, max)
>>
>> Regards
>> Petr
>>
>>
>>>
>>> Thanks in advance!
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list