I have the list of variables and their corresponding definitions. I also have the formulas, provided, but I would like the translation of the formulas (provided in the example) to better understand what the formula is saying. My example is simplistic, but I typically have about 100 variables and 100 formulas.
I tried to find similar questions but could not find the answer.
structure(list(variable = c("cs", "csp", "cb", "cc", "ccel",
"ccrt"), definition = c("cost of salad", "cost of soup", "cost of bread",
"cost of chicken", "cost of celery", "cost of carrot"), formula = c("cs=cb+ccel+cc",
"csp=cc+ccel+crt", NA, NA, NA, NA), Translation = c("cost of salad=cost of bread+cost of celery+cost of chicken",
"cost of soup=cost of chicken+cost of celery+cost of carrot",
NA, NA, NA, NA)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-6L))
Best Answer
1) Using language objects. It is common in R to convert expressions to language objects and then process them.
To do that define a function
subst
whichThus using the input in the Note at the end
giving
2) We could alternately use gsubfn to perform the substitutions. We assume that the variables consist entirely of word characters. This gives the same result.
Note