String position character replacement

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

String position character replacement

Yang, Joy (NIH/NHGRI) [F]
Hi,

Is there a way to efficiently replace specified indices in a string with another character? For example, if I had a vector of strings such as

[1] "hellohowareyoudoing"
[2] "imgoodhowareyou"
[3] "goodandyou"
[4] "yesimgoodijusttoldyou"
[5] "ohyesthatsright"

and had a list of positions that I want to replace with the character "-"

[[1]]
[1]  3  9

[[2]]
[1]  3  4

[[3]]
[1]  4  7

[[4]]
[1] 5 6 7 8 9

[[5]]
[1]  2  5  7 12

I would like to get

[1] "he-lohow-reyoudoing"
[2] "im--odhowareyou"
[3] "goo-an-you"
[4] "yesi-----ijusttoldyou"
[5] "o-ye-t-atsr-ght"

Is there an easy way to do this? Or would the easiest way be writing a function to take substrings of the original vector and pasting in the replacement character?

Thanks in advance!
Joy
______________________________________________
[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: String position character replacement

Jorge I Velez
Hi Joy,

Perhaps not the easiest way, but the following seems to work:

x <- c("hellohowareyoudoing", "imgoodhowareyou", "goodandyou",
"yesimgoodijusttoldyou", "ohyesthatsright")
pos <- list(c(3, 9), c(3,4), c(4,7), 5:9, c(2, 5, 7, 12))

sapply(1:length(pos), function(i){
    xx <- strsplit(x, "")[[i]]
    xx[pos[[i]]] <- "-"
    paste(xx, sep = "", collapse = "")
})

[1] "he-lohow-reyoudoing"   "im--odhowareyou"       "goo-an-you"
[4] "yesi-----ijusttoldyou" "o-ye-t-atsr-ght"



On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F] <> wrote:

> Hi,
>
> Is there a way to efficiently replace specified indices in a string with
> another character? For example, if I had a vector of strings such as
>
> [1] "hellohowareyoudoing"
> [2] "imgoodhowareyou"
> [3] "goodandyou"
> [4] "yesimgoodijusttoldyou"
> [5] "ohyesthatsright"
>
> and had a list of positions that I want to replace with the character "-"
>
> [[1]]
> [1]  3  9
>
> [[2]]
> [1]  3  4
>
> [[3]]
> [1]  4  7
>
> [[4]]
> [1] 5 6 7 8 9
>
> [[5]]
> [1]  2  5  7 12
>
> I would like to get
>
> [1] "he-lohow-reyoudoing"
> [2] "im--odhowareyou"
> [3] "goo-an-you"
> [4] "yesi-----ijusttoldyou"
> [5] "o-ye-t-atsr-ght"
>
> Is there an easy way to do this? Or would the easiest way be writing a
> function to take substrings of the original vector and pasting in the
> replacement character?
>
> Thanks in advance!
> Joy
> ______________________________________________
> [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: String position character replacement

Sarah Goslee
In reply to this post by Yang, Joy (NIH/NHGRI) [F]
And here's an alternative solution:

subchar <- function(string, pos, char="-") {
        for(i in pos) {
    string <- gsub(paste("^(.{", i-1, "}).", sep=""), "\\1-", string)
        }
        string
}


> subchar("hellohowareyoudoing", 3)
[1] "he-lohowareyoudoing"

> subchar("hellohowareyoudoing", c(3, 9))
[1] "he-lohow-reyoudoing"

> avec <- c("hellohowareyoudoing", "imgoodhowareyou", "goodandyou", "yesimgoodijusttoldyou", "ohyesthatsright")
> alist <- list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))

> sapply(1:length(avec), function(x)subchar(avec[x], alist[[x]]))
[1] "he-lohow-reyoudoing"   "im--odhowareyou"       "goo-an-you"
[4] "yesi-----ijusttoldyou" "o-ye-t-atsr-ght"
>

Sarah


On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
<[hidden email]> wrote:

