[R] converting a character vector to a function's input

Gabor Grothendieck ggrothendieck at gmail.com
Tue Sep 22 16:49:27 CEST 2009


Here are a few alternatives:

VAR1 <- VAR2 <- VAR1 <- 1:3
v <- "VAR1,VAR2,VAR3"

# 1
eval(parse(text = sprintf("cbind(%s)", v)))

# 2
do.call(cbind, lapply(strsplit(v, ",")[[1]], get))

# 3
library(gsubfn)
strapply(v, "\\w+", get, combine = list, simplify = ... ~ do.call(cbind, ...))

On Tue, Sep 22, 2009 at 8:57 AM, qwerty qwerty <qwerty132us at yahoo.com> wrote:
> Hi all, I have been trying to solve this problem and have had no luck so far.
>
> I have numeric vectors VAR1, VAR2, and VAR3 which I am trying to cbind. I also have a character vector "VAR1,VAR2,VAR3". How do I manipulate this character vector such that I can input a transformed version of the character vector into cbind and have it recognize that I'm trying to refer to my numeric vectors VAR1, VAR2, and VAR3.
>
> i.e. make cbind(some transformed version of character vector) equivalent to cbind(VAR1,VAR2,VAR3)
>
> Alternatively, starting with the console input: VAR1 VAR2 VAR3
> how does one transform into the input VAR1,VAR2,VAR3
>
> (to get my aformentioned character vector I am using gsub(" ",",","VAR1 VAR2 VAR3"))
>
> Thanks for the help!
>
> Mike
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list