# How to compute monthly and weekly minimum/maximum

Hi y'all!

I'm currently working on a project on extremes values. I need first to compute the monthly and weekly minimum and maximum in order then to make a graph.

I tried this code
library(tibbletime)
library(dplyr)
library(lubridate)
data %>%
group_by(Year, Month) %>%
mutate(
min_Temp= min(AvgTemperature),
max_Temp= max(AvgTemperature))
This code actually shows me the min and max but doesn't create new variables and I can't plot them...
So first it would be great if someone could help me to do that, and secondly if someone can help me with the weekly min and max that would also be awesome....

Here is a part of my data: Also I'm new on this community, I tried my best to respect the rules and I also spent an hour on the community to look for a similar question. So I'm truly sorry if my post doesn't fit the rules of the community. Just tell me what's wrong

``````            City Month Day Year AvgTemperature       Date
310563 Hong Kong     1   1 2015           62.4 2015-01-01
310564 Hong Kong     1   2 2015           60.4 2015-01-02
310565 Hong Kong     1   3 2015           63.0 2015-01-03
310566 Hong Kong     1   4 2015           67.4 2015-01-04
310567 Hong Kong     1   5 2015           69.4 2015-01-05
310568 Hong Kong     1   6 2015           70.4 2015-01-06
310569 Hong Kong     1   7 2015           63.3 2015-01-07
310570 Hong Kong     1   8 2015           60.8 2015-01-08
310571 Hong Kong     1   9 2015           60.3 2015-01-09
310572 Hong Kong     1  10 2015           63.8 2015-01-10
310573 Hong Kong     1  11 2015           62.3 2015-01-11
310574 Hong Kong     1  12 2015           55.6 2015-01-12
310575 Hong Kong     1  13 2015           55.2 2015-01-13
310576 Hong Kong     1  14 2015           57.5 2015-01-14
310577 Hong Kong     1  15 2015           59.5 2015-01-15
310578 Hong Kong     1  16 2015           63.7 2015-01-16
310579 Hong Kong     1  17 2015           61.9 2015-01-17
310580 Hong Kong     1  18 2015           62.4 2015-01-18
310581 Hong Kong     1  19 2015           61.5 2015-01-19
310582 Hong Kong     1  20 2015           62.4 2015-01-20
310583 Hong Kong     1  21 2015           65.4 2015-01-21
310584 Hong Kong     1  22 2015           64.8 2015-01-22
310585 Hong Kong     1  23 2015           64.8 2015-01-23
310586 Hong Kong     1  24 2015           65.8 2015-01-24
310587 Hong Kong     1  25 2015           68.0 2015-01-25
310588 Hong Kong     1  26 2015           68.4 2015-01-26
310589 Hong Kong     1  27 2015           66.8 2015-01-27
310590 Hong Kong     1  28 2015           64.1 2015-01-28
310591 Hong Kong     1  29 2015           65.8 2015-01-29
310592 Hong Kong     1  30 2015           62.4 2015-01-30
310593 Hong Kong     1  31 2015           60.1 2015-01-31
310594 Hong Kong     2   1 2015           60.1 2015-02-01
310595 Hong Kong     2   2 2015           63.7 2015-02-02
310596 Hong Kong     2   3 2015           65.1 2015-02-03
310597 Hong Kong     2   4 2015           60.6 2015-02-04
310598 Hong Kong     2   5 2015           56.3 2015-02-05
310599 Hong Kong     2   6 2015           57.8 2015-02-06
310600 Hong Kong     2   7 2015           62.3 2015-02-07
310601 Hong Kong     2   8 2015           61.8 2015-02-08
310602 Hong Kong     2   9 2015           59.6 2015-02-09
310603 Hong Kong     2  10 2015           60.3 2015-02-10
310604 Hong Kong     2  11 2015           62.4 2015-02-11
310605 Hong Kong     2  12 2015           64.3 2015-02-12
310606 Hong Kong     2  13 2015           67.1 2015-02-13
310607 Hong Kong     2  14 2015           68.9 2015-02-14
310608 Hong Kong     2  15 2015           66.5 2015-02-15
310609 Hong Kong     2  16 2015           68.8 2015-02-16
310610 Hong Kong     2  17 2015           68.5 2015-02-17
310611 Hong Kong     2  18 2015           66.3 2015-02-18
310612 Hong Kong     2  19 2015           64.5 2015-02-19
310613 Hong Kong     2  20 2015           67.6 2015-02-20
310614 Hong Kong     2  21 2015           72.0 2015-02-21
310615 Hong Kong     2  22 2015           70.8 2015-02-22
310616 Hong Kong     2  23 2015           69.1 2015-02-23
310617 Hong Kong     2  24 2015           71.1 2015-02-24
310618 Hong Kong     2  25 2015           73.1 2015-02-25
310619 Hong Kong     2  26 2015           74.7 2015-02-26
310620 Hong Kong     2  27 2015           67.9 2015-02-27
310621 Hong Kong     2  28 2015           69.2 2015-02-28
310622 Hong Kong     3   1 2015           66.0 2015-03-01
310623 Hong Kong     3   2 2015           65.4 2015-03-02
310624 Hong Kong     3   3 2015           70.1 2015-03-03
310625 Hong Kong     3   4 2015           63.4 2015-03-04
310626 Hong Kong     3   5 2015           65.9 2015-03-05
310627 Hong Kong     3   6 2015           63.5 2015-03-06
310628 Hong Kong     3   7 2015           65.6 2015-03-07
310629 Hong Kong     3   8 2015           69.6 2015-03-08
310630 Hong Kong     3   9 2015           71.6 2015-03-09
310631 Hong Kong     3  10 2015           65.0 2015-03-10
310632 Hong Kong     3  11 2015           61.9 2015-03-11
310633 Hong Kong     3  12 2015           61.8 2015-03-12
310634 Hong Kong     3  13 2015           66.9 2015-03-13
310635 Hong Kong     3  14 2015           71.0 2015-03-14
310636 Hong Kong     3  15 2015           75.0 2015-03-15
310637 Hong Kong     3  16 2015           75.9 2015-03-16
310638 Hong Kong     3  17 2015           76.2 2015-03-17
310639 Hong Kong     3  18 2015           77.1 2015-03-18
310640 Hong Kong     3  19 2015           77.5 2015-03-19
310641 Hong Kong     3  20 2015           76.1 2015-03-20
310642 Hong Kong     3  21 2015           75.3 2015-03-21
310643 Hong Kong     3  22 2015           71.5 2015-03-22
310644 Hong Kong     3  23 2015           71.3 2015-03-23
310645 Hong Kong     3  24 2015           68.9 2015-03-24
310646 Hong Kong     3  25 2015           65.4 2015-03-25
310647 Hong Kong     3  26 2015           67.9 2015-03-26
310648 Hong Kong     3  27 2015           70.0 2015-03-27
310649 Hong Kong     3  28 2015           72.6 2015-03-28
310650 Hong Kong     3  29 2015           74.5 2015-03-29
310651 Hong Kong     3  30 2015           76.1 2015-03-30
310652 Hong Kong     3  31 2015           77.0 2015-03-31
310653 Hong Kong     4   1 2015           78.0 2015-04-01
310654 Hong Kong     4   2 2015           79.7 2015-04-02
310655 Hong Kong     4   3 2015           80.6 2015-04-03
310656 Hong Kong     4   4 2015           80.0 2015-04-04
310657 Hong Kong     4   5 2015           80.7 2015-04-05
310658 Hong Kong     4   6 2015           81.6 2015-04-06
310659 Hong Kong     4   7 2015           76.8 2015-04-07
310660 Hong Kong     4   8 2015           63.3 2015-04-08
310661 Hong Kong     4   9 2015           64.1 2015-04-09
310662 Hong Kong     4  10 2015           63.5 2015-04-10
310663 Hong Kong     4  11 2015           63.1 2015-04-11
310664 Hong Kong     4  12 2015           69.4 2015-04-12
310665 Hong Kong     4  13 2015           73.7 2015-04-13
310666 Hong Kong     4  14 2015           70.9 2015-04-14
310667 Hong Kong     4  15 2015           72.6 2015-04-15
310668 Hong Kong     4  16 2015           75.0 2015-04-16
310669 Hong Kong     4  17 2015           77.1 2015-04-17
310670 Hong Kong     4  18 2015           80.8 2015-04-18
310671 Hong Kong     4  19 2015           82.1 2015-04-19
310672 Hong Kong     4  20 2015           78.7 2015-04-20
310673 Hong Kong     4  21 2015           74.7 2015-04-21
310674 Hong Kong     4  22 2015           75.6 2015-04-22
310675 Hong Kong     4  23 2015           75.5 2015-04-23
310676 Hong Kong     4  24 2015           77.7 2015-04-24
310677 Hong Kong     4  25 2015           77.0 2015-04-25
310678 Hong Kong     4  26 2015           77.3 2015-04-26
310679 Hong Kong     4  27 2015           79.0 2015-04-27
310680 Hong Kong     4  28 2015           80.6 2015-04-28
310681 Hong Kong     4  29 2015           82.4 2015-04-29
310682 Hong Kong     4  30 2015           82.9 2015-04-30
310683 Hong Kong     5   1 2015           82.4 2015-05-01
310684 Hong Kong     5   2 2015           83.8 2015-05-02
310685 Hong Kong     5   3 2015           85.1 2015-05-03
310686 Hong Kong     5   4 2015           84.4 2015-05-04
310687 Hong Kong     5   5 2015           82.5 2015-05-05
310688 Hong Kong     5   6 2015           82.7 2015-05-06
310689 Hong Kong     5   7 2015           84.4 2015-05-07
310690 Hong Kong     5   8 2015           84.5 2015-05-08
310691 Hong Kong     5   9 2015           82.5 2015-05-09
310692 Hong Kong     5  10 2015           82.5 2015-05-10
310693 Hong Kong     5  11 2015           80.1 2015-05-11
310694 Hong Kong     5  12 2015           80.4 2015-05-12
310695 Hong Kong     5  13 2015           82.9 2015-05-13
310696 Hong Kong     5  14 2015           84.6 2015-05-14
310697 Hong Kong     5  15 2015           85.5 2015-05-15
310698 Hong Kong     5  16 2015           79.5 2015-05-16
310699 Hong Kong     5  17 2015           82.7 2015-05-17
310700 Hong Kong     5  18 2015           84.8 2015-05-18
310701 Hong Kong     5  19 2015           84.1 2015-05-19
310702 Hong Kong     5  20 2015           82.0 2015-05-20
310703 Hong Kong     5  21 2015           77.8 2015-05-21
310704 Hong Kong     5  22 2015           78.4 2015-05-22
310705 Hong Kong     5  23 2015           80.4 2015-05-23
310706 Hong Kong     5  24 2015           81.6 2015-05-24
310707 Hong Kong     5  25 2015           85.6 2015-05-25
310708 Hong Kong     5  26 2015           84.3 2015-05-26
310709 Hong Kong     5  27 2015           87.3 2015-05-27
310710 Hong Kong     5  28 2015           87.8 2015-05-28
310711 Hong Kong     5  29 2015           88.5 2015-05-29
310712 Hong Kong     5  30 2015           86.2 2015-05-30
310713 Hong Kong     5  31 2015           86.6 2015-05-31
310714 Hong Kong     6   1 2015           86.3 2015-06-01
310715 Hong Kong     6   2 2015           86.9 2015-06-02
310716 Hong Kong     6   3 2015           87.5 2015-06-03
310717 Hong Kong     6   4 2015           87.5 2015-06-04
310718 Hong Kong     6   5 2015           85.3 2015-06-05
310719 Hong Kong     6   6 2015           86.7 2015-06-06
310720 Hong Kong     6   7 2015           87.7 2015-06-07
310721 Hong Kong     6   8 2015           87.6 2015-06-08
310722 Hong Kong     6   9 2015           88.2 2015-06-09
310723 Hong Kong     6  10 2015           87.8 2015-06-10
310724 Hong Kong     6  11 2015           87.3 2015-06-11
310725 Hong Kong     6  12 2015           87.6 2015-06-12
310726 Hong Kong     6  13 2015           88.0 2015-06-13
310727 Hong Kong     6  14 2015           86.6 2015-06-14
310728 Hong Kong     6  15 2015           87.2 2015-06-15``````

