0

So I am trying to find the average temperature by day. The head of the data is below. So I am trying to find the average temperature for each Monday, Tuesday, ... and Sunday. What kind of function should I use to group all the dates and find its average temperature?

Date
<fctr>
AvgTemp
<dbl>

1   01/01/2013  20.72222        
2   01/02/2013  19.05556        
3   01/03/2013  23.05556        
4   01/04/2013  23.88889        
5   01/05/2013  28.38889        
6   01/06/2013  29.00000    
www
  • 38,575
  • 12
  • 48
  • 84

1 Answers1

0

We can use the wday function from the lubridate package to get the week day of each date. Before that, each date should be in the date class, we can do that using the dmy function from lubridate.

library(dplyr)
library(lubridate)

dat2 <- dat %>%
  mutate(Date = dmy(Date)) %>%
  mutate(WeekDay = wday(Date)) %>%
  group_by(WeekDay) %>%
  summarize(AvgTemp = mean(AvgTemp)) %>%
  ungroup()
dat2
# # A tibble: 5 x 2
#   WeekDay AvgTemp
#     <dbl>   <dbl>
# 1       2    23.9
# 2       3    20.7
# 3       4    28.4
# 4       6    21.1
# 5       7    29  

DATA

dat <- read.table(text = " Date AvgTemp
1   01/01/2013  20.72222        
2   01/02/2013  19.05556        
3   01/03/2013  23.05556        
4   01/04/2013  23.88889        
5   01/05/2013  28.38889        
6   01/06/2013  29.00000",
                  header = TRUE, stringsAsFactors = FALSE)
www
  • 38,575
  • 12
  • 48
  • 84