substract start from the end of the vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

substract start from the end of the vector

arnaud Gaboury
Dear group,

 

Here is my df :

 

df <-

structure(list(DESCRIPTION = c("PRM HGH GD ALUMINIUM USD 09/07/10 ",

"PRM HGH GD ALUMINIUM USD 09/07/10 ", "PRIMARY NICKEL USD 04/06/10 "

), CREATED.DATE = structure(c(18361, 18361, 18325), class = "Date"),

    QUANITY = c(-1L, 1L, 1L), CLOSING.PRICE = c("2,415.90", "2,415.90",

    "25,755.71")), .Names = c("DESCRIPTION", "CREATED.DATE",

"QUANITY", "CLOSING.PRICE"), row.names = c(NA, 3L), class = "data.frame")

>

> df

                         DESCRIPTION                   CREATED.DATE
QUANITY          CLOSING.PRICE

1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09               -1
2,415.90

2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09                1
2,415.90

3              PRIMARY NICKEL USD 04/06/10    2020-03-04                1
25,755.71

 

 

 

In the DESCRIPTION column, I want to get rid of the date (09/07/10 .). I
know the function substr(x, start, stop), but in my case, I need to indicate
I want to start from the end of the vector, and I have no idea how to pass
this argument.
 
TY for any help

 

 

 

 

 

 

***************************

Arnaud Gaboury

Mobile: +41 79 392 79 56

BBM: 255B488F

