Stu @ AGS
2009-Jun-16 15:08 UTC
[R] The most straightfoward way to write a function that sums over the rows of a matrix
Hello! I am trying to write a function with vector and data.frame parameters that uses the sum() function and values from the rows of the data.frame. I need to pass this function as a parameter to optim(). My starting point is: observs <- data.frame(y, x1, x2, x3) Fn <- function(par, observs) { sum( (y - (par[1] * (x1 + 1) * x2^(-par[2]) * x3^par[3])^2 ) } y, x1, x2, x3 are all vectors. I am a bit new to R and I have not been able to find a good description of how to iterate over rows in a data.frame. What is a straightforward way to do this? What am I missing? Thanks Stu [[alternative HTML version deleted]]
Ronggui Huang
2009-Jun-16 15:12 UTC
[R] The most straightfoward way to write a function that sums over the rows of a matrix
Why not just use rowSums? Ronggui 2009/6/16 Stu @ AGS <stu at agstechnet.com>:> Hello! > > > > I am trying to write a function with vector and data.frame parameters that > uses the sum() function and values from the rows of the data.frame. > > I need to pass this function as a parameter to optim(). > > > > My starting point is: > > observs <- data.frame(y, x1, x2, x3) > > > > Fn <- function(par, observs) { > > ? ? ? ? ? ? ? ?sum( (y - (par[1] * (x1 + 1) * x2^(-par[2]) * x3^par[3])^2 ) > > } > > > > y, x1, x2, x3 are all vectors. > > > > I am a bit new to R and I have not been able to find a good description of > how to iterate over rows in a data.frame. > > > > What is a straightforward way to do this? > > What am I missing? > > > > Thanks > > Stu > > > > > ? ? ? ?[[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. >-- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html
David Winsemius
2009-Jun-16 15:31 UTC
[R] The most straightfoward way to write a function that sums over the rows of a matrix
The apply function is the way to iterate over rows or columns of dataframes or matrices. An example would have made this process easier testing and I have given up doing that job for You might try: apply(observs, 1, function(x) Fn(par, x) ) On Jun 16, 2009, at 11:08 AM, Stu @ AGS wrote:> Hello! > > > > I am trying to write a function with vector and data.frame > parameters that > uses the sum() function and values from the rows of the data.frame. > > I need to pass this function as a parameter to optim(). > > > > My starting point is: > > observs <- data.frame(y, x1, x2, x3) > > > > Fn <- function(par, observs) { > > sum( (y - (par[1] * (x1 + 1) * x2^(-par[2]) * > x3^par[3])^2 ) > > } > > > > y, x1, x2, x3 are all vectors. > > > > I am a bit new to R and I have not been able to find a good > description of > how to iterate over rows in a data.frame. > > > > What is a straightforward way to do this? > > What am I missing? > > > > Thanks > > Stu > > > > > [[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.David Winsemius, MD Heritage Laboratories West Hartford, CT
Patrick, Thanks for your suggestion! The R-Inferno was especially useful!! The first chapter had me chuckling aloud despite the fact that I work alone. Well worth the price! Thanks! Stu> -----Original Message----- > From: Patrick Burns [mailto:pburns at pburns.seanet.com] > Sent: Tuesday, June 16, 2009 11:31 AM > To: Stu @ AGS > Subject: Re: [R] The most straightfoward way to write a function that > sums over the rows of a matrix > > 'The R Inferno' may be of use to you. > > > > Patrick Burns > patrick at burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of "The R Inferno" and "A Guide for the Unwilling S User") > >