Home > Net >  How to apply a function to certain columns for multiple times in R?
How to apply a function to certain columns for multiple times in R?

Time:10-15

everybody. I've come across a problem in my R project. I got a dataset of crime rates in the US. It looks like this: state year population crimes (there is 20 more columns) Alabama 2000 4447100 4059.7
Alabama 2001 4468912 3876.8
Alabama 2002 4478896 4027.8
Alabama 2003 4503726 4046.4
Alabama 2004 4525375 4029.3
Alabama 2005 4548327 3900.0
Alabama 2006 4599030 3941.0
Alabama 2007 4627851 3977.7
Alabama 2008 4661900 4084.5
Alabama 2009 4708708 3780.4
Alabama 2010 4785401 3528.0
Alabama 2011 4803689 3605.4
Alabama 2012 4822023 3502.2
Alabama 2013 4833722 285.2
Alabama 2014 4849377 283.4
Alabama 2015 4858979 328.3
Alabama 2016 4863300 2947.8
Alabama 2017 4874747 2957.3
Alabama 2018 4887871 2817.2
Alabama 2019 4903185 2674.4
Alaska 2000 626932 3682.5
Alaska 2001 633630 3655.1
Alaska 2002 641482 3759.7
Alaska 2003 648280 3761.6
Alaska 2004 657755 3370.9
Alaska 2005 663253 3615.0
Alaska 2006 670053 3578.1
Alaska 2007 683478 3379.2
Alaska 2008 686293 2927.3
Alaska 2009 698473 2940.4
...this is the dataset

So long story short, I have 50 states listed from A-Z, and year is 2000-2019. For example, I want to use apply function to calculate the mean of the "crimes" from 2000 to 2019 for all 50 states. So what I would like to get is 50 means. For example the mean for Alabama would be (4059.7 ... 2674.4)/20

please, how can I possibly get the result I want with least efforts?

Thank you very much!

CodePudding user response:

Building on @Paul Stafford Allen comment.

Assuming your data is in a data.frame or tibble called "df"

library(dplyr)

df %>% 
  group_by(state) %>% 
  summarise(mean = mean(crimes))

Incidentally, if you want mean crimes by state and year, it's a simple change:

df %>% 
  group_by(state, year) %>% 
  summarise(mean = mean(crimes))
  •  Tags:  
  • r
  • Related