[R] gsub() on Matrix

Kevin Wang Kevin.Wang at maths.anu.edu.au
Thu Oct 28 05:07:41 CEST 2004


Hi,

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.

(I know it may be an easy question...but I haven't done much regular
expression)

Cheers,

Kevin

--------------------------------
Ko-Kang Kevin Wang
PhD Student
Centre for Mathematics and its Applications
Building 27, Room 1004
Mathematical Sciences Institute (MSI)
Australian National University
Canberra, ACT 0200
Australia

Homepage: http://wwwmaths.anu.edu.au/~wangk/
Ph (W): +61-2-6125-2431
Ph (H): +61-2-6125-7407
Ph (M): +61-40-451-8301




More information about the R-help mailing list