[R] The most straightfoward way to write a function that sums over the rows of a matrix
Ronggui Huang
ronggui.huang at gmail.com
Tue Jun 16 17:12:07 CEST 2009
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
More information about the R-help
mailing list