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

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

问题描述

我正在研究一个用于计算单个数字变量(双精度)的函数.它应该从另一个数据框中获取其组成部分,该数据存储有分解成单个部分的不同方程式(我在这里使用线性回归方程式,因此它涉及两个变量/列的斜率和截距). 根据存储在方程式表中的一个条件(名称/特定字符串),函数还应使用斜率并从同一行截取.

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)
}

该功能应以这种方式工作:

The function should work this way:

#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 assambled from 'manual' calculations of single values.

(PS:我是R和R的编程初学者,所以请原谅我可能不太准确的描述,或者如果忘记了某些内容.)

(PS: I'm a beginner in programming and R so please forgive me for my probably relatively unprecise 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天全站免登陆