qwerty qwerty
2009-Sep-22 12:57 UTC
[R] converting a character vector to a function's input
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]]
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?
Petr PIKAL
2009-Sep-22 13:26 UTC
[R] Odp: converting a character vector to a function's input
Hi r-help-bounces at r-project.org napsal dne 22.09.2009 14:57:59:> Hi all, I have been trying to solve this problem and have had no luck sofar.> > 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 thischaracter> vector such that I can input a transformed version of the charactervector> 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) equivalentto> 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(" ",",","VAR1VAR2 VAR3")) Something like this?> mp[,1] [,2] [,3] [,4] [,5] [1,] 1.5 6.5 11.5 16.5 21.5 [2,] 2.5 7.5 12.5 17.5 22.5 [3,] 3.5 8.5 13.5 18.5 23.5 [4,] 4.5 9.5 14.5 19.5 24.5> DF<-as.data.frame(mp) > DF[,unlist(strsplit("V1,V2,V3", ","))]V1 V2 V3 1 1.5 6.5 11.5 2 2.5 7.5 12.5 3 3.5 8.5 13.5 4 4.5 9.5 14.5 Regards Petr> > 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Gabor Grothendieck
2009-Sep-22 14:49 UTC
[R] converting a character vector to a function's input
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. >