r - readxl() converts dates to a number
问题描述
I have an xlsx file like this, with date in the first row:
jan/20 | mar/2020 | jun/2020 |
---|---|---|
A | K | L |
C | Y | B |
For some reason, when importing to R using readxl()
function, the dates in the header are converted to a number, like:
15890, 17890, etc.
I know that isn't good practice keeping dates as variables, but even transposing the table the problem remains. I appreciate if someone can help :)
解决方案
我不确定您是如何读取/写入数据的,但如果我使用此数据,我将无法重现:
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
推荐阅读
- excel - Excel UDF - VLOOKUP 部分匹配并在单个单元格中连接结果
- sql - 在 apex 中基于 2 种不同类型的用户进行自定义身份验证
- c# - 使用 SSH.NET 从配置字符串加载 SSH 私钥时出现“Renci.SshNet.Common.SshException:无效的私钥文件”
- jquery-ui-draggable - 如何移动附加的 div?
- python - 地图输出列表破坏地图结果
- php - 使用 ajax 提交表单:工作正常,现在不工作?
- typo3 - PHP 警告: class_parents(): object or string expected
- activemq - ActiveMQ:转换 OpenWire 和 STOMP 消息
- .htaccess - 如何从 URL 中删除文件和参数名称但保留参数值?
- php - 如何指示 Doctrine 的迁移工具忽略外键约束