readxl() 将日期转换为数字
readxl() converts dates to a number
我有一个这样的 xlsx 文件,第一行有日期:
jan/20
mar/2020
jun/2020
A
K
L
C
Y
B
出于某种原因,当使用 readxl()
函数导入 R 时,header 中的日期被转换为数字,例如:
15890、17890 等
我知道将日期保留为变量不是好习惯,但即使转置 table 问题仍然存在。
如果有人可以提供帮助,我将不胜感激:)
我不确定你的数据如何 reading/writing 但是如果我使用这些数据我就无法重现它 :
df <- structure(list(`jan/20` = c("A", "C"), `mar/2020` = c("K", "Y"),
`jun/2020` = c("L", "B")), class = "data.frame", row.names = c(NA, -2L))
df
# jan/20 mar/2020 jun/2020
#1 A K L
#2 C Y B
#Write the data
writexl::write_xlsx(df, 'test.xlsx')
#Read the data
readxl::read_excel('test.xlsx')
# A tibble: 2 x 3
# `jan/20` `mar/2020` `jun/2020`
# <chr> <chr> <chr>
#1 A K L
#2 C Y B
您可以将数字日期更改为您选择的格式:
names(df) <- format(as.Date(as.numeric(names(df)),
origin = '1970-01-01'), '%b/%Y')
df
我有一个这样的 xlsx 文件,第一行有日期:
jan/20 | mar/2020 | jun/2020 |
---|---|---|
A | K | L |
C | Y | B |
出于某种原因,当使用 readxl()
函数导入 R 时,header 中的日期被转换为数字,例如:
15890、17890 等
我知道将日期保留为变量不是好习惯,但即使转置 table 问题仍然存在。 如果有人可以提供帮助,我将不胜感激:)
我不确定你的数据如何 reading/writing 但是如果我使用这些数据我就无法重现它 :
df <- structure(list(`jan/20` = c("A", "C"), `mar/2020` = c("K", "Y"),
`jun/2020` = c("L", "B")), class = "data.frame", row.names = c(NA, -2L))
df
# jan/20 mar/2020 jun/2020
#1 A K L
#2 C Y B
#Write the data
writexl::write_xlsx(df, 'test.xlsx')
#Read the data
readxl::read_excel('test.xlsx')
# A tibble: 2 x 3
# `jan/20` `mar/2020` `jun/2020`
# <chr> <chr> <chr>
#1 A K L
#2 C Y B
您可以将数字日期更改为您选择的格式:
names(df) <- format(as.Date(as.numeric(names(df)),
origin = '1970-01-01'), '%b/%Y')
df