-1

So I have data of temperature and multiple years. However the same year pops up multiple times, ex 2001, 2001, 2001, 2001 etc. How do I compile these years into one single year and add the average temperature for that year? Using dplyr if possible.

I have tried using dplyr and writing some code like:

Data <- DataSet %>% group_by(XYZ) or mutate(XYZ).

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • Try `df %>% group_by(year_var_name) %>% summarise(sth = mean(temperature_var_name))` – Park Feb 14 '23 at 01:42
  • Your code almost worked for our data set. We forgot to mention we have temperature data for 365 days for each year, and we want the average of those days to be made into one year, for every year. – frankie122 Feb 14 '23 at 02:27

2 Answers2

0

If I understand right, you want to group your data by a 'year' column and then calculate the average 'temperature' for that year. For this, you will have to do something like below in dplyr:

data %>% group_by(year) %>% summarise(avg_temp = mean(temperature))

Update: to find average temperature for 365 days of a year, for each year, you will have to first add a column to denote a year. You may need to use a package called "lubridate" to play with dates.

data %>% 
mutate(year = year(as.Date(day))) %>% 
group_by(year) %>% 
summarise(avg_temp = mean(temperature))
noby
  • 1
  • 2
  • Your code almost worked for our data set. We forgot to mention we have temperature data for 365 days for each year, and we want the average of those days to be made into one year, for every year. – frankie122 Feb 14 '23 at 02:27
0

You can aggregate values with the same year using the mean function.

df_grouped <- df %>%
group_by(year) %>%
summarize(avg_temp = mean(temp))
Yas
  • 98
  • 5
  • Your code almost worked for our data set. We forgot to mention we have temperature data for 365 days for each year, and we want the average of those days to be made into one year, for every year. – frankie122 Feb 14 '23 at 02:27
  • Since it's a daily record of temperatures spanning over years, I would also add "day" variable to the `group_by()` function, I am basing this on what you have described but generally I think it's best to post the data so anyone can have a sense of the structure and decide what needs to be manipulated to achieve desired results. There are other similar posts too that are worth looking into [https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group] – Yas Feb 14 '23 at 11:28