> Hi,
>
> Is there a way to efficiently replace specified indices in a string with another character? For example, if I had a vector of strings such as
>
> [1] "hellohowareyoudoing"
> [2] "imgoodhowareyou"
> [3] "goodandyou"
> [4] "yesimgoodijusttoldyou"
> [5] "ohyesthatsright"
>
> and had a list of positions that I want to replace with the character "-"
>
> [[1]]
> [1]  3  9
>
> [[2]]
> [1]  3  4
>
> [[3]]
> [1]  4  7
>
> [[4]]
> [1] 5 6 7 8 9
>
> [[5]]
> [1]  2  5  7 12
>
> I would like to get
>
> [1] "he-lohow-reyoudoing"
> [2] "im--odhowareyou"
> [3] "goo-an-you"
> [4] "yesi-----ijusttoldyou"
> [5] "o-ye-t-atsr-ght"
>
> Is there an easy way to do this? Or would the easiest way be writing a function to take substrings of the original vector and pasting in the replacement character?
>
> Thanks in advance!
> Joy


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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: String position character replacement

Yang, Joy (NIH/NHGRI) [F]
Thank you both! I was working along the lines of Jorge's method, but was taking longer than it should. Sarah's is actually a lot faster.

Thanks again,
Joy

________________________________________
From: Sarah Goslee [[hidden email]]
Sent: Wednesday, February 08, 2012 1:30 PM
To: Yang, Joy (NIH/NHGRI) [F]
Cc: [hidden email]
Subject: Re: [R] String position character replacement

And here's an alternative solution:

subchar <- function(string, pos, char="-") {
        for(i in pos) {
        string <- gsub(paste("^(.{", i-1, "}).", sep=""), "\\1-", string)
        }
        string
}


> subchar("hellohowareyoudoing", 3)
[1] "he-lohowareyoudoing"

> subchar("hellohowareyoudoing", c(3, 9))
[1] "he-lohow-reyoudoing"

> avec <- c("hellohowareyoudoing", "imgoodhowareyou", "goodandyou", "yesimgoodijusttoldyou", "ohyesthatsright")
> alist <- list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))

> sapply(1:length(avec), function(x)subchar(avec[x], alist[[x]]))
[1] "he-lohow-reyoudoing"   "im--odhowareyou"       "goo-an-you"
[4] "yesi-----ijusttoldyou" "o-ye-t-atsr-ght"
>

Sarah


On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
<[hidden email]> wrote:

> Hi,
>
> Is there a way to efficiently replace specified indices in a string with another character? For example, if I had a vector of strings such as
>
> [1] "hellohowareyoudoing"
> [2] "imgoodhowareyou"
> [3] "goodandyou"
> [4] "yesimgoodijusttoldyou"
> [5] "ohyesthatsright"
>
> and had a list of positions that I want to replace with the character "-"
>
> [[1]]
> [1]  3  9
>
> [[2]]
> [1]  3  4
>
> [[3]]
> [1]  4  7
>
> [[4]]
> [1] 5 6 7 8 9
>
> [[5]]
> [1]  2  5  7 12
>
> I would like to get
>
> [1] "he-lohow-reyoudoing"
> [2] "im--odhowareyou"
> [3] "goo-an-you"
> [4] "yesi-----ijusttoldyou"
> [5] "o-ye-t-atsr-ght"
>
> Is there an easy way to do this? Or would the easiest way be writing a function to take substrings of the original vector and pasting in the replacement character?
>
> Thanks in advance!
> Joy


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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: String position character replacement

Gabor Grothendieck
In reply to this post by Yang, Joy (NIH/NHGRI) [F]
On Wed, Feb 8, 2012 at 12:33 PM, Yang, Joy (NIH/NHGRI) [F]
<[hidden email]> wrote:

> Hi,
>
> Is there a way to efficiently replace specified indices in a string with another character? For example, if I had a vector of strings such as
>
> [1] "hellohowareyoudoing"
> [2] "imgoodhowareyou"
> [3] "goodandyou"
> [4] "yesimgoodijusttoldyou"
> [5] "ohyesthatsright"
>
> and had a list of positions that I want to replace with the character "-"
>
> [[1]]
> [1]  3  9
>
> [[2]]
> [1]  3  4
>
> [[3]]
> [1]  4  7
>
> [[4]]
> [1] 5 6 7 8 9
>
> [[5]]
> [1]  2  5  7 12
>
> I would like to get
>
> [1] "he-lohow-reyoudoing"
> [2] "im--odhowareyou"
> [3] "goo-an-you"
> [4] "yesi-----ijusttoldyou"
> [5] "o-ye-t-atsr-ght"
>
> Is there an easy way to do this? Or would the easiest way be writing a function to take substrings of the original vector and pasting in the replacement character?
>

Using this input:

pos <- list(c(3, 9), c(3, 4))
s <- c("hellohowareyoudoing", "imgoodhowareyou")

we can use regmatches like this where the lapply on the LHS constructs
a matches list and the lapply on the RHS constructs a list of "-"
characters:

