我有这个小组:
package
旨在使用# A tibble: 541,909 x 7
InvoiceNo StockCode Quantity InvoiceDate UnitPrice CustomerID
<chr> <chr> <int> <dttm> <dbl> <int>
1 536365 85123A 6 2010-01-12 08:26:00 2.55 17850
2 536365 71053 6 2010-01-12 08:26:00 3.39 17850
3 536365 84406B 8 2010-01-12 08:26:00 2.75 17850
4 536365 84029G 6 2010-01-12 08:26:00 3.39 17850
5 536365 84029E 6 2010-01-12 08:26:00 3.39 17850
6 536365 22752 2 2010-01-12 08:26:00 7.65 17850
7 536365 21730 6 2010-01-12 08:26:00 4.25 17850
8 536366 22633 6 2010-01-12 08:28:00 1.85 17850
9 536366 22632 6 2010-01-12 08:28:00 1.85 17850
10 536367 84879 32 2010-01-12 08:34:00 1.69 13047
中的separate()
分割InvoiceDate列。
tidyr
我想知道有没有办法保留新日期和时间col的dttm格式?
retail %>%
separate(InvoiceDate, c("date", "time") , sep = " ", convert = FALSE)
答案 0 :(得分:1)
我假设您希望将列保留为可以识别为日期或时间的列,以便您可以根据它们进行进一步的计算。因此,我建议跳过separate
并使用mutate
以及“chron”或“lubridate”中的函数来处理“时间”列。
以下是一些例子:
library(chron)
mydf %>%
mutate(Date = as.Date(DateTime),
Time = times(format(DateTime, "%H:%M:%S"))) %>%
select(-DateTime)
# # A tibble: 5 x 3
# ID Date Time
# <int> <date> <S3: times>
# 1 1 2017-12-25 20:41:21
# 2 2 2017-12-25 20:41:22
# 3 3 2017-12-25 20:41:23
# 4 4 2017-12-25 20:41:24
# 5 5 2017-12-25 20:41:25
library(lubridate)
mydf %>%
mutate(Date = as.Date(DateTime),
Time = hms(format(DateTime, "%H:%M:%S"))) %>%
select(-DateTime)
# # A tibble: 5 x 3
# ID Date Time
# <int> <date> <S4: Period>
# 1 1 2017-12-25 20H 41M 21S
# 2 2 2017-12-25 20H 41M 22S
# 3 3 2017-12-25 20H 41M 23S
# 4 4 2017-12-25 20H 41M 24S
# 5 5 2017-12-25 20H 41M 25S
以下是一些可供使用的示例数据:
mydf <- structure(list(ID = 1:5, DateTime = structure(c(1514214681.99041,
1514214682.99041, 1514214683.99041, 1514214684.99041, 1514214685.99041),
class = c("POSIXct", "POSIXt"))), .Names = c("ID", "DateTime"),
row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
mydf
## # A tibble: 5 x 2
## ID DateTime
## <int> <dttm>
## 1 1 2017-12-25 20:41:21
## 2 2 2017-12-25 20:41:22
## 3 3 2017-12-25 20:41:23
## 4 4 2017-12-25 20:41:24
## 5 5 2017-12-25 20:41:25