Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

dataframe - Calculate daily mean with hourly data in r

I'm sure it's already been covered, but I can't make it work and so I've decided to ask my question for my specific data.

I have a dataset of hourly measured cloud cover. I'm trying to calculate the mean per day. I have already separated my data and so I have a column with the date in format YYYY-MM-DD and another column with cloud cover. I'm trying to calculate the mean cloud cover for each hourly observation (same date).

ex :

Date = c(2010-01-03, 2010-01-03, 2010-01-03, 2010-01-04, 2010-01-04, 2010-01-04, 2010-01-05, 2010-01-05)

Cloud_cover = c(5,5,2,3,5,1,5,4)

I would like to obtain a the mean cloud_cover value of every observation with 2010-01-03 and 2010-01-04 and so on.

Next step would be to create a new dataset with only the mean values, but I'll get to that later.

Thanks

question from:https://stackoverflow.com/questions/66052969/calculate-daily-mean-with-hourly-data-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
library(dplyr)
# vector Date
Date <- c("2010-01-03", "2010-01-03", "2010-01-03", "2010-01-04", "2010-01-04", "2010-01-04", "2010-01-05", "2010-01-05")

# vector Cloud_cover
Cloud_cover = c(5,5,2,3,5,1,5,4)

# put to dataframe with cbind
df <- as.data.frame(cbind(Date, Cloud_cover))

# to numeric
df$Cloud_cover <- as.numeric(Cloud_cover)

# group by date and calculate mean with dplyr
df <- df %>% 
  group_by(Date) %>% 
  dplyr::summarize(Mean = mean(Cloud_cover, na.rm=TRUE))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...