简体   繁体   English

无法使用 R 读取 csv 文件

[英]Cannot read csv file with R

I have a csv file that looks like:我有一个 csv 文件,看起来像:

,,,,,,,,
,,,,a,b,c,d,e
,,,"01.01.2022, 00:00 - 01:00",82.7,57.98,0.0,0.0,0.0
,,,"01.01.2022, 01:00 - 02:00",87.6,50.05,15.0,25.570000000000004,383.55000000000007
,,,"01.01.2022, 02:00 - 03:00",87.6,41.33,0.0,0.0,0.0

And I want to import headers first and then the data, and finally insert headers to the names of the table with data我想先导入表头,然后是数据,最后将表头插入到带有数据的表的名称中

file <- "path"

pnl <- read.csv(file, dec = ",")  #, skip = 1, header = TRUE)
headers <- read.csv(file, skip = 1, header = F, nrows = 1, as.is = T)

df  <- read.csv(file, skip = 2, header = F, as.is = T)

#or this
#df <- read.csv(file, skip = 2, header = F, nrow = 1,dec = ".",sep=",", quote = "\"")
colnames(df) <- headers

When importing headers I have multiple columns with the headers entries.导入标题时,我有多个包含标题条目的列。 However, when importing table all entries are put inside one column, the same as in csv file (should be multiple columns).但是,在导入表格时,所有条目都放在一列中,与 csv 文件中的相同(应该是多列)。 How can I solve it with read.csv() function?如何使用 read.csv() 函数解决它?

like this?像这样?

data.table::fread(',,,,,,,,
                  ,,,,a,b,c,d,e
                  ,,,"01.01.2022, 00:00 - 01:00",82.7,57.98,0.0,0.0,0.0
                  ,,,"01.01.2022, 01:00 - 02:00",87.6,50.05,15.0,25.570000000000004,383.55000000000007
                  ,,,"01.01.2022, 02:00 - 03:00",87.6,41.33,0.0,0.0,0.0', 
                  skip = 1)

   V1 V2 V3                        V4    a     b  c     d      e
1: NA NA NA 01.01.2022, 00:00 - 01:00 82.7 57.98  0  0.00   0.00
2: NA NA NA 01.01.2022, 01:00 - 02:00 87.6 50.05 15 25.57 383.55
3: NA NA NA 01.01.2022, 02:00 - 03:00 87.6 41.33  0  0.00   0.00

Without using any libraries:不使用任何库:


colClasses <- c("NULL", "NULL", "NULL", "character", "numeric", "numeric", "numeric", "numeric")

read.csv(file, header = TRUE, skip = 1, colClasses = colClasses)

#                         X.3    a     b  c     d
# 1 01.01.2022, 00:00 - 01:00 82.7 57.98  0  0.00
# 2 01.01.2022, 01:00 - 02:00 87.6 50.05 15 25.57
# 3 01.01.2022, 02:00 - 03:00 87.6 41.33  0  0.00

You will want to rename the first column.您将要重命名第一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM