0

I have a rather baffling problem:

I have imported a data table (with both float and string valued entries) from a database using RODBC, but when I try to select a column (with text values), it gets coerced into integers. Is there any way to make it remain a string valued column?

> myvect <- mydat$NummerpladeTyper

> myvect[1:5]
[1] Hvid Hvid Hvid Hvid Hvid
Levels:  Brugt gul Gul hvid Hvid

> str(myvect)
 Factor w/ 6 levels "","Brugt","gul",..: 6 6 6 6 6 6 6 6 6 6 ...

> typeof(myvect)
[1] "integer"

I would want it to retain the levels "Brugt", "gul", "Gul", "hvid", "Hvid" instead of coercing "Hvid" into 6 etc.

Thanks!

SiKiHe
  • 439
  • 6
  • 16
  • 1
    You just have a factor column there. You still have your values. What is exactly the problem you facing?. If you want to convert your column back to character, just do `as.character(mydat$NummerpladeTyper)` – David Arenburg May 12 '15 at 08:43
  • @csgillespie - it didn't show up when I searched for similar questions because the problem is the opposite. I don't want to convert a string into an integer, I have an string that gets coerced into an integer, and I want it to remain a string. The question you link to doesn't provide any information unless it explains how "Hvid" gets turned into 6! – SiKiHe May 12 '15 at 08:53
  • The string has been coerced into a `factor` (notice the `levels` part). You just need `as.character(mydat$NummerpladeTyper)`. The answer is the same if you have a `numeric` coerced to a `factor`. – csgillespie May 12 '15 at 08:58
  • Again, you didn't lose any information. See my comment above. – David Arenburg May 12 '15 at 08:58

0 Answers0