1

I have a CSV that contains integers and long decimal percentages, such as:

71,4,0.056338028169014086,0.028169014084507043

When I import the CSV in R using

df <- as.data.frame(read.csv('/file/location.csv', header = TRUE, stringsAsFactors=FALSE))

all of the integers import as integers, but the long decimal columns import as character. How can I get them to import as numeric?

Jeremy
  • 1,960
  • 4
  • 21
  • 42
  • 5
    Note that this is a behavior change in 3.1. The plan is to change it back in 3.1.1 so they will import as numeric values again. – MrFlick May 27 '14 at 13:38

2 Answers2

3

Currently, too long real numbers are imported as factor. A workaround is

db <- read.csv('/file/location.csv', header = TRUE,
               colClasses=c("integer","integer","numeric","numeric"))
Luca Braglia
  • 3,133
  • 1
  • 16
  • 21
0

I would need to take a closer look at your data to be sure but I think the reason why your decimals are imported as characters is that in Excel, they are written with a "," instead of a ".".

In R, "," is a character, hence the character type. I can't tell whether this is your problem without seeing your data (are your example copied from your csv file or did you write them manually ?) but I had the same few months ago for this reason.

Vincent
  • 955
  • 2
  • 15
  • 32