为什么read_csv2读取了一个应该是数字或者字符的数据?
Why does read_csv2 read a data that should be a number or a character?
使用以下代码我想导入一千个(小)csv 文件:
library(readr)
ldf <- list() # creates a list
listcsv <- dir(pattern = "*.csv") # creates the list of all the csv files in the directory
for (k in 1:length(listcsv)){
ldf[[k]] <- read_csv2(listcsv[k])
ldf[[k]] <- as.data.frame(ldf[[k]])
}
操作似乎已经成功,因为文件已全部加载到列表中ldf
。无论如何,其中一些似乎显示出问题。这千个文件的前两列应该是 chr
但只有极少数文件的第二列是 POSIXct 格式。我不知道为什么。此列中的值为:
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
而它们应该是:
08400711
08400711
08400711
08400711
08400711
08400711
08400711
08400711
此处,指定为“c”的 col_types
将仅将第一列转换为 character
。如果我们需要强制所有列为 character
,请指定 .default
(以防我们之前不知道列数)或将 "c"
复制为 "ccc"
(等于到列数)
library(readr)
df1 <- read_csv(readr_example("mtcars.csv"), col_types = cols(.default = "c"))
> str(df1)
spec_tbl_df [32 × 11] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ mpg : chr [1:32] "21" "21" "22.8" "21.4" ...
$ cyl : chr [1:32] "6" "6" "4" "6" ...
$ disp: chr [1:32] "160" "160" "108" "258" ...
$ hp : chr [1:32] "110" "110" "93" "110" ...
$ drat: chr [1:32] "3.9" "3.9" "3.85" "3.08" ...
$ wt : chr [1:32] "2.62" "2.875" "2.32" "3.215" ...
$ qsec: chr [1:32] "16.46" "17.02" "18.61" "19.44" ...
$ vs : chr [1:32] "0" "0" "1" "1" ...
$ am : chr [1:32] "1" "1" "1" "0" ...
$ gear: chr [1:32] "4" "4" "4" "3" ...
$ carb: chr [1:32] "4" "4" "1" "1" ...
使用以下代码我想导入一千个(小)csv 文件:
library(readr)
ldf <- list() # creates a list
listcsv <- dir(pattern = "*.csv") # creates the list of all the csv files in the directory
for (k in 1:length(listcsv)){
ldf[[k]] <- read_csv2(listcsv[k])
ldf[[k]] <- as.data.frame(ldf[[k]])
}
操作似乎已经成功,因为文件已全部加载到列表中ldf
。无论如何,其中一些似乎显示出问题。这千个文件的前两列应该是 chr
但只有极少数文件的第二列是 POSIXct 格式。我不知道为什么。此列中的值为:
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
0840-07-11
而它们应该是:
08400711
08400711
08400711
08400711
08400711
08400711
08400711
08400711
此处,指定为“c”的 col_types
将仅将第一列转换为 character
。如果我们需要强制所有列为 character
,请指定 .default
(以防我们之前不知道列数)或将 "c"
复制为 "ccc"
(等于到列数)
library(readr)
df1 <- read_csv(readr_example("mtcars.csv"), col_types = cols(.default = "c"))
> str(df1)
spec_tbl_df [32 × 11] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ mpg : chr [1:32] "21" "21" "22.8" "21.4" ...
$ cyl : chr [1:32] "6" "6" "4" "6" ...
$ disp: chr [1:32] "160" "160" "108" "258" ...
$ hp : chr [1:32] "110" "110" "93" "110" ...
$ drat: chr [1:32] "3.9" "3.9" "3.85" "3.08" ...
$ wt : chr [1:32] "2.62" "2.875" "2.32" "3.215" ...
$ qsec: chr [1:32] "16.46" "17.02" "18.61" "19.44" ...
$ vs : chr [1:32] "0" "0" "1" "1" ...
$ am : chr [1:32] "1" "1" "1" "0" ...
$ gear: chr [1:32] "4" "4" "4" "3" ...
$ carb: chr [1:32] "4" "4" "1" "1" ...