Create tibble/dataframe based on hour of day

I want to create two tables, past_90_am, and past_90_pm for observations made before & after noon, respectively.

######## Working With Entire_Health_Database.tab file
######## Set working Directory
setwd("/Users/georgemanning/Documents/datafiles/R_Projects")
######
######
#Get the Tidyverse package
library(tidyverse)
#Get Lubridate package
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
##### Now we have to read the file & assign it to a tibble
#####
library(readxl)
Entire_Health_Database <- read_excel("Entire_Health_Database.xlsx", 
                                     col_types = c("date", "date", "numeric", 
                                                   "numeric", "numeric", "numeric", 
                                                   "numeric", "text"))
#View(Entire_Health_Database)

past_90 <- Entire_Health_Database %>% filter(Date >= today()- days(90))
###
penultimate_90 <- Entire_Health_Database %>% filter(Date >= today()-days(180) & Date <= today()- days(91))


### Next is to extract observations between 0000 & 1159 and those between 1200 & 2359
### I have no idea how to do this yet
## Want to create two new dataframes/tibbles, one containing observations from past_90
## made berfore noon and a second containing observations made after noon.


##  past_90 %>% filter("Time", hour()<12)
## The above doesn't work
## Still trying to figure this out

Created on 2019-07-04 by the reprex package (v0.3.0)

Hi!

To help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue and including sample data on a copy/paste friendly format? We don't have access to your .xlsx file

Please have a look at this guide, to see how to create one:

1 Like

Sorry! I'm new to RStudio & even newer to the Studio Community. I wand to select on time of day as described in the comments. I hope this is sufficient information:

## Goal is to select entries made before and after noon, creating two new
## dataframes: past_90_am and pqst_90_pm

## I can't figure out how to sort on hour.

tibble::tribble(
  ~Date,                 ~Time, ~Blood.Sugar, ~Systolic, ~Diastolic, ~Pulse, ~Weight,                     ~Comments,
  "2019-04-24", "1899-12-31 17:39:33",          104,       120,         68,     60,      NA, "Chose not to inject insulin",
  "2019-06-02", "1899-12-31 07:48:06",           92,       130,         71,     49,      NA,                            NA,
  "2019-04-12", "1899-12-31 18:04:05",          119,       115,         67,     55,      NA, "Injected 5 units of Novolog",
  "2019-04-16", "1899-12-31 17:42:34",          129,       112,         61,     56,      NA, "Injected 5 units of Novolog",
  "2019-06-27", "1899-12-31 05:51:41",          108,       128,         69,     50,      NA,                            NA,
  "2019-05-08", "1899-12-31 17:36:33",           97,       130,         72,     56,      NA,      "Did not inject insulin",
  "2019-04-27", "1899-12-31 17:41:14",          125,       124,         76,     56,      NA,        "\"average\" readings",
  "2019-05-31", "1899-12-31 05:47:15",           93,       125,         70,     55,      NA,                            NA,
  "2019-05-21", "1899-12-31 05:50:46",          132,       125,         67,     59,      NA,                            NA,
  "2019-05-26", "1899-12-31 17:23:11",          106,       134,         83,     59,      NA, "Chose not to inject insulin"
)
#> # A tibble: 10 x 8
#>    Date    Time    Blood.Sugar Systolic Diastolic Pulse Weight Comments    
#>    <chr>   <chr>         <dbl>    <dbl>     <dbl> <dbl> <lgl>  <chr>       
#>  1 2019-0… 1899-1…         104      120        68    60 NA     Chose not t…
#>  2 2019-0… 1899-1…          92      130        71    49 NA     <NA>        
#>  3 2019-0… 1899-1…         119      115        67    55 NA     Injected 5 …
#>  4 2019-0… 1899-1…         129      112        61    56 NA     Injected 5 …
#>  5 2019-0… 1899-1…         108      128        69    50 NA     <NA>        
#>  6 2019-0… 1899-1…          97      130        72    56 NA     Did not inj…
#>  7 2019-0… 1899-1…         125      124        76    56 NA     "\"average\…
#>  8 2019-0… 1899-1…          93      125        70    55 NA     <NA>        
#>  9 2019-0… 1899-1…         132      125        67    59 NA     <NA>        
#> 10 2019-0… 1899-1…         106      134        83    59 NA     Chose not t…

Created on 2019-07-04 by the reprex package (v0.3.0)

