gsub: replacing slashes in a string

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

gsub: replacing slashes in a string

jCeradini
Hi all,

There are many R help posts out there dealing with slashes in gsub. I
understand slashes are "escape characters" and thus need to be treated
differently, and display differently in R. However, I'm still stuck on
find-replace problem, and would appreciate any tips. Thanks!

GOAL: replace all "\\" with "/", so when export file to csv all slashes are
the same.

(test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))

Lengths are all the same, I think (?) because of how R displays/deals with
slashes. However, when I export this to a csv, e.g., there are still double
slashes, which is a problem for me.
nchar(test)

Change direction of slashes - works.
(test2 <- gsub("\\", "//", test, fixed = TRUE))

Now lengths are now not the same....
nchar(test2)

Change from double to single - does not work. Is this because it actually
is a single slash but R is just displaying it as double? Regardless, when I
export from R the double slashes do appear.
gsub("////", "//", test2, fixed = TRUE)
gsub("////", "//", test2)
gsub("////////", "////", test2, fixed = TRUE)
gsub("////////", "////", test2)

        [[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: gsub: replacing slashes in a string

Rui Barradas
Hello,

I failing to understand the problem, isn't the following what you want?

(test2 <- gsub("\\", "/", test, fixed = TRUE))
[1] "8/24/2016" "8/24/2016" "6/16/2016" "6/16/2016"

Hope this helps,

Rui Barradas


Citando Joe Ceradini <[hidden email]>:

> Hi all,
>
> There are many R help posts out there dealing with slashes in gsub. I
> understand slashes are "escape characters" and thus need to be treated
> differently, and display differently in R. However, I'm still stuck on
> find-replace problem, and would appreciate any tips. Thanks!
>
> GOAL: replace all "\\" with "/", so when export file to csv all slashes are
> the same.
>
> (test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))
>
> Lengths are all the same, I think (?) because of how R displays/deals with
> slashes. However, when I export this to a csv, e.g., there are still double
> slashes, which is a problem for me.
> nchar(test)
>
> Change direction of slashes - works.
> (test2 <- gsub("\\", "//", test, fixed = TRUE))
>
> Now lengths are now not the same....
> nchar(test2)
>
> Change from double to single - does not work. Is this because it actually
> is a single slash but R is just displaying it as double? Regardless, when I
> export from R the double slashes do appear.
> gsub("////", "//", test2, fixed = TRUE)
> gsub("////", "//", test2)
> gsub("////////", "////", test2, fixed = TRUE)
> gsub("////////", "////", test2)
>
> [[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.
Reply | Threaded
Open this post in threaded view
|

Re: gsub: replacing slashes in a string

David Carlson
In reply to this post by jCeradini
Is this what you want?

> (test2 <- gsub("\\", "/", test, fixed = TRUE))
[1] "8/24/2016" "8/24/2016" "6/16/2016" "6/16/2016"
> nchar(test2)
[1] 9 9 9 9
> write.csv(test2)
"","x"
"1","8/24/2016"
"2","8/24/2016"
"3","6/16/2016"
"4","6/16/2016"

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Joe Ceradini
Sent: Wednesday, September 14, 2016 11:25 AM
To: Zilefac
Subject: [R] gsub: replacing slashes in a string

Hi all,

There are many R help posts out there dealing with slashes in gsub. I
understand slashes are "escape characters" and thus need to be treated
differently, and display differently in R. However, I'm still stuck on
find-replace problem, and would appreciate any tips. Thanks!

GOAL: replace all "\\" with "/", so when export file to csv all slashes are
the same.

(test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))

Lengths are all the same, I think (?) because of how R displays/deals with
slashes. However, when I export this to a csv, e.g., there are still double
slashes, which is a problem for me.
nchar(test)

Change direction of slashes - works.
(test2 <- gsub("\\", "//", test, fixed = TRUE))

Now lengths are now not the same....
nchar(test2)

Change from double to single - does not work. Is this because it actually
is a single slash but R is just displaying it as double? Regardless, when I
export from R the double slashes do appear.
gsub("////", "//", test2, fixed = TRUE)
gsub("////", "//", test2)
gsub("////////", "////", test2, fixed = TRUE)
gsub("////////", "////", test2)

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

Re: gsub: replacing slashes in a string

jCeradini
Wow. Thanks David and Rui. I thought I needed to "escape" the replacement
slash as well, which is why I had "//" rather than "/". I swear I had tried
all the slash combos, but missed the obvious one. Much easier than I made
it out to be.

Thanks!
Joe

On Wed, Sep 14, 2016 at 10:59 AM, David L Carlson <[hidden email]> wrote:

> Is this what you want?
>
> > (test2 <- gsub("\\", "/", test, fixed = TRUE))
> [1] "8/24/2016" "8/24/2016" "6/16/2016" "6/16/2016"
> > nchar(test2)
> [1] 9 9 9 9
> > write.csv(test2)
> "","x"
> "1","8/24/2016"
> "2","8/24/2016"
> "3","6/16/2016"
> "4","6/16/2016"
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Joe
> Ceradini
> Sent: Wednesday, September 14, 2016 11:25 AM
> To: Zilefac
> Subject: [R] gsub: replacing slashes in a string
>
> Hi all,
>
> There are many R help posts out there dealing with slashes in gsub. I
> understand slashes are "escape characters" and thus need to be treated
> differently, and display differently in R. However, I'm still stuck on
> find-replace problem, and would appreciate any tips. Thanks!
>
> GOAL: replace all "\\" with "/", so when export file to csv all slashes are
> the same.
>
> (test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))
>
> Lengths are all the same, I think (?) because of how R displays/deals with
> slashes. However, when I export this to a csv, e.g., there are still double
> slashes, which is a problem for me.
> nchar(test)
>
> Change direction of slashes - works.
> (test2 <- gsub("\\", "//", test, fixed = TRUE))
>
> Now lengths are now not the same....
> nchar(test2)
>
> Change from double to single - does not work. Is this because it actually
> is a single slash but R is just displaying it as double? Regardless, when I
> export from R the double slashes do appear.
> gsub("////", "//", test2, fixed = TRUE)
> gsub("////", "//", test2)
> gsub("////////", "////", test2, fixed = TRUE)
> gsub("////////", "////", test2)
>
>         [[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.
>



--
Cooperative Fish and Wildlife Research Unit
Zoology and Physiology Dept.
University of Wyoming
[hidden email] / 914.707.8506
wyocoopunit.org

        [[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: gsub: replacing slashes in a string

jholtman
In reply to this post by jCeradini
try this:

> gsub("\\\\", "/", test)
[1] "8/24/2016" "8/24/2016" "6/16/2016" "6/16/2016"




Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Wed, Sep 14, 2016 at 12:25 PM, Joe Ceradini <[hidden email]>
wrote:

> Hi all,
>
> There are many R help posts out there dealing with slashes in gsub. I
> understand slashes are "escape characters" and thus need to be treated
> differently, and display differently in R. However, I'm still stuck on
> find-replace problem, and would appreciate any tips. Thanks!
>
> GOAL: replace all "\\" with "/", so when export file to csv all slashes are
> the same.
>
> (test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))
>
> Lengths are all the same, I think (?) because of how R displays/deals with
> slashes. However, when I export this to a csv, e.g., there are still double
> slashes, which is a problem for me.
> nchar(test)
>
> Change direction of slashes - works.
> (test2 <- gsub("\\", "//", test, fixed = TRUE))
>
> Now lengths are now not the same....
> nchar(test2)
>
> Change from double to single - does not work. Is this because it actually
> is a single slash but R is just displaying it as double? Regardless, when I
> export from R the double slashes do appear.
> gsub("////", "//", test2, fixed = TRUE)
> gsub("////", "//", test2)
> gsub("////////", "////", test2, fixed = TRUE)
> gsub("////////", "////", test2)
>
>         [[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.
>

        [[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: gsub: replacing slashes in a string

jCeradini
Thanks Jim!

Joe

On Wed, Sep 14, 2016 at 11:06 AM, jim holtman <[hidden email]> wrote:

> try this:
>
> > gsub("\\\\", "/", test)
> [1] "8/24/2016" "8/24/2016" "6/16/2016" "6/16/2016"
>
>
>
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> On Wed, Sep 14, 2016 at 12:25 PM, Joe Ceradini <[hidden email]>
> wrote:
>
>> Hi all,
>>
>> There are many R help posts out there dealing with slashes in gsub. I
>> understand slashes are "escape characters" and thus need to be treated
>> differently, and display differently in R. However, I'm still stuck on
>> find-replace problem, and would appreciate any tips. Thanks!
>>
>> GOAL: replace all "\\" with "/", so when export file to csv all slashes
>> are
>> the same.
>>
>> (test <- c("8/24/2016", "8/24/2016", "6/16/2016", "6\\16\\2016"))
>>
>> Lengths are all the same, I think (?) because of how R displays/deals with
>> slashes. However, when I export this to a csv, e.g., there are still
>> double
>> slashes, which is a problem for me.
>> nchar(test)
>>
>> Change direction of slashes - works.
>> (test2 <- gsub("\\", "//", test, fixed = TRUE))
>>
>> Now lengths are now not the same....
>> nchar(test2)
>>
>> Change from double to single - does not work. Is this because it actually
>> is a single slash but R is just displaying it as double? Regardless, when
>> I
>> export from R the double slashes do appear.
>> gsub("////", "//", test2, fixed = TRUE)
>> gsub("////", "//", test2)
>> gsub("////////", "////", test2, fixed = TRUE)
>> gsub("////////", "////", test2)
>>
>>         [[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/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>


--
Cooperative Fish and Wildlife Research Unit
Zoology and Physiology Dept.
University of Wyoming
[hidden email] / 914.707.8506
wyocoopunit.org

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