f <- function(p) structure(p, match.length = rep(1, length(p)))
regmatches(s, lapply(pos, f)) <- lapply(pos, function(p) "-")

The resulting s is:

> s
[1] "he-lohow-reyoudoing" "im--odhowareyou"

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.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.
Reply | Threaded
Open this post in threaded view
|

Re: String position character replacement

Petr Savicky
In reply to this post by Sarah Goslee
On Wed, Feb 08, 2012 at 01:30:55PM -0500, Sarah Goslee wrote:
> And here's an alternative solution:
>
> subchar <- function(string, pos, char="-") {
> for(i in pos) {
>     string <- gsub(paste("^(.{", i-1, "}).", sep=""), "\\1-", string)
> }
> string
> }

Hi.

Try the following modification.

  subchar2 <- function(string, pos) {
    for(i in pos) {
        substr(string, i, i) <- "-"
    }
    string
  }

  avec <- c("hellohowareyoudoing", "imgoodhowareyou", "goodandyou", "yesimgoodijusttoldyou", "ohyesthatsright")
  alist <- list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))
  sapply(1:length(avec), function(x) subchar2(avec[x], alist[[x]]))

  [1] "he-lohow-reyoudoing"   "im--odhowareyou"       "goo-an-you"          
  [4] "yesi-----ijusttoldyou" "o-ye-t-atsr-ght"      

Hope this helps.

Petr Savicky.

______________________________________________
[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: String position character replacement

Yang, Joy (NIH/NHGRI) [F]
Oh cool! I didn't realize you could assign a different value to a substring like that.

Thanks!
Joy
________________________________________
From: Petr Savicky [[hidden email]]
Sent: Wednesday, February 08, 2012 3:26 PM
To: [hidden email]
Subject: Re: [R] String position character replacement

On Wed, Feb 08, 2012 at 01:30:55PM -0500, Sarah Goslee wrote:
> And here's an alternative solution:
>
> subchar <- function(string, pos, char="-") {
>       for(i in pos) {
>       string <- gsub(paste("^(.{", i-1, "}).", sep=""), "\\1-", string)
>       }
>       string
> }

Hi.

Try the following modification.

  subchar2 <- function(string, pos) {
    for(i in pos) {
        substr(string, i, i) <- "-"
    }
    string
  }

  avec <- c("hellohowareyoudoing", "imgoodhowareyou", "goodandyou", "yesimgoodijusttoldyou", "ohyesthatsright")
  alist <- list(c(3, 9), c(3, 4), c(4, 7), c(5,6,7,8,9), c(2,5,7,12))
  sapply(1:length(avec), function(x) subchar2(avec[x], alist[[x]]))

  [1] "he-lohow-reyoudoing"   "im--odhowareyou"       "goo-an-you"
  [4] "yesi-----ijusttoldyou" "o-ye-t-atsr-ght"

Hope this helps.

Petr Savicky.

______________________________________________
[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: String position character replacement

Henrique Dallazuanna
In reply to this post by Yang, Joy (NIH/NHGRI) [F]
Try this:

sapply(mapply(replace, x = strsplit(avec, NULL), list = alist, MoreArgs =
list(values = "-")), paste, collapse = "")

On Wed, Feb 8, 2012 at 3:33 PM, Yang, Joy (NIH/NHGRI) [F]
<[hidden email]>wrote:

> Hi,
>
> Is there a way to efficiently replace specified indices in a string with
> another character? For example, if I had a vector of strings such as
>
> [1] "hellohowareyoudoing"
> [2] "imgoodhowareyou"
> [3] "goodandyou"
> [4] "yesimgoodijusttoldyou"
> [5] "ohyesthatsright"
>
> and had a list of positions that I want to replace with the character "-"
>
> [[1]]
> [1]  3  9
>
> [[2]]
> [1]  3  4
>
> [[3]]
> [1]  4  7
>
> [[4]]
> [1] 5 6 7 8 9
>
> [[5]]
> [1]  2  5  7 12
>
> I would like to get
>
> [1] "he-lohow-reyoudoing"
> [2] "im--odhowareyou"
> [3] "goo-an-you"
> [4] "yesi-----ijusttoldyou"
> [5] "o-ye-t-atsr-ght"
>
> Is there an easy way to do this? Or would the easiest way be writing a
> function to take substrings of the original vector and pasting in the
> replacement character?
>
> Thanks in advance!
> Joy
> ______________________________________________
> [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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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