This is how you can filter your dataframe but I would recommend keeping all in the same dataframe and add a new variable to differentiate.

library(dplyr)
library(lubridate)

past_90 <- tibble::tribble(
    ~Date,                 ~Time, ~Blood.Sugar, ~Systolic, ~Diastolic, ~Pulse, ~Weight,                     ~Comments,
    "2019-04-24", "1899-12-31 17:39:33",          104,       120,         68,     60,      NA, "Chose not to inject insulin",
    "2019-06-02", "1899-12-31 07:48:06",           92,       130,         71,     49,      NA,                            NA,
    "2019-04-12", "1899-12-31 18:04:05",          119,       115,         67,     55,      NA, "Injected 5 units of Novolog",
    "2019-04-16", "1899-12-31 17:42:34",          129,       112,         61,     56,      NA, "Injected 5 units of Novolog",
    "2019-06-27", "1899-12-31 05:51:41",          108,       128,         69,     50,      NA,                            NA,
    "2019-05-08", "1899-12-31 17:36:33",           97,       130,         72,     56,      NA,      "Did not inject insulin",
    "2019-04-27", "1899-12-31 17:41:14",          125,       124,         76,     56,      NA,        "\"average\" readings",
    "2019-05-31", "1899-12-31 05:47:15",           93,       125,         70,     55,      NA,                            NA,
    "2019-05-21", "1899-12-31 05:50:46",          132,       125,         67,     59,      NA,                            NA,
    "2019-05-26", "1899-12-31 17:23:11",          106,       134,         83,     59,      NA, "Chose not to inject insulin"
)

past_90 %>% 
    filter(hour(Time) < 12)
#> # A tibble: 4 x 8
#>   Date     Time        Blood.Sugar Systolic Diastolic Pulse Weight Comments
#>   <chr>    <chr>             <dbl>    <dbl>     <dbl> <dbl> <lgl>  <chr>   
#> 1 2019-06… 1899-12-31…          92      130        71    49 NA     <NA>    
#> 2 2019-06… 1899-12-31…         108      128        69    50 NA     <NA>    
#> 3 2019-05… 1899-12-31…          93      125        70    55 NA     <NA>    
#> 4 2019-05… 1899-12-31…         132      125        67    59 NA     <NA>

# I recommend this instead
past_90 %>% 
    mutate(noon = case_when(hour(Time) < 12 ~ "before",
                            TRUE ~ "after")) %>% 
    arrange(noon, Time) %>% 
    select(noon, everything())
#> # A tibble: 10 x 9
#>    noon   Date   Time  Blood.Sugar Systolic Diastolic Pulse Weight Comments
#>    <chr>  <chr>  <chr>       <dbl>    <dbl>     <dbl> <dbl> <lgl>  <chr>   
#>  1 after  2019-… 1899…         106      134        83    59 NA     Chose n…
#>  2 after  2019-… 1899…          97      130        72    56 NA     Did not…
#>  3 after  2019-… 1899…         104      120        68    60 NA     Chose n…
#>  4 after  2019-… 1899…         125      124        76    56 NA     "\"aver…
#>  5 after  2019-… 1899…         129      112        61    56 NA     Injecte…
#>  6 after  2019-… 1899…         119      115        67    55 NA     Injecte…
#>  7 before 2019-… 1899…          93      125        70    55 NA     <NA>    
#>  8 before 2019-… 1899…         132      125        67    59 NA     <NA>    
#>  9 before 2019-… 1899…         108      128        69    50 NA     <NA>    
#> 10 before 2019-… 1899…          92      130        71    49 NA     <NA>
2 Likes

ifelse/if_else gets overused a lot in dplyr because you can't assign to a subset, but this is the perfect case for it:

ifelse(hour(Time) < 12, "before", "after")

THANK YOU!!

Problem solved!

This was a typical newb error.I thought the dplyr package was loaded as part of tidyverse.

Obviously not.

Now I can proceed with statistical analysis & charting.

...it is. Did you mean lubridate?

I loaded tidyverse & lubridate at the start.

After looking at the suggested solutions, I specifically loaded dplyr.

Then everything ran properly

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

...loading tidyverse loads dplyr, always (it tells you when it does!), so something else must have changed.

Thank you,

I double checked and my only conclusion is that I must have been making a typo that I didn't notice.

Everything running as expected so once again, thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.