我有几个CSV文件,其中包含本地德语风格的数字,即用逗号作为小数分隔符,点数为千分隔符,例如10.380,45。 CSV文件中的值由";"分隔。这些文件还包含类字符,日期,日期和列表中的列。时间和逻辑。
read.table函数的问题是,您可以使用dec =","来指定小数分隔符,但不能指定千位分隔符。 (如果我错了,请纠正我)
我知道预处理是一种解决方法,但我希望以某种方式编写代码,其他人可以在没有我的情况下使用它。
通过设置我自己的类,我找到了一种通过read.csv2以我想要的方式读取CSV文件的方法,如下例所示。 基于Most elegant way to load csv with point as 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"
)
)
我现在的问题是,不同的数据集最多有200列和350000行。使用上层解决方案,我需要40到60秒才能加载一个CSV文件,我想加快速度。
通过我的研究,我发现了fread()
包中的data.table
,这非常快。加载CSV文件大约需要3到5秒。
不幸的是,也没有可能指定千位分隔符。所以我尝试将我的解决方案与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
所以我的问题是:有没有办法用fread读取数值为10.380,45的CSV文件?
(或者:读取具有此类数值的CSV的最快方法是什么?)
答案 0 :(得分:1)
首先删除所有逗号。
{{1}}
答案 1 :(得分:1)
我自己从未使用过包装,但它来自Hadley Wickham,应该是好东西
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
是您正在寻找的
编辑表格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
)