How to calculate yearly/monthly average, max value, min value etc of a Column based on the date (month/day/year) on another column in R

Sankar Manalilkada Sasidharan

How to calculate yearly/monthly average, max value, min value etc. of a Column based on the date (month/day/year) on another column in R. My date frame contains daily, monthly and hourly precipitation and discharge for dates starting from Jan 1 ,2013 till December 31, 2019

  | Date     |precipitation  | Stream A Discharge |  Stream B Discharge | 
----------------------------------------------------------------------------
1  | 1/1/2013 |  0.35        |  2.35              |   3.83              | 
 

For example how would I calculate the average/mean/max/min precipitation or discharge of stream A of the year 2013 or for January 2013, or December 2014 in R?

Ronak Shah

Change the data to date class, extract year from it and using across you can calculate multiple statistics for multiple columns.

library(dplyr)
library(lubridate)

df %>%
  mutate(Date = dmy(Date), 
         year = year(Date), 
         year_month = format(Date, '%Y-%m')) %>%
  group_by(year) %>%
  #If you need for every month
  #group_by(year_month) %>%
  summarise(across(precipitation:Stream.B.Discharge, 
            list(mean = mean, min = min, max = max)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select value of a column based on another column min(date) with Bigquery

How to get the date of a max value from another column in R?

How can I aggregate a column's value based on the min() and max() values of another column?

How to assign new column value based on max value in another column preceding date

getting max or min date in sql based on value of a column in SQL

How to calculate the average value of following n rows based on another column - SQL (Oracle)

Is it possible to calculate the max/min value of 2 or more colums if the value in another column is equal to a specific value in Python pandas?

How to calculate the value of one column based on another column?

PySpark DataFrame update column value based on min/max condition on timestamp value in another column

How to assign a value for a column based on another column value in R?

Calculate min, max and average on dataframe column

How to create a ranking column based on date value range in another column?

min/max value of a column based on values of another column, grouped by and transformed in pandas

Get the value of a column based on min max values of another column of a pandas dataframe in a grouped aggregate function

R: cumulative sum based on another column and max sum value

How to group by a df in Python by a column with the difference between the max value of one column and the min of another column?

How to calculate a value row that is based on the previous value in the same column (in R)?

SQL get value based on corresponding min/max(value) from column in another related table

SQL compare min value in one column with max date in different column

Get the value of a column where one column is max and another is min

Get MAX value from one column and MIN from another column

Calculate percentage of occurences of a value in a dataframe column based on another column value

How to calculate weighted sums of rows based on value in another column

How to calculate the average value of following n rows based on another column - SQL (Oracle) - updated version of previously answered question

Return another column value based on Max value from Column

New Column repeating value based on max value from another column

DAX. Obtain the value of a column based on the max value of another column

Max value of a column based on every unique value of another column (Dataframe)

get max date based on column value