how to exclude certain keywords in strings

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

how to exclude certain keywords in strings

lily li
Hi R users,

I don't know how to extract certain words in strings. For example, if I
have strings like the formats "StationName1_temp.csv",
"StationName2_temp.csv", etc. How to get strings like these "StationName1",
"StationName2", etc? That is to say, I want to exclude the keywords
"_temp.csv", but I could not find a clue online. Thanks for your help.

        [[alternative HTML version deleted]]

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

Re: how to exclude certain keywords in strings

lily li
I just figured this out, which seems to be a simple question. I just used
gsub("_temp.csv","","StationName1_temp.csv").



> Hi R users,
>
> I don't know how to extract certain words in strings. For example, if I
> have strings like the formats "StationName1_temp.csv",
> "StationName2_temp.csv", etc. How to get strings like these "StationName1",
> "StationName2", etc? That is to say, I want to exclude the keywords
> "_temp.csv", but I could not find a clue online. Thanks for your help.
>

        [[alternative HTML version deleted]]

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

Re: how to exclude certain keywords in strings

Jeff Newmiller
In reply to this post by lily li
I cannot imagine you looked very hard... this kind of question pops up all the time on this mailing list. Read the help fur the sub() function... you want to search for something and replace it with an empty string ("").

You should also know by now to post plain text on this mailing list.

On October 11, 2018 8:59:11 PM PDT, lily li <[hidden email]> wrote:

>Hi R users,
>
>I don't know how to extract certain words in strings. For example, if I
>have strings like the formats "StationName1_temp.csv",
>"StationName2_temp.csv", etc. How to get strings like these
>"StationName1",
>"StationName2", etc? That is to say, I want to exclude the keywords
>"_temp.csv", but I could not find a clue online. Thanks for your help.
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

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

Re: how to exclude certain keywords in strings

Rui Barradas
In reply to this post by lily li
Hello,

If you want to remove everything from the last underscore to the end of
the string, this regex is better.
Note that you don't need gsub, 'g' is to replace all matches.

x <- c("StationName1_temp.csv", "Station_Name1_temp.csv",
"StationName2_temp.csv")

sub("_[^_]*$", "", x)
#[1] "StationName1"  "Station_Name1" "StationName2"


Hope this helps,

Rui Barradas

Às 05:03 de 12/10/2018, lily li escreveu:

> I just figured this out, which seems to be a simple question. I just used
> gsub("_temp.csv","","StationName1_temp.csv").
>
>
>
>> Hi R users,
>>
>> I don't know how to extract certain words in strings. For example, if I
>> have strings like the formats "StationName1_temp.csv",
>> "StationName2_temp.csv", etc. How to get strings like these "StationName1",
>> "StationName2", etc? That is to say, I want to exclude the keywords
>> "_temp.csv", but I could not find a clue online. Thanks for your help.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

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