1

[ In the program file I am using, I would like to have a translation of the variables in the program to the explanation of what the variables mean. I have the list of variables and their corresponding definitions. I also have the formulas, sample provided. The output I would like would be the translation of the variable in the formulas (provided in the example) to the explanations of what the variables represent. 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))
3
  • 4
    Please give a reproducible example. Data in images is not very helpful, if I want to run some code, you are now asking me to type all that from an image.
    – Axeman
    Commented Jul 19 at 17:44
  • 4
    Do not post images of code and/or data. Use dput() on your data frame and copy-paste the output to your post. This will make it more accessible.
    – LMc
    Commented Jul 19 at 17:49
  • No one can tell whether you have a column of formulas/language, or strings. Give he dput of your data
    – Onyambu
    Commented Jul 19 at 18:49

2 Answers 2

3

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 which

  • inputs and converts a character vector of expressions, expr, to language objects, expr2
  • converts the first two columns of the data frame, DF, to a list of names, defs2
  • performs the substitutions using substitute
  • converts back to a character vector using deparse1
  • removes backticks the above process generates
  • converts "NA" to NA

Thus using the input in the Note at the end

subst <- function(expr, defs) {
  expr2 <- lapply(expr, str2lang)
  defs2 <- defs[1:2] |> deframe() |> as.list() |> lapply(as.name)
  s <- sapply(expr2, \(expr) deparse1(do.call(substitute, list(expr, defs2))))
  s <- gsub("`", "", s)
  ifelse(s == "NA", NA, s)
}

# test run
DF %>%
  mutate(translation = subst(formula, pick(variable, definition)))

giving

# A tibble: 6 × 4
  variable definition      formula        translation                           
  <chr>    <chr>           <chr>          <chr>                                 
1 cs       cost of salad   cs=cb+ccel+cc  cost of salad = cost of bread + cost …
2 csp      cost of soup    csp=cc+cel+crt cost of soup = cost of chicken + cel …
3 cb       cost of bread   <NA>           <NA>                                  
4 cc       cost of chicken <NA>           <NA>                                  
5 ccel     cost of celery  <NA>           <NA>                                  
6 ccrt     cost of carrot  <NA>           <NA>                                  

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.

library(gsubfn)

subst2 <- function(expr, defs) {
  defs2 <- defs[1:2] |> deframe() |> as.list()
  gsubfn("\\w+", defs2, expr) 
}
 
DF |>
  mutate(translation = subst2(formula, pick(variable, definition)))

Note

library(dplyr)
library(tibble)

DF <- tibble(
  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+cel+crt", NA, NA, NA, NA),
)
5
  • Why is the conversion necessary? Why can't this be done with regular strings? Commented Jul 19 at 19:42
  • I'm afraid I'm not following your explanation. Commented Jul 19 at 19:44
  • Well, I was curious to learn about how you selected the first approach Commented Jul 19 at 20:15
  • Right, but these aren't actual formulas, in the R sense, right? They're just strings needing substitution that happen to have =s and +s in them. Unless I'm missing your point? Commented Jul 19 at 20:50
  • I like it. Do you need any "debugging details" from me?
    – Stephen
    Commented Aug 4 at 16:51
1

This can be done with str_replace_all from the stringr package.

Your data contains two separate sections, variables+definitions, and formulas.

Sort the data frame on the number of characters in variable, descending.

library(dplyr)
library(stringr)

df <- arrange(df, -nchar(variable))

Create a named vector of the variables to use as the pattern argument.

pattern <- setNames(df$definition, df$variable)

forms <- as.list(na.omit(df$formulas)
forms
#[[1]]
#[1] "csp=cc+ccel+crt"

#[[2]]
#[1] "cs=cb+ccel+cc"

lapply(forms, \(x) str_replace_all(x, pattern))
#[[1]]
#[1] "cost of soup=cost of chicken+cost of celery+cost of carrot"

#[[2]]
#[1] "cost of salad=cost of bread+cost of celery+cost of chicken"

Data

df <- structure(list(variable = c("ccel", "ccrt", "csp", "cs", "cb", 
"cc"), definition = c("cost of celery", "cost of carrot", "cost of soup", 
"cost of salad", "cost of bread", "cost of chicken"), formula = c(NA, 
NA, "csp=cc+ccel+ccrt", "cs=cb+ccel+cc", NA, NA), Translation = c(NA, 
NA, "cost of soup=cost of chicken+cost of celery+cost of carrot", 
"cost of salad=cost of bread+cost of celery+cost of chicken", 
NA, NA)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

Not the answer you're looking for? Browse other questions tagged or ask your own question.