[R] formula substitution by its calculated value

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 30 16:56:38 CET 2007


Try this:

> a <- 1
> b <- 2
> mat <- matrix(c("a", "a*b", "a+b", "b"), 2)
> mat
     [,1]  [,2]
[1,] "a"   "a+b"
[2,] "a*b" "b"
> matrix(sapply(mat, function(x) eval(parse(text = x))), nrow(mat))
     [,1] [,2]
[1,]    1    3
[2,]    2    2


On Nov 30, 2007 10:06 AM, BOISSON, Pascal <Pascal.BOISSON at veolia.com> wrote:
> Dear R_Users,
>
> I am confronted to a technical issue that I wouldn't even know how to start to handle and where to look at. It is about data manipulation in a dataframe and value substitution
> Imagine I have a matrix describing a set of chemical equations.
> Colnames are Components names and Rownames are Reaction names
>
> Each cell in that matrix corresponds to the stoechiometric coefficient to the pair (Reaction, Component). It would be easy to deal with this if these cells where filled with numbers (int or real) but often, it refers to some parameters name or even to formulae.
>
> I'd like to transform these formulae into their calculated values.
> eg:
> if I have existing R objects :
> f_ox <- 0.2
> f_inox <- 0.5
>
> and in my matrix :
> cell (Oxydation, substrate) = f_ox*(1-f_inox)
>
> I want to replace f_ox*(1-f_inox) by 0.1
>
> Further more, I can not manage to understand the formula type, which I guess would correspond to the kind of things I am dealing with.
>
> Would you have any clue or insights on this?
>
> Pascal Boisson
>
> Assainissement - Traitement Biologique
> Veolia Environnement - Centre de Recherche sur l'Eau
> Anjou Recherche
> Chemin de la Digue - BP 76
> 78603 Maisons Laffitte Cedex
> Tel. : +33 (0)1 34 93 81 44
> Fax. : +33 (0)1 34 93 31 10
> pascal.boisson at veolia.com
>
> Please consider your environmental responsibility before printing this e-mail
>
> ___________________________________________________________________________________
>
> Protegeons ensemble l'environnement : avez-vous besoin d'imprimer ce courrier electronique ?
> ___________________________________________________________________________________
>
> Les informations figurant sur cet e-mail ont un caractere strictement confidentiel et sont exclusivement adressees au destinataire mentionne ci-dessus.Tout usage, reproduction ou divulgation de cet e-mail est strictement interdit si vous n'en etes pas le destinataire. Dans ce cas, veuillez nous en avertir immediatement par la meme voie et detruire l'original. Merci
>
> This e-mail is intended only for use of the individual or entity to which it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law.
> Any use, distribution or copying of this e-mail communication is strictly prohibited if you are not the addressee. If so, please notify us immediately by e-mail, and destroy the original. Thank you.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list