Home > front end >  How to generate table until the specific date in R
How to generate table until the specific date in R

Time:09-28

I have the following problem: How can I generate the table only until the date 03/07, instead of until 05/07.

Executable code below:

library(purrr)
library(dplyr)
library(tidyverse)
library(lubridate)

df1 <- structure(
  list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
                 "2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-04-02","2021-04-03","2021-04-08","2021-04-09","2021-04-10","2021-07-01","2021-07-02","2021-07-03","2021-07-04","2021-07-05"),
       Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Saturday","Sunday","Monday"),
       DR01 = c(14,11,14,13,13,14,13,16,15,12), DR02= c(14,12,16,17,13,12,17,14,13,13),DR03= c(19,15,14,13,13,12,11,15,13,13),
       DR04 = c(15,14,13,13,16,12,11,19,11,13),DR05 = c(15,14,15,13,16,12,11,19,14,13),
       DR06 = c(21,14,13,13,15,16,17,18,12,11),DR07 = c(12,15,14,14,19,14,17,18,14,13)),
  class = "data.frame", row.names = c(NA, -10L))


dates <- subset(df1, date2 > date1, select = date2)$date2
map_dfr(dates, ~ {
  
  datas <- df1 %>%
    filter(date2 == ymd(.x)) %>%
    summarize(across(starts_with("DR"), sum)) %>%
    pivot_longer(everything(), names_pattern = "DR(. )", values_to = "val") %>%
    mutate(name = as.numeric(name))
  colnames(datas)<-c("Days","Numbers")
  mod <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
  tibble(dates = .x, coef = coef(mod)[2])
}) %>%
  mutate(dates = format(ymd(dates), "%d/%m/%Y"))
  dates       coef
  <chr>      <dbl>
1 01/07/2021  12.2
2 02/07/2021  12.4
3 03/07/2021  15.6
4 04/07/2021  13.3
5 05/07/2021  12.7

CodePudding user response:

In the subset step, add one more condition with &

dates <- subset(df1, date2 > date1 & date2 <= "2021-07-03", select = date2)$date2
  •  Tags:  
  • r
  • Related