[R] iterative using values from a data frame to parameterize a function

Dennis Murphy djmuser at gmail.com
Sat Jul 30 11:13:01 CEST 2011


Hi:

Try this:

set.seed(266)
dfm <- data.frame(c1 = sample(1:5, 10, replace = TRUE),
                  e1 = sample(1:3, 10, replace = TRUE),
                  c2 = sample(1:5, 10, replace = TRUE),
                  e2 = sample(1:3, 10, replace = TRUE),
                  c3 = sample(1:5, 10, replace = TRUE),
                   x = 1:10)

f <- function(d) with(d, c1^e1 + c2 * x^e2 + c3)
f(dfm)
[1]  70   6 172  86  15 173 254 158  19 505

HTH,
Dennis

On Sat, Jul 30, 2011 at 1:06 AM, Benjamin Caldwell
<btcaldwell at berkeley.edu> wrote:
> Hello,
>
> I'm just trying to wrap my head around the syntax for creating loops,
> functions in R. I have an array of values from a .csv. Looks something like
>
> header<-c(species,coefficient1, exponent1, coefficient2, exponent2,
> constant)
>
> with a species name for the first column, and values for coefficient1,
> exponent1, coefficient2, exponent2, constant for each species.
>
> The values are parameters for an equation
>
> V<-((coefficient1*(x)^exponent1)+(coefficient2*(x)^exponent2)+constant)
>
> x<1:100
>
> I'd like to run the equation using the parameters for each species and the
> vector x to simulate what V would be for the range of values x, and then
> plot a graph of for each species.
>
> Would the way to do this be some sort of apply nested within a for loop?
> Code so far looks like
>
> vol<-read.csv("frame.csv")
>
> vol.exp<-function(coefficient1, exponent1, coefficient2, exponent2,
> constant,x) {
> V<-((coefficient1*(x)^exponent1)+(coefficient2*(x)^exponent2)+constant)
> V
> }
> x<1:100
>
> Thanks
>
> *Ben *
>
>        [[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.
>



More information about the R-help mailing list