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

编程入门 行业动态 更新时间:2024-10-26 14:40:45
本文介绍了如何使用数据框中的变量创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我确定这个问题有点假(对不起)...我正在尝试使用我存储在Dataframe中的differents变量创建一个函数.函数是这样的:

I'm sure the question is a bit dummy (sorry)... I'm trying to create a function using differents variables I have stored in a Dataframe. The function is like that:

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)

问题是当我尝试在数据框(具有相同变量名称)中运行函数时.它不会检测到我的变量并显示以下消息:

The problem is when I try to run my function in a dataframe (with the same name of variables). It doesn't detect my variables and shows this message:

Error in mlr_turb(dataset) : argument "Flow_in" is missing, with no default

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

But, actually, there is, also all the variables.

我认为我在函数中错位或缺少某些顺序,这使它有可能从数据集中获取变量.我对此进行了很多搜索,但没有找到任何答案...

推荐答案

在编写与标准评估(SE)与非标准评估(NSE).如果需要更多元素,可以查看我写的这篇博客文章

You meet a standard problem when writing R that is related to the question of standard evaluation (SE) vs non standard evaluation (NSE). If you need more elements, you can have a look at this blog post I wrote

我认为使用变量编写函数的最便捷方法是使用变量名作为函数的参数.

I think the most convenient way to write function using variables is to use variable names as arguments of the function.

让我们再次以@Muon为例.

Let's take again @Muon example.

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

问题是 R 在哪里应该找到名称 x , y 和 z 后面的值.在函数中, R 首先将在函数环境中查看(此处 x , y 和 z 被定义为参数),则它将查看全局环境,然后将查看附加的不同程序包.

The question is where R should find the values behind names x, y and z. In a function, R will first look within the function environment (here x,y and z are defined as parameters) then it will look at global environment and then it will look at the different packages attached.

在 myFun 中, R 需要向量.如果输入列名,则会遇到错误.如果要提供列名会怎样?您必须对 R 说,您提供的名称应与数据框范围内的值相关联.例如,您可以执行以下操作:

In myFun, R expects vectors. If you give a column name, you will experience an error. What happens if you want to give a column name ? You must say to R that the name you gave should be associated to a value in the scope of a dataframe. You can for instance do something like that:

myFun <- function(df, col1 = "x", col2 = "y", col3 = "z"){ result <- (df[,col1] + df[,col2])/df[,col3] return(result) }

您可以使用 data.table 软件包在这方面走得更远.如果您开始编写需要使用数据框中的变量的函数,建议您开始看看此程序包

You can go far further in that aspect with data.table package. If you start writing functions that need to use variables from a dataframe, I recommend you to start having a look at this package

更多推荐

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

本文发布于:2023-07-20 17:43:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   变量   框中   函数   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!