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

Time:11-05

I have a df (please see below for reproducible data):

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

Now, using this df I want to create 2 new dataframes or tibbles - df1 and df2.

What I want to achieve is to write a tidy code, that checks if there are 2 rows for the same day (year, month and day are the same) and if there is, the earlier row (checking the time of the day) goes to the first df df1 and the later row goes to the second df, df2.

The desired output for 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

The desired output for 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: I am specifically interested in knowing the tidyverse approach to this problem, but I am also very curious how it would be done using base R.

Reproducible data:

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"))

CodePudding user response:

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())
  • Related