我有一个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
首先导入header,然后导入data,最后将header插入到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
当导入标题时,我有多个列与标题条目。但是,在导入表时,所有条目都放在一个列中,与csv文件中一样(应该是多个列)。我如何解决它与read.csv()函数?
像这样?
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
不使用任何库:
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
您将需要重命名第一列。