1

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?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Sakti
  • 13
  • 1
  • 3

1 Answers1

2

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.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • Oh sorry, I will do it next time, but thanks!! It worked and now I have the values in the matrix, which I needed to convert to a data frame. Thanks! – Sakti Jul 02 '11 at 18:57
  • You're welcome. Glad it solved your problem. For future posts, there's a great guide to allowing others to quickly reproduce your example here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example You'll find you get a lot more responses if you make it easy for those who want to help to help. Good luck on whatever project you were working on! – Ari B. Friedman Jul 02 '11 at 19:00
  • Thanks, I'll check it! On the same line, another doubt surged: what if lets say I have a value 4.5678 from a data frame and it is a factor, but I want to use it as a number, how do you convert it to respect the entire value? – Sakti Jul 02 '11 at 19:29
  • I got it: as.numeric(levels(frame$col)[frame$col]) – Sakti Jul 02 '11 at 19:36
  • as.numeric(as.character(my.val)) if I understand your question correctly. – Ari B. Friedman Jul 02 '11 at 20:34