-1

My data frame looks somewhat like this

1976 January     250
1976 February    350
1976 March       230
1976 April       255

This goes up to 2007

I want to aggregate and find the mean of the data per year. I want to create a variable that would contain a table of data mean by each year. So that it looks like

1976      mean of all 1976 months
1977      mean of all 1976 months
1978      mean of all 1976 months
1979      mean of all 1976 months

and so on

How can I do it?

The data frame is "data" and the first column is "year"

variable_name <- aggregate(x=data$secondcolumn_name, by=list(data$Year),  FUN="mean")

But it is not getting aggregated. I think since the column "year" has characters is the problem.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    Hi and welcome to Stack Overflow. Please do not share sample data as images, rather look at how to [make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also you may share your expected output and the code you've tried so far. – Ric S Jul 29 '20 at 10:31
  • Hi, I have changed the question. Can you remove the "closed" tag please. – Chayan Baul Jul 29 '20 at 13:16

1 Answers1

0

If the Year column has always the same form that in the image and its class is character, the dataframe is called data and the name of the second column is second_column_name (to abbreviate the name in the image), then you can do:

library(dplyr)
data %>%
    group_by(substr(Year,1,4)) %>%
    summarise(avg = mean(second_column_name))

Or using mutate instead of summarise if you want to preserve the monthly data.

iago
  • 2,990
  • 4
  • 21
  • 27