将字符日期时间转换为日期时间

时间:2018-01-17 17:31:04

标签: r datetime data.table

我想创建一个以日期时间格式转换字符日期时间的函数。

此函数只包含一个参数:dataframe

日期示例:“1/06/2017 11:10:51”

我测试了类似的东西:

character_to_date <- function(df){
  for(i in which(str_detect(colnames(df),"DATE"))){
    df[,i] <- as.Date(df[,i], "%d/%m/%Y %H:%M:%S")
  }
}

但我有这个错误:

Error in `[.data.table`(df, , i) : 
j (the 2nd argument inside [...]) is a single symbol but column name 
'i' is not found. Perhaps you intended DT[,..i] or DT[,i,with=FALSE]. 
This difference to data.frame is deliberate and explained in FAQ 1.1.

我怎样才能改善这种转变?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

由于您正在使用data.table,请尝试以下操作:

数据

dput(head(pp))
structure(list(DaTe = structure(c(1516213800, 1516217400, 1516221000, 
                                        1516224600, 1516228200, 1516231800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                     degc = c(9.31, 8.37, 7.5, 6.76, 6.14, 5.58), 
                     rh = c(0.5, 0.53, 0.56, 0.57, 0.58, 0.59), 
                     mslp = c(1016.46, 1016.52, 1016.53, 1016.52, 1016.5, 1016.52), 
                     rain = c(0, 0, 0, 0, 0, 0), 
                     snow = c(0, 0, 0, 0, 0, 0), 
                     type = c("darksky", "darksky","darksky", "darksky", "darksky", "darksky")), 
                .Names = c("DaTe", "degc", "rh", "mslp", "rain", "snow", "type"), 
                class = c("data.table", "data.frame"))

<强> CODE

date_func <- function(z){
  todate <- function(q){
    return(as.POSIXct(q, format = "%Y-%m-%d %H:%M:%S", tz = "UTC"))
  }
  colnum <- grep("date", tolower(colnames(z)))
  z[, (colnum) := lapply(.SD, todate), .SDcols = colnum]
  return(z)
}

<强>输出

ww <- date_func(copy(pp))

> str(ww)
Classes ‘data.table’ and 'data.frame':  169 obs. of  7 variables:
 $ DaTe: POSIXct, format: "2018-01-17 18:30:00" "2018-01-17 19:30:00" "2018-01-17 20:30:00" "2018-01-17 21:30:00" ...
 $ degc: num  9.31 8.37 7.5 6.76 6.14 5.58 4.87 4.07 3.86 5.14 ...
 $ rh  : num  0.5 0.53 0.56 0.57 0.58 0.59 0.6 0.61 0.6 0.53 ...
 $ mslp: num  1016 1017 1017 1017 1016 ...
 $ rain: num  0 0 0 0 0 0 0 0 0 0 ...
 $ snow: num  0 0 0 0 0 0 0 0 0 0 ...
 $ type: chr  "darksky" "darksky" "darksky" "darksky" ...
 - attr(*, ".internal.selfref")=<externalptr> 

鉴于pp,我们有:

> str(pp)
Classes ‘data.table’ and 'data.frame':  169 obs. of  7 variables:
 $ DaTe: chr  "2018-01-17 18:30:00" "2018-01-17 19:30:00" "2018-01-17 20:30:00" "2018-01-17 21:30:00" ...
 $ degc: num  9.31 8.37 7.5 6.76 6.14 5.58 4.87 4.07 3.86 5.14 ...
 $ rh  : num  0.5 0.53 0.56 0.57 0.58 0.59 0.6 0.61 0.6 0.53 ...
 $ mslp: num  1016 1017 1017 1017 1016 ...
 $ rain: num  0 0 0 0 0 0 0 0 0 0 ...
 $ snow: num  0 0 0 0 0 0 0 0 0 0 ...
 $ type: chr  "darksky" "darksky" "darksky" "darksky" ...
 - attr(*, ".internal.selfref")=<externalptr> 

请记住以下内容:

  1. 我使用tolower,因为列名可能有不同的情况。
  2. 我在函数调用中使用copy(pp)而不是pp。这是因为data.table通过引用更新。不使用它会直接对pp进行更改 - 如果这是你想要的,那也没关系。在这种情况下,wwpp都会引用已修改的pp
  3. 希望这有帮助!

答案 1 :(得分:0)

docker run --name registr \ -v ~/v1:/v1 \ -v ~/logging.yaml:/root/logging.yaml \ -v ~/.aws:/root/.aws \ -v ~/luigi.cfg:/root/luigi.cfg \ -v ~/params:/root/params \ -p 8082:8082 \ simonm3/registr echo "docker finished" The docker image has CMD ["python", "/root/worker/start.py"] 包中的函数mdy_hms已经为您执行此操作,如果您将其与lubridate包中的mutate_at结合使用,那么您可以执行此操作包含字符串dplyr的列。

DATE

如果你想让它成为多次使用的功能,你可以这样做:

df %>% mutate_at(.vars = vars(contains("DATE")), .funs = lubridate::mdy_hms)

然后运行:

my_func <- function(data){
  df %>% 
    mutate_at(.vars = vars(contains("DATE")), .funs = lubridate::mdy_hms)
}