***************************

 


        [[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: substract start from the end of the vector

Chuck Cleland
with(df, substr(DESCRIPTION, start=1, stop=nchar(DESCRIPTION) - 10))

?nchar

On 4/23/2010 11:57 AM, arnaud Gaboury wrote:

> Dear group,
>
>  
>
> Here is my df :
>
>  
>
> df <-
>
> structure(list(DESCRIPTION = c("PRM HGH GD ALUMINIUM USD 09/07/10 ",
>
> "PRM HGH GD ALUMINIUM USD 09/07/10 ", "PRIMARY NICKEL USD 04/06/10 "
>
> ), CREATED.DATE = structure(c(18361, 18361, 18325), class = "Date"),
>
>     QUANITY = c(-1L, 1L, 1L), CLOSING.PRICE = c("2,415.90", "2,415.90",
>
>     "25,755.71")), .Names = c("DESCRIPTION", "CREATED.DATE",
>
> "QUANITY", "CLOSING.PRICE"), row.names = c(NA, 3L), class = "data.frame")
>
>
>> df
>
>                          DESCRIPTION                   CREATED.DATE
> QUANITY          CLOSING.PRICE
>
> 1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09               -1
> 2,415.90
>
> 2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09                1
> 2,415.90
>
> 3              PRIMARY NICKEL USD 04/06/10    2020-03-04                1
> 25,755.71
>
>  
>
>  
>
>  
>
> In the DESCRIPTION column, I want to get rid of the date (09/07/10 .). I
> know the function substr(x, start, stop), but in my case, I need to indicate
> I want to start from the end of the vector, and I have no idea how to pass
> this argument.
>  
> TY for any help
>
>  
>
>  
>
>  
>
>  
>
>  
>
>  
>
> ***************************
>
> Arnaud Gaboury
>
> Mobile: +41 79 392 79 56
>
> BBM: 255B488F
>
> ***************************
>
>  
>
>
> [[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.

--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
[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: substract start from the end of the vector

jholtman
In reply to this post by arnaud Gaboury
Use regular expressions:

> df$newDesc <- sub("(\\d+/\\d+/\\d <file://d+///d+///d>+)", '',
df$DESCRIPTION)
> df
                         DESCRIPTION CREATED.DATE QUANITY
CLOSING.PRICE                    newDesc
1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09      -1      2,415.90 PRM
HGH GD ALUMINIUM USD
2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09       1      2,415.90 PRM
HGH GD ALUMINIUM USD
3       PRIMARY NICKEL USD 04/06/10    2020-03-04       1
25,755.71       PRIMARY NICKEL USD



On Fri, Apr 23, 2010 at 11:57 AM, arnaud Gaboury
<[hidden email]>wrote:

> Dear group,
>
>
>
> Here is my df :
>
>
>
> df <-
>
> structure(list(DESCRIPTION = c("PRM HGH GD ALUMINIUM USD 09/07/10 ",
>
> "PRM HGH GD ALUMINIUM USD 09/07/10 ", "PRIMARY NICKEL USD 04/06/10 "
>
> ), CREATED.DATE = structure(c(18361, 18361, 18325), class = "Date"),
>
>    QUANITY = c(-1L, 1L, 1L), CLOSING.PRICE = c("2,415.90", "2,415.90",
>
>    "25,755.71")), .Names = c("DESCRIPTION", "CREATED.DATE",
>
> "QUANITY", "CLOSING.PRICE"), row.names = c(NA, 3L), class = "data.frame")
>
> >
>
> > df
>
>                         DESCRIPTION                   CREATED.DATE
> QUANITY          CLOSING.PRICE
>
> 1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09               -1
> 2,415.90
>
> 2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09                1
> 2,415.90
>
> 3              PRIMARY NICKEL USD 04/06/10    2020-03-04                1
> 25,755.71
>
>
>
>
>
>
>
> In the DESCRIPTION column, I want to get rid of the date (09/07/10 .). I
> know the function substr(x, start, stop), but in my case, I need to
> indicate
> I want to start from the end of the vector, and I have no idea how to pass
> this argument.
>
> TY for any help
>
>
>
>
>
>
>
>
>
>
>
>
>
> ***************************
>
> Arnaud Gaboury
>
> Mobile: +41 79 392 79 56
>
> BBM: 255B488F
>
> ***************************
>
>
>
>
>        [[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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

        [[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: substract start from the end of the vector

Steve Lianoglou-6
In reply to this post by arnaud Gaboury
Hi,

On Fri, Apr 23, 2010 at 11:57 AM, arnaud Gaboury
<[hidden email]> wrote:

> Dear group,
>
> Here is my df :
>
> df <-
>
> structure(list(DESCRIPTION = c("PRM HGH GD ALUMINIUM USD 09/07/10 ",
>
> "PRM HGH GD ALUMINIUM USD 09/07/10 ", "PRIMARY NICKEL USD 04/06/10 "
>
> ), CREATED.DATE = structure(c(18361, 18361, 18325), class = "Date"),
>
>    QUANITY = c(-1L, 1L, 1L), CLOSING.PRICE = c("2,415.90", "2,415.90",
>
>    "25,755.71")), .Names = c("DESCRIPTION", "CREATED.DATE",
>
> "QUANITY", "CLOSING.PRICE"), row.names = c(NA, 3L), class = "data.frame")
>
>>
>
>> df
>
>                         DESCRIPTION                   CREATED.DATE
> QUANITY          CLOSING.PRICE
>
> 1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09               -1
> 2,415.90
>
> 2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09                1
> 2,415.90
>
> 3              PRIMARY NICKEL USD 04/06/10    2020-03-04                1
> 25,755.71
>
> In the DESCRIPTION column, I want to get rid of the date (09/07/10 .). I
> know the function substr(x, start, stop), but in my case, I need to indicate
> I want to start from the end of the vector, and I have no idea how to pass
> this argument.
>
> TY for any help

How about using a regular expression:

R> gsub(" *(\\d+/\\d+/\\d+)$", "", "HGH GD ALUMINIUM USD 09/07/10", perl=TRUE)
[1] "HGH GD ALUMINIUM USD"

And to replace your DESCRIPTION clolumn

R> df$DESCRIPTION <- gsub(" *(\\d+/\\d+/\\d+) *$", "", df$DESCRIPTION)
R> df
               DESCRIPTION CREATED.DATE QUANITY CLOSING.PRICE
1 PRM HGH GD ALUMINIUM USD   2020-04-09      -1      2,415.90
2 PRM HGH GD ALUMINIUM USD   2020-04-09       1      2,415.90
3       PRIMARY NICKEL USD   2020-03-04       1     25,755.71

I'm also removing leading/trailing spaces around your date to strip
out any trailing whitespace.

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
[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: substract start from the end of the vector

arnaud Gaboury
TY Steve, using regular expression does the job nicely. I need now to fully
understand your code and learn more about what a regular expression is. Any
good ref is welcome.


> -----Original Message-----
> From: Steve Lianoglou [mailto:[hidden email]]
> Sent: Friday, April 23, 2010 6:11 PM
> To: arnaud Gaboury
> Cc: [hidden email]
> Subject: Re: [R] substract start from the end of the vector
>
> Hi,
>
> On Fri, Apr 23, 2010 at 11:57 AM, arnaud Gaboury
> <[hidden email]> wrote:
> > Dear group,
> >
> > Here is my df :
> >
> > df <-
> >
> > structure(list(DESCRIPTION = c("PRM HGH GD ALUMINIUM USD 09/07/10 ",
> >
> > "PRM HGH GD ALUMINIUM USD 09/07/10 ", "PRIMARY NICKEL USD 04/06/10 "
> >
> > ), CREATED.DATE = structure(c(18361, 18361, 18325), class = "Date"),
> >
> >    QUANITY = c(-1L, 1L, 1L), CLOSING.PRICE = c("2,415.90",
> "2,415.90",
> >
> >    "25,755.71")), .Names = c("DESCRIPTION", "CREATED.DATE",
> >
> > "QUANITY", "CLOSING.PRICE"), row.names = c(NA, 3L), class =
> "data.frame")
> >
> >>
> >
> >> df
> >
> >                         DESCRIPTION                   CREATED.DATE
> > QUANITY          CLOSING.PRICE
> >
> > 1 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09               -1
> > 2,415.90
> >
> > 2 PRM HGH GD ALUMINIUM USD 09/07/10    2020-04-09                1
> > 2,415.90
> >
> > 3              PRIMARY NICKEL USD 04/06/10    2020-03-04
>    1
> > 25,755.71
> >
> > In the DESCRIPTION column, I want to get rid of the date (09/07/10
> .). I
> > know the function substr(x, start, stop), but in my case, I need to
> indicate
> > I want to start from the end of the vector, and I have no idea how to
> pass
> > this argument.
> >
> > TY for any help
>
> How about using a regular expression:
>
> R> gsub(" *(\\d+/\\d+/\\d+)$", "", "HGH GD ALUMINIUM USD 09/07/10",
> perl=TRUE)
> [1] "HGH GD ALUMINIUM USD"
>
> And to replace your DESCRIPTION clolumn
>
> R> df$DESCRIPTION <- gsub(" *(\\d+/\\d+/\\d+) *$", "", df$DESCRIPTION)
> R> df
>                DESCRIPTION CREATED.DATE QUANITY CLOSING.PRICE
> 1 PRM HGH GD ALUMINIUM USD   2020-04-09      -1      2,415.90
> 2 PRM HGH GD ALUMINIUM USD   2020-04-09       1      2,415.90
> 3       PRIMARY NICKEL USD   2020-03-04       1     25,755.71
>
> I'm also removing leading/trailing spaces around your date to strip
> out any trailing whitespace.
>
> HTH,
> -steve
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>  | Memorial Sloan-Kettering Cancer Center
>  | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
[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: substract start from the end of the vector

Steve Lianoglou-6
On Fri, Apr 23, 2010 at 12:28 PM, arnaud Gaboury
<[hidden email]> wrote:
> TY Steve, using regular expression does the job nicely. I need now to fully
> understand your code and learn more about what a regular expression is. Any
> good ref is welcome.

Skimming through the help pages for ?regex and a quick google search
for "regular expression tutorial" will lead you to the edge of that
rabbit hole rather quickly.

Jumping in it is up to you ...
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
[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.