1

I have a set of data giving information about the daily precipitation between 2013-01-01 and 2022-12-31.

         Date Precipitation_value
1  2013-01-01                 3.7
2  2013-01-02                 0.1
3  2013-01-03                 0.6
4  2013-01-04                 0.0
df <- structure(list(Date = c("2013-01-01", "2013-01-02", "2013-01-03", 
"2013-01-04"), Precipitation_value = c(3.7, 0.1, 0.6, 0)), class = "data.frame", row.names = c("1", 
"2", "3", "4"))

For every year I want to extract the periods between 15th March and 30th May in order to take a closer look at these periods.

Can you tell me how to extract this data or recommend tutorials on this topic?

Unfortunatly, I didn't have any idea how to perform the data extraction.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Marle Mü
  • 17
  • 3

3 Answers3

2

We could do it this way:

library(lubridate)
library(dplyr)

df %>%
  mutate(Date = ymd(Date),
         year = year(Date)) %>% 
  group_by(year) %>% 
  filter(between(Date, ymd(paste0(year(Date), "-03-15")), ymd(paste0(year(Date), "-03-30"))))

fake data:

Date Precipitation_value
1 2013-01-01 3.7 
2 2013-01-02 0.1 
3 2013-03-16 0.6 
4 2013-01-04 0.0

output:

Date       Precipitation_value  year
  <date>                   <dbl> <dbl>
1 2013-03-16                 0.6  2013
TarJae
  • 72,363
  • 6
  • 19
  • 66
0

library(lubridate)

library(dplyr)

df %>% filter(between(day(date),15,31),between(month(date),3,5),paste0(day(date),month(date))!="315")

Another solution using base R with the help of lubridate package.

df[day(df$date) >=15 & month(df$date) %in% 3:5 & paste0(day(df$date),month(df$date))!="315" ,]

0

We can use substring starting at 6th character to subset the dataset. No librarys or date format needed (latter may be recommended).

ds <- substring(dat$date, 6)
res <- dat[ds >= '03-15' & ds <= '05-30', ]

