列名不能是日期类。我们可以读取数据,然后从宽格式到长格式,以便列名作为日期类在行中,见下文:
# Save Excel sheet as CSV, then read the data with the headers and without checking the names for columns.
df1 <- read.table(text="id 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016
1 23 545 33 55 66
2 454 536 66 80 11
3 83 585 9 10 19",
header = TRUE, check.names = FALSE)
library(dplyr)
library(tidyr)
df1 %>%
gather(myDate, value, -id) %>%
mutate(myDate = as.Date(myDate, format = "%d/%m/%Y"))
# id myDate value
# 1 1 2016-01-01 23
# 2 2 2016-01-01 454
# 3 3 2016-01-01 83
# 4 1 2016-01-02 545
# 5 2 2016-01-02 536
# 6 3 2016-01-02 585
# 7 1 2016-01-03 33
# 8 2 2016-01-03 66
# 9 3 2016-01-03 9
# 10 1 2016-01-04 55
# 11 2 2016-01-04 80
# 12 3 2016-01-04 10
# 13 1 2016-01-05 66
# 14 2 2016-01-05 11
# 15 3 2016-01-05 19