# How to substitute the same variable with hundreds of different values in different rows

Hi All!
i am stuck with a simple problem i think, i just don't know with function to use. let me paste my reprex here:

``````df<-tibble::tribble(
~Specie,                                                               ~Equazione,  ~CF,   ~Ro,
"Abies alba",                            "TB=(-2.1386+1.8125*10^-2*D^2*H+1.1089*D)*CF", 0.32, 0.416,
"Abies alba",                             "TB=(-2.7916+0.034492*D^2*H+0.08354*D^2)*CF", 0.32, 0.416,
"Abies alba",                                      "TB=(6.3193+0.0286*ln(D^2*H^2))*CF", 0.32, 0.416,
"Acacia auriculiformis",                      "TB=CF*exp(-2.02754+1.726791*ln(D)+0.777652*ln(H))", 0.26, 0.582,
"Acacia leptocarpa",                                    "TB=(5.066*D-0.696*D^2+0.053*D^3)*CF", 0.26, 0.693,
"Acacia mangium",                                          "TB=CF*exp(-1.073+2.081*ln(D))", 0.26,   0.5,
"Acacia mangium",                                           "TB=-4.606*D^2.138*H^1.089*CF", 0.26,   0.5,
"Acacia melanoxylon",                   "TB=CF*exp((-2.6127+2*1.1265*ln(D)+0.1894*ln(H))*1.3)", 0.26, 0.568,
"Acacia polyacantha",                                   "TB=CF*exp(2.95854+0.21750*ln(D^2*H))", 0.42, 0.671,
"Acer campestre",                                      "TB=(6.4595+2.6368*10^-2*D^2*H)*CF", 0.26, 0.525,
"Acer pseudoplatanus",                                      "TB=(6.4595+2.6368*10^-2*D^2*H)*CF", 0.26, 0.508,
"Acer pseudoplatanus",                                           "TB=(-2.7018+2.5751*ln(D))*CF", 0.26, 0.508,
"Acrocarpus fraxinifolius",                      "TB=(-16218+18.8969*(3.14*(D/2)/2)/10000)*?*CF*1.3", 0.42, 0.503,
"Afrocarpus falcatus",                                             "TB=-0.125+exp(1.785*ln(D))", 0.32, 0.426,
"Afzelia africana",                                            "TB=CF*exp(-3.28+2.74*ln(D))", 0.42, 0.693,
"Afzelia pachyloba",                                                    "TB=CF*?*9.72*D^2.46", 0.42, 0.683,
"Afzelia quanzensis",                                              "TB=3.1256*D^1.5833*CF*1.3", 0.42, 0.715,
"Albizia coriaria",                                          "TB=CF*0.0164*(?*D^2*H)^1.0906", 0.42,  0.56,
"Albizia lebbeck",                                               "TB=(-0.7516+1.3805*D)*CF", 0.42, 0.613,
"Albizia niopoides", "TB=(0.054*(D*(0.9590+1.0682*(0.1/H)-0.0473*(0.1/H)^2)^1/2)^2*0.121)*CF", 0.42,  0.64,
"Alnus glutinosa",                         "TB=(-1.6747*10+1.7930*10^-2*D^2*H+2.6664*D)*CF", 0.26, 0.419,
"Alnus glutinosa",                                                "TB=(0.0859*D^2.3537)*CF", 0.26, 0.419,
"Anacardium occidentale",                           "TB=CF*exp(-2.6516+1.9741*ln(D)+0.7169*ln(H))", 0.42, 0.446,
"Anacardium occidentale",                                       "TB=(0.3152*D^1.7722*H^0.5003)*CF", 0.42, 0.446,
"Annona diversifolia",                                          "TB=CF*exp(-2.772+2.562*ln(D))", 0.42, 0.426,
"Annona muricata",                                         "TB=exp((-2.134+2.53*ln(D))*CF)", 0.42,  0.36,
"Annona reticulata",             "TB=(exp(-0.37+0.333*ln(D)+0.933*ln(D^2)-0.122*ln(D^3)))*CF", 0.42,  0.55,
"Aquilaria crassna",                                                 "TB=(24.096+1.503*D)*CF", 0.42,  0.34
)

df\$Equazione <- gsub("?",df\$Ro,df\$Equazione)
#> Warning in gsub("?", df\$Ro, df\$Equazione): l'argomento 'replacement' ha
#> lunghezza > 1 e solo il primo elemento sarà usato

df\$Equazione <- sub("CF",df\$CF,df\$Equazione)
#> Warning in sub("CF", df\$CF, df\$Equazione): l'argomento 'replacement' ha
#> lunghezza > 1 e solo il primo elemento sarà usato
``````

Created on 2022-06-16 by the reprex package (v2.0.1)

df contains a list of 200 equations in the first column (each one specific to a tree specie), that includes several variable each, and in the second and third columns the numeric value for one specific variable for those equations (variable that most equations contains, but not all, in this case wood density -Ro and the correction factor CF). What i want is to simply substitute the variable character in the equation string with the actual specie-specific value of the variable. i have tried with sub, gsub, replacer (qdap), substring or similars, but they all replace with text or fixed strings it seems. I just need to replace a fixed variable in the equations with the different corresponding values sitting next to it in the row.
Someone can help? thanks you a lot..
Michel

the excel is not a part of issue you need help with, so is irrelevant to your posting.
the result of reading the excel with read_xlsx is critical. i.e. the `df` object you made.

Sharing that (`df`), or part of that, or a facsimile of that, would make a reprex. A reprex will help others help you.

Here is a link to the guide.

Hi nirgrahamuk, thanks for helping me out. i used datapaste, hope can help a little!

example of replacing CF in the equation string with the value of CF for that row

``````
library(tidyverse)

mutate(df,
Equazione=str_replace_all(Equazione,
"CF",
as.character(CF))
)``````

Thanks a lot nirgrahamuk! so it's mutate than can help me..
i tried and i see it working on the R studio console ,
... But when i write the file, the mutate effects are not shown (nor are they in the df file in the same R Studio)
Should i give some other command to make them showable in the csv output?

in general in R when you make a change to an object, theres a seperate step to record the result into a name so it can be used later, like written to a file.
The assignment operator in R is the arrow `<-`

``````df2  <- mutate(df,
Equazione=str_replace_all(Equazione,
"CF",
as.character(CF))
)
``````

i.e. put the result of the mutate with str_replacement into an object called df2, its df2 that you might write out as a csv if you want that version written out.

Thanks again, sorry for my noobism.
Glad that a selfless person took the time to help...
may the force be with you! This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.