res
#           date prec
# 75  2020-03-15  0.8
# 76  2020-03-16  2.9
# 77  2020-03-17  0.0
# 78  2020-03-18  1.5
# 79  2020-03-19  2.1
# 80  2020-03-20  0.0
# 81  2020-03-21  2.3
# 82  2020-03-22  0.6
# 83  2020-03-23  1.4
# 84  2020-03-24  2.6
# 85  2020-03-25  3.1
# 86  2020-03-26  2.3
# 87  2020-03-27  0.9
# 88  2020-03-28  0.4
# 89  2020-03-29  0.3
# 90  2020-03-30  1.2
# 91  2020-03-31  2.7
# 92  2020-04-01  0.0
# 93  2020-04-02  0.8
# 94  2020-04-03  3.7
# 95  2020-04-04  3.7
# 96  2020-04-05  2.9
# 97  2020-04-06  1.3
# 98  2020-04-07  2.1
# 99  2020-04-08  3.0
# 100 2020-04-09  2.5
# 101 2020-04-10  2.5
# 102 2020-04-11  0.9
# 103 2020-04-12  0.9
# 104 2020-04-13  1.6
# 105 2020-04-14  3.8
# 106 2020-04-15  3.9
# 107 2020-04-16  3.0
# 108 2020-04-17  2.9
# 109 2020-04-18  2.1
# 110 2020-04-19  0.0
# 111 2020-04-20  2.4
# 112 2020-04-21  3.3
# 113 2020-04-22  3.0
# 114 2020-04-23  1.8
# 115 2020-04-24  2.1
# 116 2020-04-25  2.1
# 117 2020-04-26  0.0
# 118 2020-04-27  1.4
# 119 2020-04-28  2.4
# 120 2020-04-29  3.3
# 121 2020-04-30  1.4
# 122 2020-05-01  1.6
# 123 2020-05-02  2.3
# 124 2020-05-03  2.4
# 125 2020-05-04  2.9
# 126 2020-05-05  1.6
# 127 2020-05-06  3.7
# 128 2020-05-07  3.9
# 129 2020-05-08  0.9
# 130 2020-05-09  2.9
# 131 2020-05-10  3.6
# 132 2020-05-11  2.4
# 133 2020-05-12  2.5
# 134 2020-05-13  3.7
# 135 2020-05-14  3.4
# 136 2020-05-15  2.3
# 137 2020-05-16  3.3
# 138 2020-05-17  0.5
# 139 2020-05-18  3.1
# 140 2020-05-19  2.5
# 141 2020-05-20  0.6
# 142 2020-05-21  0.3
# 143 2020-05-22  1.9
# 144 2020-05-23  3.1
# 145 2020-05-24  2.9
# 146 2020-05-25  3.3
# 147 2020-05-26  0.7
# 148 2020-05-27  3.8
# 149 2020-05-28  1.2
# 150 2020-05-29  0.6
# 151 2020-05-30  2.9
# 440 2021-03-15  1.9
# 441 2021-03-16  2.6
# 442 2021-03-17  3.7
# 443 2021-03-18  1.5
# 444 2021-03-19  3.4
# 445 2021-03-20  1.2
# 446 2021-03-21  1.9
# 447 2021-03-22  0.6
# 448 2021-03-23  3.2
# 449 2021-03-24  2.7
# 450 2021-03-25  0.2
# 451 2021-03-26  1.7
# 452 2021-03-27  1.6
# 453 2021-03-28  2.8
# 454 2021-03-29  2.6
# 455 2021-03-30  1.6
# 456 2021-03-31  1.2
# 457 2021-04-01  1.0
# 458 2021-04-02  2.7
# 459 2021-04-03  3.6
# 460 2021-04-04  3.4
# 461 2021-04-05  1.6
# 462 2021-04-06  0.3
# 463 2021-04-07  3.3
# 464 2021-04-08  0.3
# 465 2021-04-09  0.5
# 466 2021-04-10  2.6
# 467 2021-04-11  1.3
# 468 2021-04-12  0.8
# 469 2021-04-13  1.6
# 470 2021-04-14  3.4
# 471 2021-04-15  1.4
# 472 2021-04-16  0.0
# 473 2021-04-17  3.6
# 474 2021-04-18  3.8
# 475 2021-04-19  2.0
# 476 2021-04-20  1.9
# 477 2021-04-21  2.4
# 478 2021-04-22  3.6
# 479 2021-04-23  0.7
# 480 2021-04-24  3.1
# 481 2021-04-25  0.9
# 482 2021-04-26  2.3
# 483 2021-04-27  3.4
# 484 2021-04-28  0.5
# 485 2021-04-29  3.6
# 486 2021-04-30  1.8
# 487 2021-05-01  3.6
# 488 2021-05-02  1.0
# 489 2021-05-03  0.3
# 490 2021-05-04  0.2
# 491 2021-05-05  3.9
# 492 2021-05-06  1.9
# 493 2021-05-07  3.4
# 494 2021-05-08  1.7
# 495 2021-05-09  2.0
# 496 2021-05-10  0.7
# 497 2021-05-11  3.0
# 498 2021-05-12  1.2
# 499 2021-05-13  0.7
# 500 2021-05-14  0.1
# 501 2021-05-15  0.5
# 502 2021-05-16  0.7
# 503 2021-05-17  2.1
# 504 2021-05-18  3.2
# 505 2021-05-19  0.5
# 506 2021-05-20  3.6
# 507 2021-05-21  2.3
# 508 2021-05-22  0.6
# 509 2021-05-23  3.6
# 510 2021-05-24  1.0
# 511 2021-05-25  0.6
# 512 2021-05-26  3.1
# 513 2021-05-27  0.9
# 514 2021-05-28  1.2
# 515 2021-05-29  2.1
# 516 2021-05-30  1.3
# 805 2022-03-15  2.1
# 806 2022-03-16  0.6
# 807 2022-03-17  2.1
# 808 2022-03-18  0.9
# 809 2022-03-19  1.1
# 810 2022-03-20  2.0
# 811 2022-03-21  0.6
# 812 2022-03-22  3.7
# 813 2022-03-23  2.0
# 814 2022-03-24  2.5
# 815 2022-03-25  3.0
# 816 2022-03-26  2.5
# 817 2022-03-27  3.7
# 818 2022-03-28  0.3
# 819 2022-03-29  0.1
# 820 2022-03-30  2.2
# 821 2022-03-31  1.0
# 822 2022-04-01  2.6
# 823 2022-04-02  1.5
# 824 2022-04-03  0.2
# 825 2022-04-04  0.2
# 826 2022-04-05  3.9
# 827 2022-04-06  0.9
# 828 2022-04-07  0.0
# 829 2022-04-08  0.9
# 830 2022-04-09  0.2
# 831 2022-04-10  0.9
# 832 2022-04-11  1.6
# 833 2022-04-12  0.1
# 834 2022-04-13  1.4
# 835 2022-04-14  2.3
# 836 2022-04-15  2.4
# 837 2022-04-16  1.1
# 838 2022-04-17  2.7
# 839 2022-04-18  3.7
# 840 2022-04-19  0.0
# 841 2022-04-20  3.1
# 842 2022-04-21  3.2
# 843 2022-04-22  0.3
# 844 2022-04-23  2.3
# 845 2022-04-24  2.2
# 846 2022-04-25  0.6
# 847 2022-04-26  1.1
# 848 2022-04-27  1.4
# 849 2022-04-28  3.3
# 850 2022-04-29  0.7
# 851 2022-04-30  2.1
# 852 2022-05-01  3.1
# 853 2022-05-02  1.2
# 854 2022-05-03  1.7
# 855 2022-05-04  2.7
# 856 2022-05-05  0.1
# 857 2022-05-06  2.2
# 858 2022-05-07  3.7
# 859 2022-05-08  1.7
# 860 2022-05-09  3.7
# 861 2022-05-10  3.9
# 862 2022-05-11  2.7
# 863 2022-05-12  1.2
# 864 2022-05-13  0.8
# 865 2022-05-14  1.3
# 866 2022-05-15  3.4
# 867 2022-05-16  0.2
# 868 2022-05-17  0.7
# 869 2022-05-18  1.9
# 870 2022-05-19  1.4
# 871 2022-05-20  3.3
# 872 2022-05-21  3.5
# 873 2022-05-22  0.1
# 874 2022-05-23  3.3
# 875 2022-05-24  1.6
# 876 2022-05-25  2.7
# 877 2022-05-26  0.5
# 878 2022-05-27  1.9
# 879 2022-05-28  3.7
# 880 2022-05-29  2.3
# 881 2022-05-30  3.7

This is how it works:

substring('2020-03-15', 6)
# [1] "03-15"

Note that if we want a substring from inside, we use substr:

substr('2020-03-15', 3, 7)
# [1] "20-03"

BTW, to get "Date" format, we can simply do

dat$date <- as.Date(dat$date)

Data:

dat <- data.frame(date=as.character(seq.Date(as.Date('2020-01-01'), as.Date('2023-01-01'), 'day')))
set.seed(42)
dat$prec <- round(runif(nrow(dat), 0, 4), 1)
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    Thanks a lot for your solution and good illustration! – Marle Mü Mar 27 '23 at 07:00
  • Oh, thanks for the tipp! I am quite new to this forum, so I'm still finding out how things are going here... – Marle Mü Mar 27 '23 at 09:31
  • @MarleMü Good job! You could earn the [tour](https://stackoverflow.com/tour) badge. Especially for the R tag, you might want to read our (more or less) short [tutorial](https://stackoverflow.com/q/5963269/6574038). Cheers! – jay.sf Mar 27 '23 at 09:44