[R] converting a character vector to a function's input
jim holtman
jholtman at gmail.com
Tue Sep 22 15:21:32 CEST 2009
first part:
> VAR1 <- 1:10
> VAR2 <- 11:20
> VAR3 <- 21:30
> input <- "VAR1,VAR2,VAR3"
> # split the input
> i.s <- unlist(strsplit(input, ','))
> # create matrix
> do.call(cbind, lapply(i.s, get))
[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25
[6,] 6 16 26
[7,] 7 17 27
[8,] 8 18 28
[9,] 9 19 29
[10,] 10 20 30
second part:
> x <- scan(what='')
1: VAR1 VAR2 VAR3
4:
Read 3 items
> x
[1] "VAR1" "VAR2" "VAR3"
> paste(x, collapse=',')
[1] "VAR1,VAR2,VAR3"
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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list