Thanks in advance, and I'm sorry if this question was answered earlier - I looked pretty wide. I have a data set containing a string with concatenated information, in particular: name, color code, some function expression. For example, a single value might be:
Cost # FF0033 @ magazine (x) +6.
I have all the code to extract the information, and I get a vector of expressions that I would like to convert to a list of actual functions.
For instance:
func.list <- list()
test.func <- c("x","x+1","x+2","x+3","x+4")
where test.func is the expression vector. I would like to:
func.list[[3]]
To be equivalent
function(x){x+3}
I know that I can create a function using:
somefunc <- function(x){eval(parse(text="x+1"))}
to convert the value of a character to a function. The problem occurs when I try to execute a loop to make several functions. For an example of what I tried, this did not work:
for(i in 1:length(test.func)){
temp <- test.func[i]
f <- assign(function(x){eval(expr=parse(text=temp))})
func.list[[i]] <- f
}
(http://stats.stackexchange.com/questions/3836/how-to-create-a-vector-of-functions), :
makefunc <- function(y){y;function(x){y}}
for(i in 1:length(test.func)){
func.list[[i]] <- assign(x=paste("f",i,sep=""),value=makefunc(eval(parse(text=test.func[i]))))
}
: eval (expr, envir, enc): 'x'
- j- j- data.frame, script , , .