[R] apply in apply

2008-05-30 Thread baptiste Auguié

Dear list,

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? I was considering recasting the list into a  
matrix, apply the function to all its columns, and then reshape the  
result into an array of adequate dimensions, as in:


results - sapply(matrix(unlist(my.data),ncol= 2 * length 
(my.data)), function(y) plot(x,y))
#results - matrix(results, ncol = length(my.data)) # when the  
actual function is used instead of plot



but again it feels a bit twisted.

Best regards,

baptiste



_

Baptiste Auguié

Physics Department
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
http://projects.ex.ac.uk/atto

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


Re: [R] apply in apply

2008-05-30 Thread Richard . Cotton
 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 inform...{{dropped:20}}

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


Re: [R] apply in apply

2008-05-30 Thread baptiste Auguié
Thank you for the suggestions (off-list as well). I think the best  
option may eventually be an explicit for loop to make things clearer.  
To clarify a bit, I've used the plot function in the example where in  
fact it is a numerical integration (which is why I need to pass an  
additional variable in the second apply call),



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

On 30 May 2008, at 10:02, [EMAIL PROTECTED] wrote:


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


Re: [R] apply in apply

2008-05-30 Thread Charilaos Skiadas


On May 30, 2008, at 5:37 AM, baptiste Auguié wrote:

Thank you for the suggestions (off-list as well). I think the best  
option may eventually be an explicit for loop to make things  
clearer. To clarify a bit, I've used the plot function in the  
example where in fact it is a numerical integration (which is why I  
need to pass an additional variable in the second apply call),



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,


I think this is where the beauty of ... comes in, the following  
should be doing just what you want:


sapply(my.data, apply, 2, intg, x)

More clear? Not sure I can judge that, certainly more  concise.  
sapply just passes the extra arguments to apply, which then just  
passes them to intg.



baptiste


Haris Skiadas
Department of Mathematics and Computer Science
Hanover College


On 30 May 2008, at 10:02, [EMAIL PROTECTED] wrote:

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



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


Re: [R] apply in apply

2008-05-30 Thread baptiste Auguié
Very nice, i really like this one! May confuse non-R users, but  
that's not a concern here.


Thanks a lot,

baptiste

On 30 May 2008, at 13:19, Charilaos Skiadas wrote:



I think this is where the beauty of ... comes in, the following  
should be doing just what you want:


sapply(my.data, apply, 2, intg, x)


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