dawei
dawei

Reputation: 57

How to pass multiple columns into uniroot as parameters

My task now is to solve a equation like eqn(a,b,c), from which a, b, c each is a column from a data. How to pass these columns to uniroot to solve roots at the same? I guess maybe sapply is useful here.

As alistaire suggested, I paste the details as followed:

the equation is like:

     eqnt <- function(T,l,fn,m,EI) { T - fn^2*m/l^2 - EI }

and Now the parameters are all (l,fn,m,EI) loaded into a dataframe, say df. I intend to solve all the equations with the parameters in df.

    L        m       EI       fn
 1 10 6.190004 9988.997  6.59710
 2 10 6.190004 9988.997  8.01847
 3 10 6.190004 9988.997  9.21858
 4 15 6.190004 9988.997 10.27676
 5 15 6.190004 9988.997 11.23391
 6 15 6.190004 9988.997 12.11441

How coud I?

When I tried followed code, defining a function and using mapply:

rootfun <- function(l,fn,m,EI){
     uniroot.all(eqnt,l,fn,m,EI,lower=0.8e5, upper=3.5e5,
                 interval=c(0.8e5,3.8e5))
 }

 res <- mapply(rootfun,l=data$L,fn=data$f.4,m=data$m,EI=data$EI)

the error arises:

 Error in f(xseq, ...) (from #8) : argument "mm" is missing, with no default

Upvotes: 0

Views: 348

Answers (1)

Dom
Dom

Reputation: 1053

You could use pmap but without some data to work on its hard to answer your question. See for example:

library(purrr)

df <- data.frame(
  x = c("apple", "banana", "cherry"),
  pattern = c("p", "n", "h"),
  replacement = c("x", "f", "q"),
  stringsAsFactors = FALSE
)
pmap(df, gsub)

Explanation - name your data you need to loop over in-line with the inputs of your function (in this case uniroot). Pass the data frame to pmap along with the function.

Upvotes: 1

Related Questions