# Re: [R] apply in apply

From: baptiste Auguiť <ba208_at_exeter.ac.uk>
Date: Fri, 30 May 2008 10:37:17 +0100

> intg <- function (y, x)
> {
> n <- length(x)
> index <- order(x)
> dx <- diff(sort(x))
> z <- y[index]
> ys <- (z[1:(n - 1)] + z[2:n])/2
> sum(ys * dx)
> }
> <environment: namespace:PROcess>

Thanks again for the suggestions,

baptiste

```>> I need to apply a function on each column of each matrix contained in
>> a list. Consider the following code,
>>
>>> x <- 1:3
>>> my.data <- list(matrix(c(1,2,3,4,5,6),ncol=2),
>>>       matrix(c(4,5,6,7,8,9),ncol=2))
>>>
>>> par(mfrow=c(2,2))
>>> results <- sapply(1:length(my.data),
>>>          function(ii) apply(my.data[[ii]], 2, function(y) plot
>>> (x,y) ))
```

> #
```>>> plot is for demonstration purposes
>>
>>
>> It works, but I think this is quite dirty code. Is there a simpler
>> way of achieving this?
>
```

> The last line can be simplified
> results <- sapply(my.data, function(x) apply(x,2,sum))
```>
```

> (It is perhaps a little clearer what is going on when you use sum
> rather
> than plot as the example function.)
```>
```

> Regards,
> Richie.
```>
```

> Mathematical Sciences Unit
> HSL
```>
>

> ----------------------------------------------------------------------
```
> --
> ATTENTION:

>
> This message contains privileged and confidential info...{{dropped:30}}

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. Received on Mon 02 Jun 2008 - 05:57:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 02 Jun 2008 - 06:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.