Hi to all I need a string like temp <- paste("m1","m2","m3",sep=",") But i must know how many items are in the string,afterwards the other option would be to use a vector temp <- c("m1","m2","m3") No problem to get the count of items but I must get afterwards the string "m1,m2,m3" No problem to build the string with a loop, but it should be more easy but it seems that I am looking to the wrong functions. Kind regards Knut
Will this do? temp <- paste("m", 1:3, sep="",collapse=",") HTH, baptiste 2009/12/23 Knut Krueger <rh at krueger-family.de>:> Hi to all > > I need a string like > temp <- paste("m1","m2","m3",sep=",") > But i must know how many items are in the string,afterwards > the other option would be to use a vector > temp <- c("m1","m2","m3") > No problem to get the count of items but I must get afterwards the string > ?"m1,m2,m3" > No problem to build the string with a loop, but it should be more easy but > it seems that I am looking to the wrong functions. > > Kind regards Knut > > ______________________________________________ > 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. >
> Will this do? > > temp <- paste("m", 1:3, sep="",collapse=",") > >Unfortunately not, because I explained the example not detailed enough. The string could have different Items, like November, December, Monday, Tuesday, Daylight and so on Therefore I must count the Items of the string separated by "," or change the vector to a string Thanks for your reply Knut
On 12/23/2009 09:21 PM, Knut Krueger wrote:> Hi to all > > I need a string like > temp <- paste("m1","m2","m3",sep=",") > But i must know how many items are in the string,afterwards > the other option would be to use a vector > temp <- c("m1","m2","m3") > No problem to get the count of items but I must get afterwards the > string "m1,m2,m3" > No problem to build the string with a loop, but it should be more easy > but it seems that I am looking to the wrong functions. >Hi Knut, Not as easy as I thought it would be, but: mlist<-as.list(paste("m",1:sample(5:10,1),sep="")) do.call("paste",c(mlist,sep=",")) Jim
Hi r-help-bounces at r-project.org napsal dne 23.12.2009 11:46:31:> > > Will this do? > > > > temp <- paste("m", 1:3, sep="",collapse=",") > > > > > Unfortunately not, because I explained the example not detailed enough. > The string could have different Items, like November, December, Monday, > Tuesday, Daylight and so on > Therefore I must count the Items of the string separated by "," or > change the vector to a stringlike that? x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech") paste(x, seq_along(x), sep=".") Regards Petr> > Thanks for your reply > Knut > > ______________________________________________ > 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.
Jim Lemon schrieb:> > Not as easy as I thought it would be, but: > > mlist<-as.list(paste("m",1:sample(5:10,1),sep="")) > do.call("paste",c(mlist,sep=",")) >Hi Jim, yes it works :-) temp <- c("November", "December","Monday","Tuesday") length(temp) #getting the length of the vector string1=do.call("paste",c(as.list(temp),sep=",")) #converting to a string but I have no idea where I could find that documentation ;-) Thanks a lot Knut
On 23-Dec-09 11:08:02, Knut Krueger wrote:> Jim Lemon schrieb: >> Not as easy as I thought it would be, but: >> >> mlist<-as.list(paste("m",1:sample(5:10,1),sep="")) >> do.call("paste",c(mlist,sep=",")) > > Hi Jim, > yes it works :-) > > temp <- c("November", "December","Monday","Tuesday") > length(temp) #getting the length of the vector > string1=do.call("paste",c(as.list(temp),sep=",")) #converting to a > string > > but I have no idea where I could find that documentation ;-) > > Thanks a lot > KnutInterestingly, cat() does the pasting job in the simplest possible way:> temp <- c("November", "December","Monday","Tuesday") > cat(temp,sep=",")November,December,Monday,Tuesday> [copied from the R console; note the trialing ">" which is the command prompt for the next input -- not part of the output of cat() ] with output to screen (or to nominated file). But there seems to be no way to persuade cat to *return* this result as a value, which could be assigned to a variable. If there were such a way, that would be a very smooth solution! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 23-Dec-09 Time: 11:28:47 ------------------------------ XFMail ------------------------------
On Wed, Dec 23, 2009 at 11:21 AM, Knut Krueger <rh at krueger-family.de> wrote:> Hi to all > > I need a string like > temp <- paste("m1","m2","m3",sep=",") > But i must know how many items are in the string,afterwards > the other option would be to use a vector > temp <- c("m1","m2","m3") > No problem to get the count of items but I must get afterwards the string > ?"m1,m2,m3" > No problem to build the string with a loop, but it should be more easy but > it seems that I am looking to the wrong functions. > > Kind regards Knut >Just thought I'd show you a solution from the other direction, in addition to those that all other have posted: temp <- paste("m1","m2","m3",sep=",")##Generate string nchar(gsub("([^,])","",temp))+1## Count commas in the string and add 1. Regards, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik