Skip to content
Prev 180395 / 398503 Next

Function to read a string as the variables as opposed to taking the string name as the variable

On May 14, 2009, at 12:16 PM, Lori Simpson wrote:

            
Your two examples actually require different approaches.


In the first example, you want to create a character vector and coerce  
it to a 'formula' object, which can be used here and with other  
functions where a formula is one of the arguments (eg. regression  
models).

For example:

string_with_columns <- paste("column", 1:3, sep = "", collapse = " + ")

 > string_with_columns
[1] "column1 + column2 + column3"


form <- paste(string_with_columns, "column4", sep = " ~ ")

 > form
[1] "column1 + column2 + column3 ~ column4"


You would then use something like:

   cast(table1, as.formula(form), mean)



For your second example, you don't need a formula object, you just  
need to 'get' the values in the objects that are named:

first <- 4
second <- 6
third <- "first, second"

vars <- unlist(strsplit(third, split = ", "))

 > vars
[1] "first"  "second"

 > sapply(vars, get)
  first second
      4      6

 > max(sapply(vars, get))
[1] 6

 > sum(sapply(vars, get))
[1] 10


So, see ?as.formula and ?get. Also see ?paste and ?strsplit for  
manipulating the character vectors.

HTH,

Marc Schwartz