Hi
I'd like to remove a leading "3" if my number is 7 digits long, if
it is
only 6 I don't want to anything.
I think this should be possible with a 1-liner using sub() but I am not
sure how to define the number of characters following the leading one.
For example my vector:
a <- c(3593857,384723,4395843,3398374)
with sub("^3","",a) I also remove the leading from the
second element which
is only 6 digits long. So how to restrict that using sub? The final result
should be
a <- c(593857,384723,4395843,398374)
Any suggestions?
Best regards,
Johannes
[[alternative HTML version deleted]]
Hi Johannes,
Not sure if this can be done with sub() only, but combining it with
ifelse() apparently does what you want:
ifelse(nchar(a)==7, sub("^3","",a), a)
HTH,
Ivan
--
Ivan Calandra, PhD
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra
Le 06/10/15 16:38, Johannes Radinger a ?crit :> Hi
>
> I'd like to remove a leading "3" if my number is 7 digits
long, if it is
> only 6 I don't want to anything.
> I think this should be possible with a 1-liner using sub() but I am not
> sure how to define the number of characters following the leading one.
>
> For example my vector:
>
> a <- c(3593857,384723,4395843,3398374)
>
> with sub("^3","",a) I also remove the leading from the
second element which
> is only 6 digits long. So how to restrict that using sub? The final result
> should be
>
> a <- c(593857,384723,4395843,398374)
>
> Any suggestions?
>
> Best regards,
> Johannes
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
> On Oct 6, 2015, at 9:38 AM, Johannes Radinger <johannesradinger at gmail.com> wrote: > > Hi > > I'd like to remove a leading "3" if my number is 7 digits long, if it is > only 6 I don't want to anything. > I think this should be possible with a 1-liner using sub() but I am not > sure how to define the number of characters following the leading one. > > For example my vector: > > a <- c(3593857,384723,4395843,3398374) > > with sub("^3","",a) I also remove the leading from the second element which > is only 6 digits long. So how to restrict that using sub? The final result > should be > > a <- c(593857,384723,4395843,398374) > > Any suggestions? > > Best regards, > JohannesHi,> gsub("^3([0-9]{6})$", "\\1", a)[1] "593857" "384723" "4395843" "398374" or> sub("^3([0-9]{6})$", "\\1", a)[1] "593857" "384723" "4395843" "398374" If the source begins with a 3 followed by 6 digits only from 0 to 9, it will return the 6 digits part of the regex within the parens. Otherwise, the source is returned unchanged. See ?regex Regards, Marc Schwartz
On Oct 6, 2015, at 7:38 AM, Johannes Radinger wrote:> Hi > > I'd like to remove a leading "3" if my number is 7 digits long, if it is > only 6 I don't want to anything. > I think this should be possible with a 1-liner using sub() but I am not > sure how to define the number of characters following the leading one. > > For example my vector: > > a <- c(3593857,384723,4395843,3398374) > > with sub("^3","",a) I also remove the leading from the second element which > is only 6 digits long. So how to restrict that using sub? The final result > should be > > a <- c(593857,384723,4395843,398374)Use a wild-card capture class of the correct length:> sub("^3(.{6})$", "\\1", a)[1] "593857" "384723" "4395843" "398374"> > [[alternative HTML version deleted]]It doesn't affect this post but you are requested to post in plain text. -- David Winsemius Alameda, CA, USA