Hi all I have got something like that (actually those are column names) [51] "X19.2.300b...80" "X19.2.400v...80" "X19.2.400b...80" "X19.2.300v...90" "X19.2.300b...90" [56] "X19.2.400v...90" "X19.2..400b..90" "X19.2.300v...100" "X19.2.300b...100" "X19.2.400v...100" in character vector. I would like to get last n figures from this character vector but my regexpr unerstanding is inferior and I did not succeed. Please, is there anybody who can extract those n digits from end of each character string? If there were only 2 digits I could use substr but there can be one, two or three digits. I tried several variations of gsub("0:9$", "\\1" ,names(foto)[10]) but either got whole string or an error. Best regards Petr
Here are a couple of possibilities: sub(".*\\.", "", s) library(gsubfn) strapply(s, "[0-9]*$", simplify = c) On Wed, Jul 29, 2009 at 9:10 AM, Petr PIKAL<petr.pikal at precheza.cz> wrote:> Hi all > > I have got something like that (actually those are column names) > > [51] "X19.2.300b...80" ? ? ? ? ?"X19.2.400v...80" "X19.2.400b...80" > "X19.2.300v...90" ? ? ? ? ?"X19.2.300b...90" > [56] "X19.2.400v...90" ? ? ? ? ?"X19.2..400b..90" "X19.2.300v...100" > "X19.2.300b...100" ? ? ? ? "X19.2.400v...100" > > in character vector. I would like to get last n figures from this > character vector but my regexpr unerstanding is inferior and I did not > succeed. > Please, is there anybody who can extract those n digits from end of each > character string? If there were only 2 digits I could use substr but there > can be one, two or three digits. > > I tried several variations of > > gsub("0:9$", "\\1" ,names(foto)[10]) > > but either got whole string or an error. > > Best regards > Petr > > ______________________________________________ > 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. >
Thanks If somebody does not come with more elaborated solution I will adapt yours. After strsplit I can get not only 3 but several chunks. If the last chunk is every time the one I need then sapply(strsplit(names(foto),"\\..."), length) [1] 2 6 6 6 6 5 5 6 5 3 3 3 3 3 3 3 3 3 3 4 3 3 4 4 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 3 3 4 3 3 3 3 3 3 4 4 3 4 3 3>gives me number of chunks which I can use for selection. Best regards Jorge Ivan Velez <jorgeivanvelez at gmail.com> napsal dne 29.07.2009 15:18:33:> Dear Petr, > > How about this strsplit()ing them up? > > > x > [1] "X19.2.400v...80" > > strsplit(x,"\\...")[[1]][3] > [1] "80" > > HTH, > > Jorge >> On Wed, Jul 29, 2009 at 9:10 AM, Petr PIKAL <petr.pikal at precheza.cz>wrote:> Hi all > > I have got something like that (actually those are column names) > > [51] "X19.2.300b...80" "X19.2.400v...80" "X19.2.400b...80" > "X19.2.300v...90" "X19.2.300b...90" > [56] "X19.2.400v...90" "X19.2..400b..90" "X19.2.300v...100" > "X19.2.300b...100" "X19.2.400v...100" > > in character vector. I would like to get last n figures from this > character vector but my regexpr unerstanding is inferior and I did not > succeed. > Please, is there anybody who can extract those n digits from end of each > character string? If there were only 2 digits I could use substr butthere> can be one, two or three digits. > > I tried several variations of > > gsub("0:9$", "\\1" ,names(foto)[10]) > > but either got whole string or an error. > > Best regards > Petr > > ______________________________________________ > 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.
Thank you Gabor Grothendieck <ggrothendieck at gmail.com> napsal dne 29.07.2009 15:20:40:> Here are a couple of possibilities: > > sub(".*\\.", "", s)This seems to be the target. Can you be so kind and translate for me what it really does? ".* matches several dots? \\. what is this? I thought sub finds a match and replaces "replacement" in a string. But from this solution it seems that the command replaces all characters in a string with empty string and leave only value after three or two dots? Am I right? Best regards Petr> > library(gsubfn) > strapply(s, "[0-9]*$", simplify = c) > > > > On Wed, Jul 29, 2009 at 9:10 AM, Petr PIKAL<petr.pikal at precheza.cz>wrote:> > Hi all > > > > I have got something like that (actually those are column names) > > > > [51] "X19.2.300b...80" "X19.2.400v...80" "X19.2.400b...80" > > "X19.2.300v...90" "X19.2.300b...90" > > [56] "X19.2.400v...90" "X19.2..400b..90" "X19.2.300v...100" > > "X19.2.300b...100" "X19.2.400v...100" > > > > in character vector. I would like to get last n figures from this > > character vector but my regexpr unerstanding is inferior and I did not > > succeed. > > Please, is there anybody who can extract those n digits from end ofeach> > character string? If there were only 2 digits I could use substr butthere> > can be one, two or three digits. > > > > I tried several variations of > > > > gsub("0:9$", "\\1" ,names(foto)[10]) > > > > but either got whole string or an error. > > > > Best regards > > Petr > > > > ______________________________________________ > > 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. > >
dot means any character and * repeats it for as many times as possible so it replaces eveything (.*) up to the last dot (\\.) with nothing. On Wed, Jul 29, 2009 at 9:43 AM, Petr PIKAL<petr.pikal at precheza.cz> wrote:> Thank you > > Gabor Grothendieck <ggrothendieck at gmail.com> napsal dne 29.07.2009 > 15:20:40: > >> Here are a couple of possibilities: >> >> sub(".*\\.", "", s) > > This seems to be the target. Can you be so kind and translate for me what > it really does? > > ".* matches several dots? > \\. what is this? > > I thought sub finds a match and replaces "replacement" in a string. But > from this solution it seems that the command replaces all characters in a > string with empty string and leave only value after three or two dots? Am > I right? > > Best regards > Petr > > > >> >> library(gsubfn) >> strapply(s, "[0-9]*$", simplify = c) >> >> >> >> On Wed, Jul 29, 2009 at 9:10 AM, Petr PIKAL<petr.pikal at precheza.cz> > wrote: >> > Hi all >> > >> > I have got something like that (actually those are column names) >> > >> > [51] "X19.2.300b...80" ? ? ? ? ?"X19.2.400v...80" "X19.2.400b...80" >> > "X19.2.300v...90" ? ? ? ? ?"X19.2.300b...90" >> > [56] "X19.2.400v...90" ? ? ? ? ?"X19.2..400b..90" "X19.2.300v...100" >> > "X19.2.300b...100" ? ? ? ? "X19.2.400v...100" >> > >> > in character vector. I would like to get last n figures from this >> > character vector but my regexpr unerstanding is inferior and I did not >> > succeed. >> > Please, is there anybody who can extract those n digits from end of > each >> > character string? If there were only 2 digits I could use substr but > there >> > can be one, two or three digits. >> > >> > I tried several variations of >> > >> > gsub("0:9$", "\\1" ,names(foto)[10]) >> > >> > but either got whole string or an error. >> > >> > Best regards >> > Petr >> > >> > ______________________________________________ >> > 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. >> > > >
Petr, Here is a different approach. It, in effect, works from the end of the string backwards, rather than from the beginning of the string forwards. substring(x, regexpr("[0-9]+$", x)) The regular expression "[0-9]+$" finds a string of 1 or more ("+") digits ("[0-9]") followed by the end of the line ("$"). The regexpr() function returns the position in the string x where that regular expression begins. Feed that to substring() and you get the desired result. Both substring() and regexpr() work on vectors of strings. Best regards, Chuck Taylor TIBCO Spotfire Seattle -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Petr PIKAL Sent: Wednesday, July 29, 2009 6:10 AM To: r-help at stat.math.ethz.ch Subject: [R] regexpr virtue Hi all I have got something like that (actually those are column names) [51] "X19.2.300b...80" "X19.2.400v...80" "X19.2.400b...80" "X19.2.300v...90" "X19.2.300b...90" [56] "X19.2.400v...90" "X19.2..400b..90" "X19.2.300v...100" "X19.2.300b...100" "X19.2.400v...100" in character vector. I would like to get last n figures from this character vector but my regexpr unerstanding is inferior and I did not succeed. Please, is there anybody who can extract those n digits from end of each character string? If there were only 2 digits I could use substr but there can be one, two or three digits. ... Best regards Petr
Many thanks to you and all others for solutions together with regular expressions insights. Petr "Chuck Taylor" <ctaylor at tibco.com> napsal dne 29.07.2009 19:45:10:> Petr, > > Here is a different approach. It, in effect, works from the end of the > string backwards, rather than from the beginning of the string forwards. > > substring(x, regexpr("[0-9]+$", x)) > > The regular expression "[0-9]+$" finds a string of 1 or more ("+") > digits ("[0-9]") followed by the end of the line ("$"). The regexpr() > function returns the position in the string x where that regular > expression begins. Feed that to substring() and you get the desired > result. Both substring() and regexpr() work on vectors of strings. > > Best regards, > Chuck Taylor > TIBCO Spotfire Seattle > > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Petr PIKAL > Sent: Wednesday, July 29, 2009 6:10 AM > To: r-help at stat.math.ethz.ch > Subject: [R] regexpr virtue > > Hi all > > I have got something like that (actually those are column names) > > [51] "X19.2.300b...80" "X19.2.400v...80" "X19.2.400b...80" > "X19.2.300v...90" "X19.2.300b...90" > [56] "X19.2.400v...90" "X19.2..400b..90" "X19.2.300v...100" > "X19.2.300b...100" "X19.2.400v...100" > > in character vector. I would like to get last n figures from this > character vector but my regexpr unerstanding is inferior and I did not > succeed. > Please, is there anybody who can extract those n digits from end of each > > character string? If there were only 2 digits I could use substr but > there > can be one, two or three digits. > > ... > > Best regards > Petr