Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list of functions from a vector of characters

Thanks in advance, and sorry if this question has been answered previously - I have looked pretty extensively. I have a dataset containing a row of with concatenated information, specifically: name,color code,some function expression. For example, one value may be:

cost#FF0033@log(x)+6.

I have all of the code to extract the information, and I end up with a vector of expressions that I would like to convert to a list of actual functions.

For example:

func.list <- list()
test.func <- c("x","x+1","x+2","x+3","x+4")

where test.func is the vector of expressions. What I would like is:

func.list[[3]]

To be equivalent to

function(x){x+3}

I know that I can create a function using:

somefunc <- function(x){eval(parse(text="x+1"))} 

to convert a character value into a function. The problem comes when I try and loop through to make multiple functions. For an example of something I tried that didn't 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
}

Based on another post (http://stats.stackexchange.com/questions/3836/how-to-create-a-vector-of-functions) I also tried this:

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]))))
 }

Which gives the following error: Error in eval(expr, envir, enclos) : object 'x' not found

The eventual goal is to take the list of functions and apply the jth function to the jth column of the data.frame, so that the user of the script can specify how to normalize each column within the concatenated information given by the column header.

like image 263
dayne Avatar asked Aug 24 '12 22:08

dayne


People also ask

How do you apply a function to all elements of a list in R?

lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.

How do I convert a vector to a list in R?

Convert Vector to List in R or create a list from vector can be done by using as. list() or list() functions. A Vector in R is a basic data structure consist elements of the same data type whereas an R List is an object consisting of heterogeneous elements meaning can contain elements of different types.

Can you have a vector of functions?

A vector function is a function that takes one or more variables and returns a vector. We'll spend most of this section looking at vector functions of a single variable as most of the places where vector functions show up here will be vector functions of single variables.

How do I create a list of characters in R?

How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.


1 Answers

Maybe initialize your list with a single generic function, and then update them using:

foo <- function(x){x+3}
> body(foo) <- quote(x+4)
> foo
function (x) 
x + 4

More specifically, starting from a character, you'd probably do something like:

body(foo) <- parse(text = "x+5")
like image 68
joran Avatar answered Sep 21 '22 21:09

joran