[R] gsub() on Matrix
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Oct 28 20:03:44 CEST 2004
Tony Plate <tplate at acm.org> writes:
> Many more recent regular expression implementations have ways of
> indicating a match on a word boundary. It's usually "\b".
....
Another idea is that if what you need is something that is parseable
as a model formula RHS, then you might want to parse first and
substitute later. Something along these lines:
> e <- parse(text="x1 + x3 + x4 + x5 + x1:x3 + x1:x4")[[1]]
> repl = lapply(c("i7", "i14", "i13", "d2", "i8", "i5"),as.name)
> names(repl)<-paste("x",1:6,sep="")
> eval(substitute(substitute(e,repl),list(e=e)))
i7 + i13 + d2 + i8 + i7:i13 + i7:d2
> At Wednesday 09:07 PM 10/27/2004, Kevin Wang wrote:
> >Suppose I've got a matrix, and the first few elements look like
> > "x1 + x3 + x4 + x5 + x1:x3 + x1:x4"
> > "x1 + x2 + x3 + x5 + x1:x2 + x1:x5"
> > "x1 + x3 + x4 + x5 + x1:x3 + x1:x5"
> >and so on (have got terms from x1 ~ x14).
> >
> >If I want to replace all the x1 with i7, all x2 with i14, all x3 with i13,
> >for example. Is there an easy way?
> >
> >I tried to put what I want to replace in a vector, like:
> > repl = c("i7", "i14", "i13", "d2", "i8", "i5",
> > "i6", "i3", "A", "i9", "i2",
> > "i4", "i15", "i21")
> >and have another vector, say:
> > > orig
> > [1] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"
> >[11] "x11" "x12" "x13" "x14"
> >
> >Then I tried something like
> > gsub(orig, repl, mat)
> >## mat is the name of my matrix
> >
> >but it didn't work *_*.....it would replace terms like x10 with i70.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list