0

converting a data.frame column with as.numeric returns completely different values. I read in a .csv with

data <- read.csv("~/Downloads/data-1.csv")

data$column1 returns decimal values I want to work and which look like the following:

[5509] 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.01 0.13 0.13 0.13 0.13 0.13 0.13

typeof(data$column1)

returns "integer"

class(data$column1)

returns "factor"

But in order to work further with this column I need to convert it into numeric But

as.numeric(data$column1)

returns values like these:

[5509] 14 14 14 14 14 14 14 14 14 14 14 2 14 14 14 14 14 14 14 14 14 14 63 14 14 14 14

Can someone help converting these numbers correctly? Can't figure it out..

Elia
  • 151
  • 1
  • 1
  • 8
  • Rather than `as.numeric()` try `as.numeric(as.character(data$column1))`. – Daniel Anderson Nov 08 '16 at 19:41
  • 1
    Best place to fix, though, is when you read in the data. Find out why the column is being read as a `factor` (maybe it has non-numeric values somewhere?). You can also use `stringsAsFactors = FALSE` so it would at least be a character, or you can explicitly set the column class using the `colClasses` argument of `read.csv`. – Gregor Thomas Nov 08 '16 at 19:44
  • Thanks a lot! as.character did it – Elia Nov 08 '16 at 20:24

0 Answers0