-1

Let's use an example. I have a two matrix:

Matrix with names:

> dput(a)
structure(c("Greg", "Martin", "Hunge", "Rek", "Pred", "Singa", 
"Kreton", "Wita", "Marcus", "Sebas", "Theo", "Rox", "Plate", 
"Tret"), .Dim = c(7L, 2L))

Matrix with data:

> dput(b)
structure(c("Greg", "Hunge", "Pred", "12", "54", "11", "33", 
"44", "55", "61", "23", "68", "34", "123", "43", "22", "112", 
"35", "79", "22", "19"), .Dim = c(3L, 7L))

I would like to change the names in the data matrix (first column) according to the matrix a. So, the output should be like that:

> dput(c)
structure(c("Wita", "Sebas", "Rox", "12", "54", "11", "33", 
"44", "55", "61", "23", "68", "34", "123", "43", "22", "112", 
"35", "79", "22", "19"), .Dim = c(3L, 7L))

What's the easiest way to do that ? Remember that in a is more names than I need. The fucntion need to find the proper name for each row.

7
  • 2
    I don't see any pattern here, are these names just randomly replaced or am I missing something? Jun 3, 2014 at 10:10
  • If you load a you can see that "Greg" and "Singa" are in the same row in that matrix and same goes for other names. I want to replace the names from the first column of a with names from second column in a. Jun 3, 2014 at 10:13
  • 1
    I think you have either mis-specified the structure a or the pattern is not clear. It seems to be that you are substituting the ith element of as.vector(a) with the (i+5)th one, but not in the one in the next column. The function match should do what you need (e.g. if your matrix a was such that you substitute the first column with the second one, then you would use a[,2][match(b[,1], a[,1])] to substitute the first column of b)
    – konvas
    Jun 3, 2014 at 10:13
  • Sorry, my bad... I wrote it wrong... Will edit soon. Jun 3, 2014 at 10:15
  • @ShaxiLiver - No its looking different then what you are claiming..
    – vrajs5
    Jun 3, 2014 at 10:15

2 Answers 2

2

You can use match() for this:

a <- structure(c("Greg", "Martin", "Hunge", "Rek", "Pred", "Singa", 
            "Kreton", "Wita", "Marcus", "Sebas", "Theo", "Rox", "Plate", 
            "Tret"), .Dim = c(7L, 2L))

b <- structure(c("Greg", "Hunge", "Pred", "12", "54", "11", "33", 
            "44", "55", "61", "23", "68", "34", "123", "43", "22", "112", 
            "35", "79", "22", "19"), .Dim = c(3L, 7L))

# copy
c <- b
c[ ,1] <- a[match(b[ ,1], a[ ,1]), 2]
c
#     [,1]    [,2] [,3] [,4] [,5]  [,6]  [,7]
#[1,] "Wita"  "12" "33" "61" "34"  "22"  "79"
#[2,] "Sebas" "54" "44" "23" "123" "112" "22"
#[3,] "Rox"   "11" "55" "68" "43"  "35"  "19"
2
  • or c[,1] <- a[a[,1] %in% b[,1], 2] Jun 3, 2014 at 10:44
  • I know, this is why I didn't post an alternative solution. I just wanted to add my prefferable operator for future readers. It also works a bit differently, notice the a and b order in my line compared to yours Jun 3, 2014 at 12:22
0

I hope this will work for you...

d=merge(b,a,by.x=1,by.y=1)
c=as.matrix(d)
c=c[,-1]
c=c[,c(7,1:6)]
colnames(c)=NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.