Skip to content

Apply a function to columns of a matrix

4 messages · Andras Farkas, jim holtman, arun

#
Is this what you wanted?  You had two arguments to your function, but
only supplying one via the 'apply'.  Also your argument names were the
same as your variables which was confusing.
[1]   99  279  459  639  819  999 1179 1359 1539 1719
Were you assuming that you could reference the value of 'b' within the
function?  If so, you could have done this:
[1]   99  279  459  639  819  999 1179 1359 1539 1719

        
On Sat, Sep 8, 2012 at 3:45 PM, Andras Farkas <motyocska at yahoo.com> wrote:

  
    
#
Hi,

Sorry, there was a mistake in my previous code:
f1<-function(x) sum(diff(b)*(x[-1]+x[-length(x)]))/2
a1<-split(t(a),1:ncol(a))
mapply(f1,a1)
#?? 1??? 2??? 3??? 4??? 5??? 6??? 7??? 8??? 9?? 10 
? #99? 279? 459? 639? 819? 999 1179 1359 1539 1719 
A.K.



----- Original Message -----
From: Andras Farkas <motyocska at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Saturday, September 8, 2012 3:45 PM
Subject: [R] Apply a function to columns of a matrix

Dear All,
?
as a follow up to my previous e-mail (I think I am getting closer...):
?
I am trying to apply the trapezoidal functions to a matric column by column. I have the following code:
?
a <-matrix(c(1:100),ncol=10)
b <-matrix(c(2,4,6,8,10,12,14,16,18,20))
?
apply(a,2,function(b,a) sum(diff(b)*(a[-1]+a[-length(a)]))/2)
?
for some reason i get an error message:
Error in FUN(newX[[, i], ...): argument "a" is missing with no default.
?
Any ideas of why that may be happening?
?
thanks,
?
Andras
??? [[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.
#
Hi,
You can also use sapply()
sapply(split(t(a),1:ncol(a)),function(x) sum(diff(b)*(x[-1]+x[-length(x)]))/2)
#?? 1??? 2??? 3??? 4??? 5??? 6??? 7??? 8??? 9?? 10 
? #99? 279? 459? 639? 819? 999 1179 1359 1539 1719 
A.K.



----- Original Message -----
From: Andras Farkas <motyocska at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Saturday, September 8, 2012 3:45 PM
Subject: [R] Apply a function to columns of a matrix

Dear All,
?
as a follow up to my previous e-mail (I think I am getting closer...):
?
I am trying to apply the trapezoidal functions to a matric column by column. I have the following code:
?
a <-matrix(c(1:100),ncol=10)
b <-matrix(c(2,4,6,8,10,12,14,16,18,20))
?
apply(a,2,function(b,a) sum(diff(b)*(a[-1]+a[-length(a)]))/2)
?
for some reason i get an error message:
Error in FUN(newX[[, i], ...): argument "a" is missing with no default.
?
Any ideas of why that may be happening?
?
thanks,
?
Andras
??? [[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.