I have a dataframe that includes the lower and upper bound of a few parameters for each category of fruit. It looks sth like this:
+----------+-----------+-------+-------+
| Category | Parameter | Upper | Lower |
+----------+-----------+-------+-------+
| Apple | alpha | 10 | 20 |
+----------+-----------+-------+-------+
| Apple | beta | 20 | 30 |
+----------+-----------+-------+-------+
| Orange | alpha | 10 | 20 |
+----------+-----------+-------+-------+
| Orange | beta | 30 | 40 |
+----------+-----------+-------+-------+
| Orange | gamma | 50 | 60 |
+----------+-----------+-------+-------+
| Pear | alpha | 10 | 30 |
+----------+-----------+-------+-------+
| Pear | beta | 20 | 40 |
+----------+-----------+-------+-------+
| Pear | gamma | 20 | 30 |
+----------+-----------+-------+-------+
| Banana | alpha | 40 | 50 |
+----------+-----------+-------+-------+
| Banana | beta | 20 | 40 |
+----------+-----------+-------+-------+
I would like to write a function that:
- Input is 1 fruit name
function("Apple")
- Extracts the upper & lower values of all the parameters of this fruit
- Feed the upper and lower bound for alpha, beta, and gamma (if applicable) of selected fruit into the following process to make one dataframe:
param_grid_[fruit_name] <- expand.grid(alpha = seq(lower, upper, length.out = 100),
beta = seq(lower, upper, length.out = 100),
gamma = seq(lower, upper, length.out = 100))
- gamma is applicable only if the fruit has gamma parameter in the original table
For example, if my input to the function is "Apple", then I should end up having:
param_grid_Apple <- expand.grid(alpha = seq(10, 20, length.out = 100),
beta = seq(20, 30, length.out = 100))
For example, if my input to the function is "Pear", then I should end up having:
param_grid_Pear <- expand.grid(alpha = seq(10, 30, length.out = 100),
beta = seq(20, 40, length.out = 100),
gamma = seq(20, 30, length.out = 100))
I have tried directly subsetting the row & col index. For example, for Apple's upper alpha, I would do df[2,3]. But this is a rather manual & unsophisticated way to do this. I am wondering if I could wrap everything in a function to streamline this process.
Still a beginner in R and trying to learn ways to streamline procedures by writing functions. Much appreciation for any help!
P.S. (FYI - maybe not be directly related to the center issue of this post) I am doing this so that I can feed param_grid into nls2 function to fit a curve for each fruit:
nls2(formula = ...,
data = ...,
start = param_grid,
algorithm = "brute-force",
control = nls.control(maxiter = 1e4))