I am assuming you are working with only one city i.e. Hong Kong. If not, just add the city variable in `group_by`. Note that I have used `summarise()` instead of `mutate()`. Hope this helps.

``````suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(lubridate)))
#************
# Toy Data
#************
toy_data <- tibble(
date = seq(as.Date('2015-01-01'),as.Date('2015-06-30'),by = 1),
city = "Hong Kong",
avg_temp = rnorm(181, mean = 65, sd = 7),
) %>%
# Generate Month & Week
mutate(month = month(date, label = TRUE),
week = week(date))

#> # A tibble: 6 x 5
#>   date       city      avg_temp month  week
#>   <date>     <chr>        <dbl> <ord> <dbl>
#> 1 2015-01-01 Hong Kong     63.8 Jan       1
#> 2 2015-01-02 Hong Kong     59.3 Jan       1
#> 3 2015-01-03 Hong Kong     59.7 Jan       1
#> 4 2015-01-04 Hong Kong     58.3 Jan       1
#> 5 2015-01-05 Hong Kong     52.1 Jan       1
#> 6 2015-01-06 Hong Kong     60.9 Jan       1

# Note: summarise() will create a new data frame

#*************
# Monthly Stat
#*************
monthly_stat <- toy_data %>%
group_by(month) %>%
summarise(
min_temp = min(avg_temp),
max_temp = max(avg_temp)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 6 x 3
#>   month min_temp max_temp
#>   <ord>    <dbl>    <dbl>
#> 1 Jan       51.0     79.0
#> 2 Feb       46.7     80.0
#> 3 Mar       49.9     87.3
#> 4 Apr       55.7     80.3
#> 5 May       51.9     78.0
#> 6 Jun       51.6     77.3

#*************
# Weekly Stat
#*************
weekly_stat <- toy_data %>%
group_by(week) %>%
summarise(
min_temp = min(avg_temp),
max_temp = max(avg_temp)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 6 x 3
#>    week min_temp max_temp
#>   <dbl>    <dbl>    <dbl>
#> 1     1     52.1     77.0
#> 2     2     60.0     71.7
#> 3     3     51.0     76.3
#> 4     4     61.4     79.0
#> 5     5     62.7     79.0
#> 6     6     57.7     80.0
``````

Created on 2020-11-15 by the reprex package (v0.3.0)

You did not assign your result to any name, so you won't be able to retrieve them

Assignment is done with <-
You will see how budugulo used them

Hi!

Just wanted to thank you for your help!!

Claire

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.