Re: [R] string question
You can try noquote also: noquote(paste('abc', '"xyz"', sep = "")) On Wed, Jun 30, 2010 at 3:31 PM, Paul Evans wrote: > Hi, > > How can I get double quotes embedded in the string? > > Example: > > -- > str1 <- '"xyz"' > > ## desired output > # abc"xyz" > > qr2 <- paste('abc',str1,sep='') > print(qr2) > > - > > Actual output: > > > [1] "abc\"str\"" > > I also tried putting an escape sequence before the quote, but couldn't get > the > string that I want. > > > thanks, > > > >[[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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[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.
Re: [R] string question
cat() is probably what you want, but note that print() has a 'quote=' argument that you could set to FALSE: print(qr2, quote = FALSE) See ?print.default -Peter Ehlers On 2010-06-30 13:16, Phil Spector wrote: Paul - When you print a string, it escapes the quotes with a backslash. That's a property of the print() function, not the string itself. If you want to see the string, use cat(). The nchar() function is also useful: str1 <- '"xyz"' qr2 <- paste('abc',str1,sep='') print(qr2) [1] "abc\"xyz\"" cat(qr2,'\n') abc"xyz" nchar(qr2) [1] 8 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Wed, 30 Jun 2010, Paul Evans wrote: Hi, How can I get double quotes embedded in the string? Example: -- str1 <- '"xyz"' ## desired output # abc"xyz" qr2 <- paste('abc',str1,sep='') print(qr2) - Actual output: [1] "abc\"str\"" I also tried putting an escape sequence before the quote, but couldn't get the string that I want. thanks, [[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. __ 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. -- Peter Ehlers University of Calgary __ 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.
Re: [R] string question
Paul - When you print a string, it escapes the quotes with a backslash. That's a property of the print() function, not the string itself. If you want to see the string, use cat(). The nchar() function is also useful: str1 <- '"xyz"' qr2 <- paste('abc',str1,sep='') print(qr2) [1] "abc\"xyz\"" cat(qr2,'\n') abc"xyz" nchar(qr2) [1] 8 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Wed, 30 Jun 2010, Paul Evans wrote: Hi, How can I get double quotes embedded in the string? Example: -- str1 <- '"xyz"' ## desired output # abc"xyz" qr2 <- paste('abc',str1,sep='') print(qr2) - Actual output: [1] "abc\"str\"" I also tried putting an escape sequence before the quote, but couldn't get the string that I want. thanks, [[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. __ 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.
[R] string question
Hi, How can I get double quotes embedded in the string? Example: -- str1 <- '"xyz"' ## desired output # abc"xyz" qr2 <- paste('abc',str1,sep='') print(qr2) - Actual output: > [1] "abc\"str\"" I also tried putting an escape sequence before the quote, but couldn't get the string that I want. thanks, [[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.
Re: [R] String question
Knut Krueger wrote: 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 The right hint was there, I think: > paste(c("m1","m2","m3"),collapse=",") [1] "m1,m2,m3" -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ 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.
Re: [R] String question
On Wed, Dec 23, 2009 at 11:21 AM, 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. > > 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 __ 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.
Re: [R] String question
Hi r-help-boun...@r-project.org napsal dne 23.12.2009 12:08:02: > 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 Isn't it same like > paste(temp, collapse=",") [1] "November,December,Monday,Tuesday" I also thought you actually wanted > paste(paste(temp, seq_along(temp), sep="."), collapse=",") [1] "November.1,December.2,Monday.3,Tuesday.4" Regards Petr > > but I have no idea where I could find that documentation ;-) > > Thanks a lot > Knut > > __ > 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. __ 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.
Re: [R] String question
Hi Baptiste, Isn't paste doing exactly this? yes indeed - surprising temp <- c("November", "December","Monday","Tuesday") paste(temp, collapse=",") paste(temp, sep=",") I tried to use sep :-( Arguments |...| one or more *R* objects, to be converted to character vectors. |sep| a character string to separate the terms. Not |NA_character_ |. |collapse| an optional character string to separate the results. Not |NA_character_ |. and I did not realize that I separated the wrong part ... Kind regards Knut __ 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.
Re: [R] String question
On 23-Dec-09 11:40:12, baptiste auguie wrote: > Isn't paste doing exactly this? > > temp <- c("November", "December","Monday","Tuesday") > paste(temp, collapse=",") ># "November,December,Monday,Tuesday" > > HTH, > baptiste Yes, spot-on! I got involved in the confusion resulting from the use of "sep" in previous postings. I think that solves it for Knut! Ted. > 2009/12/23 Ted Harding : >> 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 >>> Knut >> >> Interestingly, 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) >> Fax-to-email: +44 (0)870 094 0861 >> Date: 23-Dec-09 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Time: 11:28:47 >> -- XFMail -- >> >> __ >> 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. >> > > __ > 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. E-Mail: (Ted Harding) Fax-to-email: +44 (0)870 094 0861 Date: 23-Dec-09 Time: 11:57:19 -- XFMail -- __ 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.
Re: [R] String question
Isn't paste doing exactly this? temp <- c("November", "December","Monday","Tuesday") paste(temp, collapse=",") # "November,December,Monday,Tuesday" HTH, baptiste 2009/12/23 Ted Harding : > 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 >> Knut > > Interestingly, 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) > Fax-to-email: +44 (0)870 094 0861 > Date: 23-Dec-09 Time: 11:28:47 > -- XFMail -- > > __ > 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. > __ 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.
Re: [R] String question
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 > Knut Interestingly, 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) Fax-to-email: +44 (0)870 094 0861 Date: 23-Dec-09 Time: 11:28:47 -- XFMail -- __ 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.
Re: [R] String question
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 __ 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.
Re: [R] String question
Hi r-help-boun...@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 string like 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@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. __ 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.
Re: [R] String question
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 __ 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.
Re: [R] String question
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 __ 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.
Re: [R] String question
Will this do? temp <- paste("m", 1:3, sep="",collapse=",") HTH, baptiste 2009/12/23 Knut Krueger : > 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@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. > __ 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.
[R] String question
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@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.