如何根据一天中的时间从数据帧中导出 2 个数据帧?

How to derive 2 dataframes from a dataframe based on the time of the day?

我有一个 df(请参阅下面的可重现数据):

library(tidyverse)
df
# A tibble: 6 × 2
     id date               
  <int> <dttm>             
1     1 2015-03-14 17:18:31
2     2 2015-03-14 17:18:56
3     3 2015-03-26 17:18:32
4     4 2015-03-26 17:18:56
5     5 2015-04-07 17:18:32
6     6 2015-04-07 17:18:57

现在,使用这个 df 我想创建 2 个新的 dataframestibbles - df1df2.

我想要实现的是编写一个整洁的代码,检查同一天是否有 2 行(年月日相同),如果有,则较早的行(检查时间当天)转到第一个 df df1,后面一行转到第二个 df,df2.

df1 的期望输出:

# A tibble: 3 × 2
     id date               
  <int> <dttm>             
1     1 2015-03-14 17:18:31
2     3 2015-03-26 17:18:32
3     5 2015-04-07 17:18:32

df2 的期望输出:

# A tibble: 3 × 2
     id date               
  <int> <dttm>             
1     2 2015-03-14 17:18:56
2     4 2015-03-26 17:18:56
3     6 2015-04-07 17:18:57

P.S:我特别想知道 tidyverse 解决这个问题的方法,但我也很好奇如何使用基数 R.

可重现数据:

structure(list(id = 1:6, date = structure(c(1426353511, 1426353536, 
1427390312, 1427390336, 1428427112, 1428427137), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))
library(data.table) #for the convenience of the as.IDate-function
# split by duplicate-status
L <- split(df, duplicated(as.IDate(df$date)))
# set names
names(L) <- c("df1", "df2")
# pust list's element to current environment (use names of list as objects' names)
list2env(L, envir = environment())