Search code examples
rmatrixr-factor

Inserting factor values into an R matrix


I have a matrix of the form

arkhansas 15 16 17
newyork 56 45 30

where column 1 are factors and columns 2:4 are integers.

I'm stuck trying to generate a matrix with the form:

15 arkhansas 1
16 arkhansas 2
17 arkhansas 3
56 newyork 1
45 newyork 2
30 newyork 3

Every time I try to assign the factor value to the second column of my new matrix I get numbers instead of city names, how can I solve that?


Solution

  • as.character() will coerce your factor into a character string like you want.

    That said, providing example code would be helpful. Try using dput() on your matrix object and copying and pasting the results into your post. You're likely using a data.frame not a matrix, since I believe matrices can only hold one data type.