1

Could you help me to solve the following problem: note that in the table I generate, it appears twice for the day 05/07, but I would like it to show only once. How can I adjust this?

Thank you very much!

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","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","2021-07-05"),
       Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Saturday","Sunday","Monday","Monday"),
       DR01 = c(14,11,14,13,13,14,13,16,15,11,13), DR02= c(14,12,16,17,13,12,17,14,13,15,18),DR03= c(19,15,14,13,13,12,11,15,13,13,18),
       DR04 = c(15,14,13,13,16,12,11,19,11,12,11),DR05 = c(15,14,15,13,16,12,11,19,14,15,18),
       DR06 = c(21,14,13,13,15,16,17,18,12,12,18),DR07 = c(12,15,14,14,19,14,17,18,14,13,18)),
  class = "data.frame", row.names = c(NA, -11L))


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"))
# A tibble: 6 x 2
  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  27.9
6 05/07/2021  27.9
Antonio
  • 1,091
  • 7
  • 24
  • In dates 05/07/2021 is repeated. That's why you are getting this observation twice. Using a unique before dates will hopefully solve the problem. ```map_dfr( unique(dates), ~ { ...}) ``` – Ahad Zaman Sep 27 '21 at 21:59

1 Answers1

1

We may add distinct at the end

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")) %>%
  distinct()
# A tibble: 5 × 2
  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  27.9

Or if it is only for 'date'

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")) %>% 
     distinct(dates, .keep_all = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks akrun! I have a quick question, I know it's not part of the answer. But I wanted to export this table in pdf format, is there this possibility? If you can't answer, then I'll ask you a new question! – Antonio Sep 27 '21 at 20:14
  • 1
    @Jose You can wrap it in `gt` or flextable and do a knit with rmarkdown to export as pdf or html – akrun Sep 27 '21 at 20:15
  • 1
    @Jose you may check [here](https://stackoverflow.com/questions/14321127/writing-data-frame-to-pdf-table) – akrun Sep 27 '21 at 20:17
  • could you please take a look at this question? https://stackoverflow.com/questions/69586743/generate-graphs-for-dates-that-are-not-in-my-database I believe you can help, you once helped my brother in a very similar question. Thank you in advance! – Antonio Oct 15 '21 at 17:56
  • 1
    @Antonio Sure, I will look at it. DId you changed your username – akrun Oct 15 '21 at 17:57
  • My name is Antonio Jose. When I registered, I had only placed Jose. – Antonio Oct 15 '21 at 17:58
  • Thanks @akrun for checking the question! Even if you can't solve it, you could give your opinion about what could be done. One person even replied, but it was still a little vague for me. – Antonio Oct 15 '21 at 18:27
  • 1
    @Antonio I will check it out once I get some time. Thanks – akrun Oct 15 '21 at 18:28