Oh cool! I didn't realize you could assign a different value to a substring
like that.
Thanks!
Joy
From: Petr Savicky [savi...@cs.cas.cz]
Sent: Wednesday, February 08, 2012 3:26 PM
To: r-help@r-project.org
Subject: Re: [R] String position char
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]
wrote:
> Hi,
>
> Is there a way to efficiently replace specified indices in a string with
> another character?
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 followin
M
To: Yang, Joy (NIH/NHGRI) [F]
Cc: r-help@R-project.org
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("^(.{&q
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
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("hellohowareyoudo
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, "")[
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
8 matches
Mail list logo