[R] iterative using values from a data frame to parameterize a function
David Winsemius
dwinsemius at comcast.net
Sat Jul 30 16:22:09 CEST 2011
On Jul 30, 2011, at 5:13 AM, Dennis Murphy wrote:
> 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
If there were multiple species (and we cannot tell in the absence of a
reproducible example) you may need to add a match() operation to
select the proper row of coefficients and exponents in "dfm" on the
basis of the species value in each line of input of x.
--
David.
>
> 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.
>>
>
> ______________________________________________
> 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
West Hartford, CT
More information about the R-help
mailing list