如何将多列作为参数传递给 uniroot
How to pass multiple columns into uniroot as parameters
我现在的任务是求解像 eqn(a,b,c)
这样的方程式,其中 a、b、c 各是数据中的一列。如何将这些列传递给 uniroot
以同时求解根?我想也许 sapply
在这里很有用。
按照 alistaire 的建议,我将详细信息粘贴如下:
等式如下:
eqnt <- function(T,l,fn,m,EI) { T - fn^2*m/l^2 - EI }
现在参数全部 (l,fn,m,EI) 加载到数据帧中,比如 df
。我打算用 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
我怎么能?
当我尝试遵循代码时,定义一个函数并使用 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)
出现错误:
Error in f(xseq, ...) (from #8) : argument "mm" is missing, with no default
你可以使用 pmap 但没有一些数据来解决它很难回答你的问题。参见示例:
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)
说明 - 将需要循环的数据命名为 in-line,并使用函数的输入(在本例中为 uniroot
)。将数据框与函数一起传递给 pmap。
我现在的任务是求解像 eqn(a,b,c)
这样的方程式,其中 a、b、c 各是数据中的一列。如何将这些列传递给 uniroot
以同时求解根?我想也许 sapply
在这里很有用。
按照 alistaire 的建议,我将详细信息粘贴如下:
等式如下:
eqnt <- function(T,l,fn,m,EI) { T - fn^2*m/l^2 - EI }
现在参数全部 (l,fn,m,EI) 加载到数据帧中,比如 df
。我打算用 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
我怎么能?
当我尝试遵循代码时,定义一个函数并使用 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)
出现错误:
Error in f(xseq, ...) (from #8) : argument "mm" is missing, with no default
你可以使用 pmap 但没有一些数据来解决它很难回答你的问题。参见示例:
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)
说明 - 将需要循环的数据命名为 in-line,并使用函数的输入(在本例中为 uniroot
)。将数据框与函数一起传递给 pmap。