substring from behind

classic Classic list List threaded Threaded
4 messages Options
Mat
Reply | Threaded
Open this post in threaded view
|

substring from behind

Mat
This post was updated on .
Hello together,

i have a question for "substring".
I know i can filter a number like this one:
bill$No<-substring(bill$Customer,2,4)

in this case i get the 2nd, 3rd and 4th number of my Customer ID.
But how can i do this, if i want the 2nd, 3rd and 4th number from behind of a column.

Like this one.
I have: Mercedes_02352
Audi_03555
and now i want to filter this data.frame to
235
355

can you help me?

Thanks.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: substring from behind

Jorge I Velez
Hi Mat,

The following should get you started:

> s <- "Mercedes_02352"
> substr(s, nchar(s) - 3, nchar(s) - 1)
[1] "235"
>

> # defining a function
> foo <- function(x, a = 3, b = 1) substr(s, nchar(x) - a, nchar(x) - b)
> foo(s)
[1] "235"

HTH,
Jorge.-


On Wed, Jan 30, 2013 at 8:05 PM, Mat <> wrote:

> Hello together,
>
> i have a question for "substring".
> I know i can filter a number like this one:
> bill$No<-substring(bill$Customer,2,4)
>
> in this case i get the 2nd, 3rd and 4th number of my Customer ID.
> But how can i do this, if i want the 2nd, 3rd and 4th number of a column.
>
> Like this one.
> I have: Mercedes_02352
> Audi_03555
> and now i want to filter this data.frame to
> 235
> 355
>
> can you help me?
>
> Thanks.
>
> Mat
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/substring-from-behind-tp4657029.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: substring from behind

arun kirshna
In reply to this post by Mat
Hi,
You could use:
dat1<-data.frame(col1=c("Mercedes_02352","Audi_03555"))
dat1[,1]<-as.numeric(gsub(".*_\\d{1}(.*)\\d{1}$","\\1",dat1[,1])) #if the number of digits are the same
 dat1
#  col1
#1  235
#2  355
A.K.



----- Original Message -----
From: Mat <[hidden email]>
To: [hidden email]
Cc:
Sent: Wednesday, January 30, 2013 4:05 AM
Subject: [R] substring from behind

Hello together,

i have a question for "substring".
I know i can filter a number like this one:
bill$No<-substring(bill$Customer,2,4)

in this case i get the 2nd, 3rd and 4th number of my Customer ID.
But how can i do this, if i want the 2nd, 3rd and 4th number of a column.

Like this one.
I have: Mercedes_02352
Audi_03555
and now i want to filter this data.frame to
235
355

can you help me?

Thanks.

Mat



--
View this message in context: http://r.789695.n4.nabble.com/substring-from-behind-tp4657029.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: substring from behind

Isidro Hidalgo
In reply to this post by Jorge I Velez
It's possible the direct way in R:

customer <- c("Mercedes_02352", "Audi_03555")
substr(customer, nchar(customer) - 3, nchar(customer) - 1)

Isidro

> -----Mensaje original-----
> De: [hidden email] [mailto:[hidden email]]
> En nombre de Jorge I Velez
> Enviado el: miércoles, 30 de enero de 2013 10:50
> Para: Mat
> CC: [hidden email]
> Asunto: Re: [R] substring from behind
>
> Hi Mat,
>
> The following should get you started:
>
> > s <- "Mercedes_02352"
> > substr(s, nchar(s) - 3, nchar(s) - 1)
> [1] "235"
> >
>
> > # defining a function
> > foo <- function(x, a = 3, b = 1) substr(s, nchar(x) - a, nchar(x) -
> b)
> > foo(s)
> [1] "235"
>
> HTH,
> Jorge.-
>
>
> On Wed, Jan 30, 2013 at 8:05 PM, Mat <> wrote:
>
> > Hello together,
> >
> > i have a question for "substring".
> > I know i can filter a number like this one:
> > bill$No<-substring(bill$Customer,2,4)
> >
> > in this case i get the 2nd, 3rd and 4th number of my Customer ID.
> > But how can i do this, if i want the 2nd, 3rd and 4th number of a
> column.
> >
> > Like this one.
> > I have: Mercedes_02352
> > Audi_03555
> > and now i want to filter this data.frame to
> > 235
> > 355
> >
> > can you help me?
> >
> > Thanks.
> >
> > Mat
> >
> >
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/substring-from-behind-tp4657029.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.