Home > OS >  Using R, can I take these monthly targets and split them into daily targets?
Using R, can I take these monthly targets and split them into daily targets?

Time:06-02

I would like to take the following table: Targets

and split the data into daily targets, based on the month and the year, so that leap years are taken into consideration using R?

Thanks for any help!!!

CodePudding user response:

You can use days_in_month from lubridate to get the correct number of days in each month, including leap years. You can then divide the target by this number.

library(tidyverse)
library(lubridate)

df %>% 
  group_by(across(everything())) %>%
  summarize(day = seq(days_in_month(
    as.POSIXct(paste(year, match(month, month.name), 1, sep = "-")))),
    .groups = "drop") %>%
  group_by(year, month) %>%
  mutate(daily_target = target_ticket_click / n()) %>%
  ungroup() %>%
  select(-target_ticket_click) %>%
  as.data.frame()

#>         month year day daily_target
#> 1       April 2023   1     278.9667
#> 2       April 2023   2     278.9667
#> 3       April 2023   3     278.9667
#> 4       April 2023   4     278.9667
#> 5       April 2023   5     278.9667
#> 6       April 2023   6     278.9667
#> 7       April 2023   7     278.9667
#> 8       April 2023   8     278.9667
#> 9       April 2023   9     278.9667
#> 10      April 2023  10     278.9667
#> 11      April 2023  11     278.9667
#> 12      April 2023  12     278.9667
#> 13      April 2023  13     278.9667
#> 14      April 2023  14     278.9667
#> 15      April 2023  15     278.9667
#> 16      April 2023  16     278.9667
#> 17      April 2023  17     278.9667
#> 18      April 2023  18     278.9667
#> 19      April 2023  19     278.9667
#> 20      April 2023  20     278.9667
#> 21      April 2023  21     278.9667
#> 22      April 2023  22     278.9667
#> 23      April 2023  23     278.9667
#> 24      April 2023  24     278.9667
#> 25      April 2023  25     278.9667
#> 26      April 2023  26     278.9667
#> 27      April 2023  27     278.9667
#> 28      April 2023  28     278.9667
#> 29      April 2023  29     278.9667
#> 30      April 2023  30     278.9667
#> 31     August 2022   1     256.9677
#> 32     August 2022   2     256.9677
#> 33     August 2022   3     256.9677
#> 34     August 2022   4     256.9677
#> 35     August 2022   5     256.9677
#> 36     August 2022   6     256.9677
#> 37     August 2022   7     256.9677
#> 38     August 2022   8     256.9677
#> 39     August 2022   9     256.9677
#> 40     August 2022  10     256.9677
#> 41     August 2022  11     256.9677
#> 42     August 2022  12     256.9677
#> 43     August 2022  13     256.9677
#> 44     August 2022  14     256.9677
#> 45     August 2022  15     256.9677
#> 46     August 2022  16     256.9677
#> 47     August 2022  17     256.9677
#> 48     August 2022  18     256.9677
#> 49     August 2022  19     256.9677
#> 50     August 2022  20     256.9677
#> 51     August 2022  21     256.9677
#> 52     August 2022  22     256.9677
#> 53     August 2022  23     256.9677
#> 54     August 2022  24     256.9677
#> 55     August 2022  25     256.9677
#> 56     August 2022  26     256.9677
#> 57     August 2022  27     256.9677
#> 58     August 2022  28     256.9677
#> 59     August 2022  29     256.9677
#> 60     August 2022  30     256.9677
#> 61     August 2022  31     256.9677
#> 62   December 2022   1     500.6452
#> 63   December 2022   2     500.6452
#> 64   December 2022   3     500.6452
#> 65   December 2022   4     500.6452
#> 66   December 2022   5     500.6452
#> 67   December 2022   6     500.6452
#> 68   December 2022   7     500.6452
#> 69   December 2022   8     500.6452
#> 70   December 2022   9     500.6452
#> 71   December 2022  10     500.6452
#> 72   December 2022  11     500.6452
#> 73   December 2022  12     500.6452
#> 74   December 2022  13     500.6452
#> 75   December 2022  14     500.6452
#> 76   December 2022  15     500.6452
#> 77   December 2022  16     500.6452
#> 78   December 2022  17     500.6452
#> 79   December 2022  18     500.6452
#> 80   December 2022  19     500.6452
#> 81   December 2022  20     500.6452
#> 82   December 2022  21     500.6452
#> 83   December 2022  22     500.6452
#> 84   December 2022  23     500.6452
#> 85   December 2022  24     500.6452
#> 86   December 2022  25     500.6452
#> 87   December 2022  26     500.6452
#> 88   December 2022  27     500.6452
#> 89   December 2022  28     500.6452
#> 90   December 2022  29     500.6452
#> 91   December 2022  30     500.6452
#> 92   December 2022  31     500.6452
#> 93   February 2023   1     295.3929
#> 94   February 2023   2     295.3929
#> 95   February 2023   3     295.3929
#> 96   February 2023   4     295.3929
#> 97   February 2023   5     295.3929
#> 98   February 2023   6     295.3929
#> 99   February 2023   7     295.3929
#> 100  February 2023   8     295.3929
#> 101  February 2023   9     295.3929
#> 102  February 2023  10     295.3929
#> 103  February 2023  11     295.3929
#> 104  February 2023  12     295.3929
#> 105  February 2023  13     295.3929
#> 106  February 2023  14     295.3929
#> 107  February 2023  15     295.3929
#> 108  February 2023  16     295.3929
#> 109  February 2023  17     295.3929
#> 110  February 2023  18     295.3929
#> 111  February 2023  19     295.3929
#> 112  February 2023  20     295.3929
#> 113  February 2023  21     295.3929
#> 114  February 2023  22     295.3929
#> 115  February 2023  23     295.3929
#> 116  February 2023  24     295.3929
#> 117  February 2023  25     295.3929
#> 118  February 2023  26     295.3929
#> 119  February 2023  27     295.3929
#> 120  February 2023  28     295.3929
#> 121   January 2023   1     265.1613
#> 122   January 2023   2     265.1613
#> 123   January 2023   3     265.1613
#> 124   January 2023   4     265.1613
#> 125   January 2023   5     265.1613
#> 126   January 2023   6     265.1613
#> 127   January 2023   7     265.1613
#> 128   January 2023   8     265.1613
#> 129   January 2023   9     265.1613
#> 130   January 2023  10     265.1613
#> 131   January 2023  11     265.1613
#> 132   January 2023  12     265.1613
#> 133   January 2023  13     265.1613
#> 134   January 2023  14     265.1613
#> 135   January 2023  15     265.1613
#> 136   January 2023  16     265.1613
#> 137   January 2023  17     265.1613
#> 138   January 2023  18     265.1613
#> 139   January 2023  19     265.1613
#> 140   January 2023  20     265.1613
#> 141   January 2023  21     265.1613
#> 142   January 2023  22     265.1613
#> 143   January 2023  23     265.1613
#> 144   January 2023  24     265.1613
#> 145   January 2023  25     265.1613
#> 146   January 2023  26     265.1613
#> 147   January 2023  27     265.1613
#> 148   January 2023  28     265.1613
#> 149   January 2023  29     265.1613
#> 150   January 2023  30     265.1613
#> 151   January 2023  31     265.1613
#> 152      July 2022   1     255.2903
#> 153      July 2022   2     255.2903
#> 154      July 2022   3     255.2903
#> 155      July 2022   4     255.2903
#> 156      July 2022   5     255.2903
#> 157      July 2022   6     255.2903
#> 158      July 2022   7     255.2903
#> 159      July 2022   8     255.2903
#> 160      July 2022   9     255.2903
#> 161      July 2022  10     255.2903
#> 162      July 2022  11     255.2903
#> 163      July 2022  12     255.2903
#> 164      July 2022  13     255.2903
#> 165      July 2022  14     255.2903
#> 166      July 2022  15     255.2903
#> 167      July 2022  16     255.2903
#> 168      July 2022  17     255.2903
#> 169      July 2022  18     255.2903
#> 170      July 2022  19     255.2903
#> 171      July 2022  20     255.2903
#> 172      July 2022  21     255.2903
#> 173      July 2022  22     255.2903
#> 174      July 2022  23     255.2903
#> 175      July 2022  24     255.2903
#> 176      July 2022  25     255.2903
#> 177      July 2022  26     255.2903
#> 178      July 2022  27     255.2903
#> 179      July 2022  28     255.2903
#> 180      July 2022  29     255.2903
#> 181      July 2022  30     255.2903
#> 182      July 2022  31     255.2903
#> 183      June 2022   1     497.7000
#> 184      June 2022   2     497.7000
#> 185      June 2022   3     497.7000
#> 186      June 2022   4     497.7000
#> 187      June 2022   5     497.7000
#> 188      June 2022   6     497.7000
#> 189      June 2022   7     497.7000
#> 190      June 2022   8     497.7000
#> 191      June 2022   9     497.7000
#> 192      June 2022  10     497.7000
#> 193      June 2022  11     497.7000
#> 194      June 2022  12     497.7000
#> 195      June 2022  13     497.7000
#> 196      June 2022  14     497.7000
#> 197      June 2022  15     497.7000
#> 198      June 2022  16     497.7000
#> 199      June 2022  17     497.7000
#> 200      June 2022  18     497.7000
#> 201      June 2022  19     497.7000
#> 202      June 2022  20     497.7000
#> 203      June 2022  21     497.7000
#> 204      June 2022  22     497.7000
#> 205      June 2022  23     497.7000
#> 206      June 2022  24     497.7000
#> 207      June 2022  25     497.7000
#> 208      June 2022  26     497.7000
#> 209      June 2022  27     497.7000
#> 210      June 2022  28     497.7000
#> 211      June 2022  29     497.7000
#> 212      June 2022  30     497.7000
#> 213     March 2023   1     268.3226
#> 214     March 2023   2     268.3226
#> 215     March 2023   3     268.3226
#> 216     March 2023   4     268.3226
#> 217     March 2023   5     268.3226
#> 218     March 2023   6     268.3226
#> 219     March 2023   7     268.3226
#> 220     March 2023   8     268.3226
#> 221     March 2023   9     268.3226
#> 222     March 2023  10     268.3226
#> 223     March 2023  11     268.3226
#> 224     March 2023  12     268.3226
#> 225     March 2023  13     268.3226
#> 226     March 2023  14     268.3226
#> 227     March 2023  15     268.3226
#> 228     March 2023  16     268.3226
#> 229     March 2023  17     268.3226
#> 230     March 2023  18     268.3226
#> 231     March 2023  19     268.3226
#> 232     March 2023  20     268.3226
#> 233     March 2023  21     268.3226
#> 234     March 2023  22     268.3226
#> 235     March 2023  23     268.3226
#> 236     March 2023  24     268.3226
#> 237     March 2023  25     268.3226
#> 238     March 2023  26     268.3226
#> 239     March 2023  27     268.3226
#> 240     March 2023  28     268.3226
#> 241     March 2023  29     268.3226
#> 242     March 2023  30     268.3226
#> 243     March 2023  31     268.3226
#> 244       May 2022   1     478.5161
#> 245       May 2022   2     478.5161
#> 246       May 2022   3     478.5161
#> 247       May 2022   4     478.5161
#> 248       May 2022   5     478.5161
#> 249       May 2022   6     478.5161
#> 250       May 2022   7     478.5161
#> 251       May 2022   8     478.5161
#> 252       May 2022   9     478.5161
#> 253       May 2022  10     478.5161
#> 254       May 2022  11     478.5161
#> 255       May 2022  12     478.5161
#> 256       May 2022  13     478.5161
#> 257       May 2022  14     478.5161
#> 258       May 2022  15     478.5161
#> 259       May 2022  16     478.5161
#> 260       May 2022  17     478.5161
#> 261       May 2022  18     478.5161
#> 262       May 2022  19     478.5161
#> 263       May 2022  20     478.5161
#> 264       May 2022  21     478.5161
#> 265       May 2022  22     478.5161
#> 266       May 2022  23     478.5161
#> 267       May 2022  24     478.5161
#> 268       May 2022  25     478.5161
#> 269       May 2022  26     478.5161
#> 270       May 2022  27     478.5161
#> 271       May 2022  28     478.5161
#> 272       May 2022  29     478.5161
#> 273       May 2022  30     478.5161
#> 274       May 2022  31     478.5161
#> 275  November 2022   1     924.8333
#> 276  November 2022   2     924.8333
#> 277  November 2022   3     924.8333
#> 278  November 2022   4     924.8333
#> 279  November 2022   5     924.8333
#> 280  November 2022   6     924.8333
#> 281  November 2022   7     924.8333
#> 282  November 2022   8     924.8333
#> 283  November 2022   9     924.8333
#> 284  November 2022  10     924.8333
#> 285  November 2022  11     924.8333
#> 286  November 2022  12     924.8333
#> 287  November 2022  13     924.8333
#> 288  November 2022  14     924.8333
#> 289  November 2022  15     924.8333
#> 290  November 2022  16     924.8333
#> 291  November 2022  17     924.8333
#> 292  November 2022  18     924.8333
#> 293  November 2022  19     924.8333
#> 294  November 2022  20     924.8333
#> 295  November 2022  21     924.8333
#> 296  November 2022  22     924.8333
#> 297  November 2022  23     924.8333
#> 298  November 2022  24     924.8333
#> 299  November 2022  25     924.8333
#> 300  November 2022  26     924.8333
#> 301  November 2022  27     924.8333
#> 302  November 2022  28     924.8333
#> 303  November 2022  29     924.8333
#> 304  November 2022  30     924.8333
#> 305   October 2022   1     338.2903
#> 306   October 2022   2     338.2903
#> 307   October 2022   3     338.2903
#> 308   October 2022   4     338.2903
#> 309   October 2022   5     338.2903
#> 310   October 2022   6     338.2903
#> 311   October 2022   7     338.2903
#> 312   October 2022   8     338.2903
#> 313   October 2022   9     338.2903
#> 314   October 2022  10     338.2903
#> 315   October 2022  11     338.2903
#> 316   October 2022  12     338.2903
#> 317   October 2022  13     338.2903
#> 318   October 2022  14     338.2903
#> 319   October 2022  15     338.2903
#> 320   October 2022  16     338.2903
#> 321   October 2022  17     338.2903
#> 322   October 2022  18     338.2903
#> 323   October 2022  19     338.2903
#> 324   October 2022  20     338.2903
#> 325   October 2022  21     338.2903
#> 326   October 2022  22     338.2903
#> 327   October 2022  23     338.2903
#> 328   October 2022  24     338.2903
#> 329   October 2022  25     338.2903
#> 330   October 2022  26     338.2903
#> 331   October 2022  27     338.2903
#> 332   October 2022  28     338.2903
#> 333   October 2022  29     338.2903
#> 334   October 2022  30     338.2903
#> 335   October 2022  31     338.2903
#> 336 September 2022   1     267.2333
#> 337 September 2022   2     267.2333
#> 338 September 2022   3     267.2333
#> 339 September 2022   4     267.2333
#> 340 September 2022   5     267.2333
#> 341 September 2022   6     267.2333
#> 342 September 2022   7     267.2333
#> 343 September 2022   8     267.2333
#> 344 September 2022   9     267.2333
#> 345 September 2022  10     267.2333
#> 346 September 2022  11     267.2333
#> 347 September 2022  12     267.2333
#> 348 September 2022  13     267.2333
#> 349 September 2022  14     267.2333
#> 350 September 2022  15     267.2333
#> 351 September 2022  16     267.2333
#> 352 September 2022  17     267.2333
#> 353 September 2022  18     267.2333
#> 354 September 2022  19     267.2333
#> 355 September 2022  20     267.2333
#> 356 September 2022  21     267.2333
#> 357 September 2022  22     267.2333
#> 358 September 2022  23     267.2333
#> 359 September 2022  24     267.2333
#> 360 September 2022  25     267.2333
#> 361 September 2022  26     267.2333
#> 362 September 2022  27     267.2333
#> 363 September 2022  28     267.2333
#> 364 September 2022  29     267.2333
#> 365 September 2022  30     267.2333

Created on 2022-06-01 by the reprex package (v2.0.1)


Data used - transcribed from image in question

df <- data.frame(month = c(month.name[5:12], month.name[1:4]),
                 year = c(rep(2022, 8), rep(2023, 4)),
                 target_ticket_click = c(14834, 14931, 7914, 7966, 8017, 10487, 
                                         27745, 15520, 8220, 8271, 8318, 8369))

df
#>        month year target_ticket_click
#> 1        May 2022               14834
#> 2       June 2022               14931
#> 3       July 2022                7914
#> 4     August 2022                7966
#> 5  September 2022                8017
#> 6    October 2022               10487
#> 7   November 2022               27745
#> 8   December 2022               15520
#> 9    January 2023                8220
#> 10  February 2023                8271
#> 11     March 2023                8318
#> 12     April 2023                8369
  •  Tags:  
  • r
  • Related