[R] Evaluating a formula
Chel Hee Lee
chl948 at mail.usask.ca
Fri Jan 16 17:48:40 CET 2015
You may use 'eval()' with 'parse()' if you wish to use 'formula()' as below:
> a <- params[[1]]
> b <- params[[2]]
> eval(parse(text=func1))
[1] 5.000000 5.652893 6.338843 7.057851 7.809917 8.595041
[7] 9.413223 10.264463 11.148760 12.066116 13.016529 14.000000
[13] 15.016529 16.066116 17.148760 18.264463 19.413223 20.595041
[19] 21.809917 23.057851 24.338843 25.652893 27.000000 28.380165
[25] 29.793388 31.239669 32.719008 34.231405 35.776860 37.355372
[31] 38.966942 40.611570 42.289256 44.000000 45.743802 47.520661
[37] 49.330579 51.173554 53.049587 54.958678 56.900826 58.876033
[43] 60.884298 62.925620 65.000000 67.107438 69.247934 71.421488
[49] 73.628099 75.867769 78.140496 80.446281 82.785124 85.157025
[55] 87.561983 90.000000 92.471074 94.975207 97.512397 100.082645
[61] 102.685950 105.322314 107.991736 110.694215 113.429752 116.198347
[67] 119.000000 121.834711 124.702479 127.603306 130.537190 133.504132
[73] 136.504132 139.537190 142.603306 145.702479 148.834711 152.000000
[79] 155.198347 158.429752 161.694215 164.991736 168.322314 171.685950
[85] 175.082645 178.512397 181.975207 185.471074 189.000000 192.561983
[91] 196.157025 199.785124 203.446281 207.140496 210.867769 214.628099
[97] 218.421488 222.247934 226.107438 230.000000
>
Or, you can define your own function as below:
> fn <- function(x, a, b)a*x^2 + b*x
> fn(x=x, a=params[[1]], b=params[[2]])
[1] 5.000000 5.652893 6.338843 7.057851 7.809917 8.595041
[7] 9.413223 10.264463 11.148760 12.066116 13.016529 14.000000
[13] 15.016529 16.066116 17.148760 18.264463 19.413223 20.595041
[19] 21.809917 23.057851 24.338843 25.652893 27.000000 28.380165
[25] 29.793388 31.239669 32.719008 34.231405 35.776860 37.355372
[31] 38.966942 40.611570 42.289256 44.000000 45.743802 47.520661
[37] 49.330579 51.173554 53.049587 54.958678 56.900826 58.876033
[43] 60.884298 62.925620 65.000000 67.107438 69.247934 71.421488
[49] 73.628099 75.867769 78.140496 80.446281 82.785124 85.157025
[55] 87.561983 90.000000 92.471074 94.975207 97.512397 100.082645
[61] 102.685950 105.322314 107.991736 110.694215 113.429752 116.198347
[67] 119.000000 121.834711 124.702479 127.603306 130.537190 133.504132
[73] 136.504132 139.537190 142.603306 145.702479 148.834711 152.000000
[79] 155.198347 158.429752 161.694215 164.991736 168.322314 171.685950
[85] 175.082645 178.512397 181.975207 185.471074 189.000000 192.561983
[91] 196.157025 199.785124 203.446281 207.140496 210.867769 214.628099
[97] 218.421488 222.247934 226.107438 230.000000
>
Is this what you are looking for? I hope this helps.
Chel Hee Lee
On 01/16/2015 02:16 AM, philippe massicotte wrote:
> Hi all.
>
> How we evaluate a formula in R?
>
> Ex.:
>
> params <- list(a = 2, b = 3)
> x <- seq(1,10, length.out = 100)
>
> func1 <- as.formula("y ~ a*x^2 + b*x")
>
> ##How to evaluate func1 using x and the params list
> ???
>
>
> Thank you in advance,
> Phil
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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