What you're are describing in a vector of character strings
containing expressions. It doesn't make sense to try to
coerce them to double without evaluating them. I'm not
completely clear on your intent, but this example may point you to
where you'd need to look to do what you want:
exprs = c('(1+s) / (1+k) ;','(1+(2*s)^2) / (1+k)^2 ;')
s = 4
sapply(1:10,function(x){k=x;eval(parse(text=exprs[1]))})
[1] 2.5000000 1.6666667 1.2500000 1.0000000 0.8333333 0.7142857 0.6250000
[8] 0.5555556 0.5000000 0.4545455
or perhaps
expr1 = function(k,s){k=k;s=s;eval(parse(text=exprs[1]))}
expr1(3,2)
[1] 0.75
expr1(10,5)
[1] 0.5454545
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spec...@stat.berkeley.edu
On Sun, 27 Feb 2011, LucasM wrote:
Hello,
I try to plot a number of functions that are given by a textfile. This file
looks like this:
----
(1+s) / (1+k) ;
(1+(2*s)^2) / (1+k)^2 ;
etc etc.
----
I import these functions into R with the 'readLines' command. This creates a
table whose elements contain the conditions as character strings. I want to
generically plot the functions (there are many) while varying k, but the
type of the imported functions is 'character'. I cannot coerce it to double;
this creates NaN's.
Any suggestions for a way out?
--
View this message in context:
http://r.789695.n4.nabble.com/typecasting-a-function-from-character-to-double-tp3327555p3327555.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help@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.
______________________________________________
R-help@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.