gpt4 book ai didi

r - 如何使用数据框中的变量创建函数

转载 作者:行者123 更新时间:2023-12-03 16:21:42 25 4
gpt4 key购买 nike

我确定这个问题有点愚蠢(抱歉)......我正在尝试使用我存储在 Dataframe 中的不同变量来创建一个函数。功能是这样的:

mlr_turb <- function(Cond_in, Flow_in, pH_in, pH_out, Turb_in, nm250_i, nm400_i, nm250_o, nm400_o){

Coag = (+0.032690 + 0.090289*Cond_in + 0.003229*Flow_in - 0.021980*pH_in - 0.037486*pH_out
+0.016031*Turb_in -0.026006*nm250_i +0.093138*nm400_o - 0.397858*nm250_o - 0.109392*nm400_o)/0.167304

return(Coag)
}

m4_turb <- mlr_turb(dataset)

问题是当我尝试在数据帧(具有相同名称的变量)中运行我的函数时。它不会检测我的变量并显示此消息:
Error in mlr_turb(dataset) : 
argument "Flow_in" is missing, with no default


但是,实际上,还有所有变量。

我想我在函数中遗漏了或遗漏了一些顺序,这使它有可能从数据集中获取变量。我对此进行了很多搜索,但没有找到任何答案...

最佳答案

没有愚蠢的问题!

我认为您正在寻找 do.call .此函数允许您将值作为参数解压缩到函数中。这是一个非常简单的例子。

# a simple function that takes x, y and z as arguments 
myFun <- function(x, y, z){
result <- (x + y)/z
return(result)
}

# a simple data frame with columns x, y and z
myData <- data.frame(x=1:5,
y=(1:5)*pi,
z=(11:15))

# unpack the values into the function using do.call
do.call('myFun', myData)

输出:
[1] 0.3765084 0.6902654 0.9557522 1.1833122 1.3805309

关于r - 如何使用数据框中的变量创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61208406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com