I need to change a vector dd that looks like this:
c("LBAM 5|A|15C|3h", "LBAM 5|K|15C|2h")
into this:
c("LBAM 5|15C|3h", "LBAM 5|15C|2h")
It's not very imaginative, but I could use a complicated nesting of
gsub() as so:
gsub("-", "\\|", gsub("K-", "",
gsub("A-", "", gsub("\\|", "-", dd))))
Or I could make it a bit more readable by using interim objects,
But I'd prefer to use a single regular expression that can detect
"A|"
*and* "K|" without collateral damage from the impact of special
characters and regular characters.
TIA
--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
___ Patrick Connolly
{~._.~} Great minds discuss ideas
_( Y )_ Average minds discuss events
(:_~*~_:) Small minds discuss people
(_)-(_) ..... Eleanor Roosevelt
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
gsub("[A|K]\\|", "", x)
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
2015-09-24 11:52 GMT+02:00 Patrick Connolly <p_connolly at
slingshot.co.nz>:
> I need to change a vector dd that looks like this:
> c("LBAM 5|A|15C|3h", "LBAM 5|K|15C|2h")
>
> into this:
> c("LBAM 5|15C|3h", "LBAM 5|15C|2h")
>
> It's not very imaginative, but I could use a complicated nesting of
> gsub() as so:
>
> gsub("-", "\\|", gsub("K-", "",
gsub("A-", "", gsub("\\|", "-", dd))))
>
> Or I could make it a bit more readable by using interim objects,
>
> But I'd prefer to use a single regular expression that can detect
"A|"
> *and* "K|" without collateral damage from the impact of special
> characters and regular characters.
>
> TIA
>
> --
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ___ Patrick Connolly
> {~._.~} Great minds discuss ideas
> _( Y )_ Average minds discuss events
> (:_~*~_:) Small minds discuss people
> (_)-(_) ..... Eleanor Roosevelt
>
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
>
> ______________________________________________
> R-help at r-project.org 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]]
On 24 Sep 2015, at 12:05 , Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:> gsub("[A|K]\\|", "", x)That'll probably do it, but what was the point of the | in [A|K] ?? I don't think it does what I think you think it does... Somewhat safer, maybe: gsub("\\|[AK]\\|","\\|", x) (avoids surprises from, say, "LBAM 5|A|15A|3h") -pd> [snip] > 2015-09-24 11:52 GMT+02:00 Patrick Connolly <p_connolly at slingshot.co.nz>: > >> I need to change a vector dd that looks like this: >> c("LBAM 5|A|15C|3h", "LBAM 5|K|15C|2h") >> >> into this: >> c("LBAM 5|15C|3h", "LBAM 5|15C|2h") >> >> It's not very imaginative, but I could use a complicated nesting of >> gsub() as so: >> >> gsub("-", "\\|", gsub("K-", "", gsub("A-", "", gsub("\\|", "-", dd)))) >> >> Or I could make it a bit more readable by using interim objects, >> >> But I'd prefer to use a single regular expression that can detect "A|" >> *and* "K|" without collateral damage from the impact of special >> characters and regular characters. >>-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com