如何构建使用外部定义的值和 R 中的字符串条件的自定义函数 [英] How to build a custom function that uses externally defined values with a string condition in R

查看:13
本文介绍了如何构建使用外部定义的值和 R 中的字符串条件的自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个用于计算单个数值变量 (double) 的函数.它应该从另一个数据框中获取它的组件,该数据框存储不同的方程,这些方程被分解成单个部分(我在这里使用线性回归方程,所以它是关于两个变量/列的斜率和截距).根据存储在方程表中的一个条件(名称/特定字符串),该函数应使用同一行的斜率和截距.

I'm working on a function for calculations of a single numeric variable (double). It should takes it's components from another data frame that stores different equations which are broken up into their single pieces (I use linear regression equations here so it's about the two variables/columns slope and intercept). Depending on one condition (a name/specific string) which is stored in the equations table as well the function should use the slope and intercept from the same row.

计算的实际输入数据来自存储在数字列中的另一个数据框.

The actual input data for the calculations comes from another dataframe stored in a numeric column.

背景:每个条件都需要不同的方程,而且有太多的方程无法将它们合并为单个函数.

The background: every condition requires a different equation and there are too many to come them into single functions.

我猜这个函数应该遵循这个基本方案:

I guess the function should follow this basic scheme:

data_conversion(numeric_input_data, "equation_id")

尝试在网上找到解决方案后,我尝试了 apply-、subset-、ifelse- 和 switch-函数的形式,但都没有成功.

After trying to find a solution online, I experimented with forms of the apply-, subset-, ifelse- and switch-functions but was not successfull.

最后,我希望有一个简单的方法,如果可能的话,尽量避免循环等.

At the end I would appreciate a simple way, trying to avoid loops etc. if possible.

#create dataframe with equation parameters
equation_id <- c("eq_1", "eq_2", "eq_3", "eq_4", "eq_5")
slope <- c(1.1627907, 1.6949153, 1.2658228, 0.9345794, 0.9433962)
intercept <- c(-26.4069767,  -0.4067797, -27.3544304, -21.2336449, -22.9245283)
eq_df <- data.frame(equation_id, slope, intercept) 

#create some test data
group <- c("A", "B", "C", "A")
iso_value_p <- c(14, 12, NA, 13.5)
data_df <- data.frame(group, iso_value_p) 

#function [not working]; using iso_value as input for x
data_conversion <- function (x, choose_equation) {
  switch(choose_equation,
        eq_df[eq_df$equation_id == choose_equation, ] = { 
        res <- eq_df$slope * x + eq_df$intercept 
    }
  )
  return(res)
}

函数应该这样工作:

#for the first data row and the first equation
data_conversion(14.0, "eq_1")

#which should go like
1.1627907 * 14.0 + (- 26.4069767)

#result:
[1] -10.12791

#if I choose the second equation: 
data_conversion(14.0, "eq_2")

#which should go like
1.6949153 * 14.0  + (-0.4067797)

#should give:
[1] 23.32203

####and using the whole dataset togehter with "eq_1" should give:
data_conversion(iso_value_p , "eq_1")
[1] -10.127907  -12.45349  NA  -10.709302

但我没有设法让代码工作 - 上面的例子只是从单个值的手动"计算中组合而成的.

But I did not manage to get the code working - the examples above are just assembled from 'manual' calculations of single values.

(PS:我是编程和 R 的初学者,所以请原谅我可能相对不准确的描述或者如果忘记了什么.)

(PS: I'm a beginner in programming and R so please forgive me for my probably relatively imprecise description or if forgot something.)

推荐答案

只要环境中存在eq_df,我们就可以创建一个函数

Provided that eq_df is present in the environment, we can create a function

data_conversion <- function(x, choose_equation) {
   inds <- eq_df$equation_id %in% choose_equation
   eq_df$slope[inds] * x + eq_df$intercept[inds]
}

data_conversion(14.0, "eq_1")
#[1] -10.12791
data_conversion(14.0, "eq_2")
#[1] 23.32203
data_conversion(iso_value_p , "eq_1")
#[1] -10.12791 -12.45349        NA -10.70930

如果您将两个方程一起传递,这也将起作用.结合上面的 1) 和 2)

This will also work if you pass two equations together. Combining 1) and 2) from above

data_conversion(14.0, c("eq_1", "eq_2"))
#[1] -10.12791  23.32203

但是,最好在函数中将数据帧eq_df作为参数传递

However, it is better if we pass dataframe eq_df in the function as a parameter

data_conversion <- function(eq_df, x, choose_equation) {
   inds <- eq_df$equation_id %in% choose_equation
   eq_df$slope[inds] * x + eq_df$intercept[inds]
}

data_conversion(eq_df, 14.0, "eq_1")
#[1] -10.12791

这篇关于如何构建使用外部定义的值和 R 中的字符串条件的自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