Reputation: 1768
I want to read in a csv file with numbers such as 123.456
where the .
is a thousands separator. However, R treats the .
as a decimal separator and instead of 123456
it gives me 123.456
. Is there an easier solution than the ones outlined in R - read csv with numeric columns containing thousands separator?
write("123.456", "number.csv")
read.csv("number.csv", header = F, stringsAsFactors = F)
Upvotes: 2
Views: 1122
Reputation: 2589
The world is full of crazy people from crazy places who write numbers in crazy ways. In computing all this craziness is called "locale".
The readr
package allows you to specify locale options. You are interested in what is variously called the "grouping mark" or "thousands separator".
For example:
library(readr)
read_csv('x,y\n1.234,5.678') # Default is not what you want
#> # A tibble: 1 x 2
#> x y
#> <dbl> <dbl>
#> 1 1.23 5.68
# This is what you want
read_csv('x,y\n1.234,5.678', locale = locale(grouping_mark = "."))
#> # A tibble: 1 x 2
#> x y
#> <dbl> <dbl>
#> 1 1234 5678
Upvotes: 4