简体   繁体   English

如何一次将多个字符变量转换为日期时间?

[英]How to convert multiple character variables to datetime at once?

Trying to convert multiple character variables to datetimes.尝试将多个字符变量转换为日期时间。 Simplified example:简化示例:

#create df/tibble with two "datetime" columns still as character 
df=tibble(date1=c("2013-11-26 00:10:12.536","2013-11-26 23:04:32.512","2014-02-19 23:34:44.459"),
          date2=c("2013-11-26 07:06:40.720","2013-11-27 07:09:50.552","2014-02-20 08:00:03.975"))

datetimeFormat="%Y-%m-%d %H:%M:%OS"

#OK: converting a single var using $
df_temp=df
df_temp$date1=as_datetime(df_temp$date1,format = datetimeFormat)

#not OK: converting a single var using indexing (presumably because df_temp[,"date1"] is still a tibble)
df_temp=df
df_temp[,"date1"]=as_datetime(df_temp[,"date1"],format = datetimeFormat)

#also not OK: converting multiple variables in one go
datetimeVars=c("date1","date2")
df_temp=df
df_temp[,datetimeVars]=as_datetime(df_temp[,datetimeVars],format = datetimeFormat)

How can I convert multiple character columns to datetime at once, specifically using a variable containing the variable names (like datetimeVars above)?如何一次将多个字符列转换为日期时间,特别是使用包含变量名称的变量(如上面的datetimeVars )?

Some context:一些背景:

  • my source csv files are not uniform and contain a variable - often large - number of datetimes-to-be (in custom format as example above).我的源 csv 文件不统一,并且包含一个变量 - 通常很大 - 数量的日期时间(如上例所示的自定义格式)。 I can determine which variables should become datetime from their names我可以根据名称确定哪些变量应该成为日期时间
  • read_csv does not consistently recognize the relevant variables as datetime read_csv 不能始终将相关变量识别为日期时间
  • read_csv does not appear to allow setting variable type for multiple variables at once, so can't do something like: df=read_csv("myFile.csv",col_types=cols(datetimeVars=col_datetime(format=datetimeFormat))) I also can't specify/hardcode variable type for each relevant variable like cols(date1=col_datetime(),date2=col_datettime, date3=...) because the number of datetime variables isn't known ahead of time read_csv 似乎不允许一次为多个变量设置变量类型,所以不能做类似的事情: df=read_csv("myFile.csv",col_types=cols(datetimeVars=col_datetime(format=datetimeFormat)))我也可以't 为每个相关变量指定/硬编码变量类型,如cols(date1=col_datetime(),date2=col_datettime, date3=...)因为日期时间变量的数量无法提前知道

So currently stuck at both the levels of import (read_csv) and conversion (as_datetime).所以目前停留在导入(read_csv)和转换(as_datetime)两个级别。 Suggestions welcome.欢迎提出建议。

Tackling the conversion part since the import highly depends on the files and the included formats.由于导入高度依赖于文件和包含的格式,因此处理转换部分。

Convert to date class using as.POSIXct (keep in mind that the date class is always shown in the format printed but keeps more info in the class object - see Reading below).使用as.POSIXct转换为date class(请记住, date class 始终以打印格式显示,但在 class object 中保留更多信息 - 请参阅下文阅读)。

library(dplyr)

datetimeVars <- c("date1", "date2")

df_date <- df %>% 
  summarise(across(all_of(datetimeVars), as.POSIXct))
df_date
# A tibble: 3 × 2
  date1               date2              
  <dttm>              <dttm>             
1 2013-11-26 00:10:12 2013-11-26 07:06:40
2 2013-11-26 23:04:32 2013-11-27 07:09:50
3 2014-02-19 23:34:44 2014-02-20 08:00:03

or with column names matching a starting pattern ( starts_with() )或者列名匹配起始模式( starts_with()

datetimeVars <- c("date")

df_date <- df %>% 
  summarise(across(starts_with(datetimeVars), as.POSIXct))
df_date
# A tibble: 3 × 2
  date1               date2              
  <dttm>              <dttm>             
1 2013-11-26 00:10:12 2013-11-26 07:06:40
2 2013-11-26 23:04:32 2013-11-27 07:09:50
3 2014-02-19 23:34:44 2014-02-20 08:00:03

Reading your desired format from date class with strftime使用strftimedate class读取您想要的格式

df_date %>% 
  summarise(across(starts_with("date"), strftime, format="%Y-%m-%d %H:%M:%OS3"))
# A tibble: 3 × 2
  date1                   date2                  
  <chr>                   <chr>                  
1 2013-11-26 00:10:12.536 2013-11-26 07:06:40.720
2 2013-11-26 23:04:32.512 2013-11-27 07:09:50.552
3 2014-02-19 23:34:44.459 2014-02-20 08:00:03.974

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM