我有一个带有日期列的dataframe
,该列名为dateDecision
。它们的格式为1970/01/01
。我正在尝试将日期分为year
,month
和date
列。我使用了以下代码
df %>% mutate(year = lubridate::year(dateDecision),
month = lubridate::month(dateDecision),
day = lubridate::day(dateDecision))
我收到一条错误消息,指出as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format
中的错误
当我使用dput(head(df,10))时,这是输出
structure(list(term = c("1791", "1791", "1791", "1791", "1791",
"1792", "1792", "1792", "1792", "1792"), dateDecision = c("8/3/1791",
"8/3/1791", "8/3/1791", "8/3/1791", "8/3/1791", "8/11/1792",
"8/11/1792", "8/11/1792", "8/11/1792", "8/11/1792"), decisionType = c("6",
"6", "6", "6", "6", "8", "8", "8", "8", "8"), dateArgument = c("8/2/1791",
"8/2/1791", "8/2/1791", "8/2/1791", "8/2/1791", "8/9/1792", "8/9/1792",
"8/9/1792", "8/9/1792", "8/9/1792")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
我该如何解决?
答案 0 :(得分:1)
您首先需要使用lubridate将dateDecision
列转换为日期
df %>% mutate(dateDecision=lubridate::mdy(dateDecision),
year = lubridate::year(dateDecision),
month = lubridate::month(dateDecision),
day = lubridate::day(dateDecision))
答案 1 :(得分:1)
首先使用anytime::anydate()
库将您的dateDecision
列转换为日期:
library(anytime)
date <- "1970/01/01"
anytime::anydate(date)
[1] "1970-01-01"
然后运行您的变异。您也可以使用tk_augment_timeseries_signature()
中的timetk
,并且仅选择year
,month
和date
列。
library(timetk)
library(tidyverse)
# make some reproducible data
dates <-
seq(as.Date('2019-01-01'), as.Date('2019-12-31'), by = 'days')
dates <- as_tibble(dates)
#add the time signature features including year, month, day, day of week, day of month, #day of year, hour, minute, second to the input data
dates %>%
tk_augment_timeseries_signature()