0

I've looked at other answers regarding my issue and nothing seems to help. So I hope I'm in the right to post a question similar to one that was posted some years ago.

My issue is that during read.table() all the columns come in as factors, even if some of them only contain values (and a few NA's), and, when I try to convert them using as.numeric(), the values simply change, although, indeed they turn numeric. See example.

df <- read.table(doc.csv, header = TRUE, sep = ",", dec = ".")
df$value

# Results in
[1]  2254    1873    2201    2147    2456    1785

# So..
as.numeric(df$value)
[1] 26 14 22 20 32 11
  • I have made sure that the original .csv document contains only numerics and NA's.
  • I have tried converting into characters first, and then numerics. Same issue.

Any ideas? I'm stumped.

1 Answers1

1

you can try

as.numeric(as.character(df$value))

But I would say the reason that read.table reads it in as factors is because somewhere in your column there is non numeric data

Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
  • Hi. I did try that before and it didn't work. But actually, I did solve the issue. A simple oversight on my behalf, there happened to be two spaces after the values in the csv document that I hadn't noticed. – Þórgnýr Thoroddsen Jun 01 '18 at 15:04