R: How can I read a CSV file with data.table :: fread, which has a comma as a decimal and indicate as a thousand separators = "."

I have several CSV files that contain numbers in the local German style, i.e. with a semicolon as a decimal separator, and a dot as a thousand separator, for example. 10.380.45. Values ​​in the CSV file are separated by a ";" Files also contain columns of class characters, date, date and time, and logic.

The problem with read.table functions is that you can specify a decimal separator with dec = ",", but not a thousandth dot separator. (If I'm wrong, please correct me)

I know that preprocessing is a workaround, but I want to write my code so that others can use it without me.

I found a way to read the CSV file the way I want it using read.csv2, setting my own classes, as seen in the following example. Based on the most elegant way to load csv with a dot as a thousands separator in R

# Create test example
df_test_write <- cbind.data.frame(c("a","b","c","d","e","f","g","h","i","j",rep("k",times=200)),
                            c("5.200,39","250,36","1.000.258,25","3,58","5,55","10.550,00","10.333,00","80,33","20.500.000,00","10,00",rep("3.133,33",times=200)),
                            c("25.03.2015","28.04.2015","03.05.2016","08.08.2016","08.08.2016","08.08.2016","08.08.2016","08.08.2016","08.08.2016","08.08.2016",rep("08.08.2016",times=200)),
                            stringsAsFactors=FALSE)
colnames(df_test_write) <- c("col_text","col_num","col_date")

# write test csv
write.csv2(df_test_write,file="Test.csv",quote=FALSE,row.names=FALSE)

#### read with read.csv2 ####

# First, define your own class

#define your own numeric class
setClass('myNum')
#define conversion
setAs("character","myNum", function(from) as.numeric(gsub(",","\\.",gsub("\\.","",from))))

# own date class
library(lubridate)
setClass('myDate')
setAs("character","myDate",function(from) dmy(from))

# Read the csv file, in colClasses the columns class can be defined
df_test_readcsv <- read.csv2(paste0(getwd(),"/Test.csv"),
                       stringsAsFactors = FALSE,
                       colClasses = c(
                         col_text = "character",
                         col_num = "myNum",
                         col_date = "myDate"
                       )
                )

My problem is that different datasets have up to 200 columns and 350,000 rows. With the top solution, I need 40 to 60 seconds to download one CSV file, and I would like to speed it up.

In the course of my research, I found fread()from the package data.tablethat very quickly. Downloading a CSV file takes 3 to 5 seconds.

, . colClasses, , , , fread https://github.com/Rdatatable/data.table/issues/491

. :

##### read with fread ####
library(data.table)

# Test without colclasses
df_test_readfread1 <- fread(paste0(getwd(),"/Test.csv"),
                            stringsAsFactors = FALSE,
                            dec = ",",
                            sep=";",
                            verbose=TRUE)
str(df_test_readfread1)

# PROBLEM: In my real dataset it turns the number into an numeric column, 
# unforunately it sees the "." as decimal separator, so it turns e.g. 10.550, 
# into 10.5
# Here it keeps everything as character

# Test with colclasses
df_test_readfread2 <- fread(paste0(getwd(),"/Test.csv"),
                            stringsAsFactors = FALSE,
                            colClasses = c(
                              col_text = "character",
                              col_num = "myNum",
                              col_date = "myDate"
                            ),
                            sep=";",
                            verbose=TRUE)
str(df_test_readfread2)

# Keeps everything as character

, : CSV , 10.380,45 fread? (: CSV ?)

, , ; -).

+4
2

, ,

https://cran.r-project.org/web/packages/readr/readr.pdf

:

locale(date_names = "en", date_format = "%AD", time_format = "%AT", decimal_mark = ".", grouping_mark = ",", tz = "UTC", encoding = "UTF-8", asciify = FALSE)

decimal_mark grouping_mark - ,

EDIT PhiSeu:

read_csv2() readr. 350000 CSV 8 , , read.csv2. ( hadley RStudio, )

library(readr)

# solution 1 with specified columns
df_test_readr <- read_csv2(paste0(getwd(),"/Test.csv"),
                           locale = locale("de"),
                           col_names = TRUE,
                           cols(
                             col_text = col_character(),
                             col_num = col_number(), # number is automatically regcognized through locale=("de")
                             col_date2 = col_date(format ="%d.%m.%Y") # Date specification
                           )
                           )

# solution 2 with overall definition of date format
df_test_readr <- read_csv2(paste0(getwd(),"/Test.csv"),
                           locale = locale("de",date_format = "%d.%m.%Y"), # specifies the date format for the whole file
                           col_names = TRUE
)
+1

.

filepath<-paste0(getwd(),"/Test.csv")
filestring<-readChar(filepath, file.info(filepath)$size)
filestring<-gsub('.','',filestring,fixed=TRUE)
fread(filestring)
+1

Source: https://habr.com/ru/post/1651561/


All Articles