简体   繁体   中英

Cannot read csv file with R

I have a csv file that looks like:

,,,,,,,,
,,,,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). How can I solve it with read.csv() function?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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