![](/img/trans.png)
[英]Is there any way to assign the col_types by column names in read_excel(readxl) in R
[英]How can I change column type `<dttm>` to `<date>` with `col_types` using `read_xlsx` or `read_excel` in r?
在 Windows 10 上使用 R 版本 3.6.1。我正在嘗試使用read_xlsx
或read_excel
將第一列類型從<dttm>
更改為<date>
。
這是演示數據集https://www.dropbox.com/s/ts1utkyib6hw226/demo.xlsx?dl=0的鏈接(dropbox 有一些問題,所以我希望它可以工作)。
這是我嘗試過的一些事情以及相應的錯誤消息。
read_xlsx("demo/demo.xlsx", col_types = list(Date = col_date(format = "%Y-%m-%d")))
## Error in check_col_types(col_types) : is.character(col_types) is not TRUE
read_xlsx("demo/demo.xlsx", col_types = list(Date = "date"))
## Error in check_col_types(col_types) : is.character(col_types) is not TRUE
或者,如果我列出 26 種列類型,則列類型 output 仍然是<dttm>
。
read_xlsx("demo/demo.xlsx", col_types = c("date", "guess", "guess", "guess", "guess", "guess", ...)
另一種嘗試是as.Date()
function 但返回以下錯誤。
read_xlsx("demo/demo.xlsx", col_types = list(Date = as.Date()))
## Error in inherits(x, "Date") : argument "x" is missing, with no default
謝謝您的幫助!
讀取數據時是否需要更改列類型? 閱讀后可以輕松地將數據更改為您想要的格式。 例如,嘗試
transform(readxl::read_xlsx("demo/demo.xlsx"), Date = as.Date(Date))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.