-2

I have a dataset with hourly rainfall (mm) and I wish to calculate mean daily rainfall. With 20.00 as start and endpoint, not 00.00 - 24.00. Will it be possible to get mean value (rainfall) from 20.00 to 20.00?

Here is the dataset with date, time and rainfall (mm). https://drive.google.com/file/d/1JlEGTsJpAAVBQW2v6VTD3B-mrp9W6tWD/view?usp=sharing

Kes
  • 17
  • 3
  • In the example data you shared, there is only "00:00" as time. How do you want to extract 20:00 from this data? – bird May 18 '22 at 20:02
  • Sorry, I uploaded the wrong file. I will update it with the correct data asap. – Kes May 18 '22 at 20:04
  • @bird, I have updated the file now – Kes May 18 '22 at 20:10
  • Please don't upload data to external sites. See how to create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on including data in the question itself. Ideally you'd also include the desired output for that sample input so that possible solutions can be tested and verifies. We don't need all your data, just enough to test the code. – MrFlick May 18 '22 at 20:33

1 Answers1

0

Something like this:


library(lubridate)
library(tidyverse)

df %>% 
  filter(row_number() != 1:20) %>% 
  mutate(x_axis = trimws(`Date and time`, whitespace = ' .*')) %>% 
  group_by(group_id = cumsum(Hour == 20)) %>% 
  mutate(mean20_20 = mean(`Rainfall mm`)) %>% 
  slice(1) %>% 
  drop_na() %>% 
  ggplot(aes(x = x_axis, y=mean20_20, group=1)) +
  geom_point(size= 1, color = "blue")+
  geom_line(color = "red")+ 
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thanks, it seems to work. I do get this error message: Warning message: In row_number() != 1:20 : longer object length is not a multiple of shorter object length, could I ingore this? – Kes May 19 '22 at 05:12
  • Would it be possible to get columns with sum, min and max also? Note: I added a new file with column names without space. – Kes May 19 '22 at 05:52