[R] gsub wildcard

Peter Langfelder peter.langfelder at gmail.com
Mon Aug 15 19:40:19 CEST 2011


On Mon, Aug 15, 2011 at 10:34 AM, Rebecca Gray <atlasrrg at gmail.com> wrote:
> Hello all,
>
> I have what I think is a simple question but I've been unable to solve it. I
> have the following string:
>
> A[&states=1]:[&rate=2]425, B[&states=3]:[&rate=5]500
>
> I would like to combine the two expressions in the [], so that only one set
> of [] is present after each letter, so that I have the following string:
>
> A:[&states=1,&rate=2]425, B:[&states=3,&rate=5]500
>
> I tried this:
>
>> Y <- gsub("\\[&states=.\\]:\\[&",":\\[&states=.,", X)
>
> which almost works, except that it replaces the numbers associated with
> "states" with the period - not retaining the original value as I thought the
> wildcard character would.

How about a simpler solution:

x = "A[&states=1]:[&rate=2]425, B[&states=3]:[&rate=5]500"

gsub("]:[", ", ", x, fixed = TRUE)

i.e., simply replace ]:[ by ,

will that work?

Peter



More information about the R-help mailing list