vega_l at yahoo.com
2010-Mar-10 11:44 UTC
[R] Joining elements of an array into a single element
Dear forum: I've been trying like for hours but I cannot solve this. I wonder if you could give me a hand. I would like to combine the following elements of an array which I must generate in order to get the syntax I need for another software, lets say: c<-c(1:7) zest<-c(12,34,45,132,56,23,6) p<-"[foresnat2]" for(i in 1:7){tre[i]<-paste("con(",p," == ",c[i],", ",zest[i],", ",sep="")} [1] "con([foresnat2] == 1, 12, " [1] "con([foresnat2] == 2, 34, " [1] "con([foresnat2] == 3, 45, " [1] "con([foresnat2] == 4, 132, " [1] "con([foresnat2] == 5, 56, " [1] "con([foresnat2] == 6, 23, " [1] "con([foresnat2] == 7, 6, " I would like the elements of the object "tre" to go one after another without space (or with a character I design like for example "*" or "+" )like this: [1] "con([foresnat2] == 1, 12, con([foresnat2] == 2, 34, con([foresnat2] == 3, 45, con... etc I understand it could be done with the function "paste" invoking each of the elements, but lets say they are not only 7 but 20 or 40 elements... Is it possible? What can I do? Thank you very much in advance. Luis Vega PD: Just in case: For the other software language i was talking about, the usage of "con" is the same as "ifelse" in R, I'm trying to generate a large embed ifelse(..., ..., ifelse(...,...,ifelse()))) entry. but with what I'm requiring lines above should be enough I think. [[alternative HTML version deleted]]
Hello, If you do this after the for loop, you'll get what you want: paste(tre,collapse="") (you can use whatever separator you want in the collapse argument) But you don't even need the for loop, just do this instead of the for loop: paste("con(",p," == ",c,", ",zest,", ",sep="",collapse="") Best, Miguel On Wed, Mar 10, 2010 at 11:44 AM, <vega_l@yahoo.com> wrote:> Dear forum: > > I've been trying like for hours but I cannot solve this. I wonder if you > could give me a hand. > > I would like to combine the following elements of an array which I must > generate in order to get the syntax I need for another software, lets say: > > c<-c(1:7) > zest<-c(12,34,45,132,56,23,6) > p<-"[foresnat2]" > > for(i in 1:7){tre[i]<-paste("con(",p," == ",c[i],", ",zest[i],", ",sep="")} > > [1] "con([foresnat2] == 1, 12, " > [1] "con([foresnat2] == 2, 34, " > [1] "con([foresnat2] == 3, 45, " > [1] "con([foresnat2] == 4, 132, " > [1] "con([foresnat2] == 5, 56, " > [1] "con([foresnat2] == 6, 23, " > [1] "con([foresnat2] == 7, 6, " > > I would like the elements of the object "tre" to go one after another > without space (or with a character I design like for example "*" or "+" > )like this: > > [1] "con([foresnat2] == 1, 12, con([foresnat2] == 2, 34, con([foresnat2] => 3, 45, con... etc > > I understand it could be done with the function "paste" invoking each of > the elements, but lets say they are not only 7 but 20 or 40 elements... > > Is it possible? What can I do? > > Thank you very much in advance. > > Luis Vega > > PD: Just in case: For the other software language i was talking about, the > usage of "con" is the same as "ifelse" in R, I'm trying to generate > a large embed ifelse(..., ..., ifelse(...,...,ifelse()))) entry. but > with what I'm requiring lines above should be enough I think. > > > > > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]