How to build a custom function that uses externall

2019-07-29 04:02发布

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

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: I'm a beginner in programming and R so please forgive me for my probably relatively unprecise description or if forgot something.)

1条回答
Summer. ? 凉城
2楼-- · 2019-07-29 04:37

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

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

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
查看更多
登录 后发表回答