Home > OS >  How to find mean for one column based on another column in dataset
How to find mean for one column based on another column in dataset

Time:02-15

I have the following data frame. I am trying to calculate the mean months of residence for each sector (there are actually 100 total, but I simplified the data). How would I do this? I am wanting to have a list of the 100 sectors with the average resident time in months (Month_Res) for each sector.

##       ID        DOB sector  meter Oct   Res_FROM     Res_TO   Exp_FROM
## 1  20100 1979-08-24    H38   6400   W 1979-08-15 1991-05-15 1979-08-24
## 2  20101 1980-05-05    B01   1600  NW 1980-05-15 1991-04-15 1980-05-15
## 3  20102 1979-03-17    H04   1600  SW 1972-06-15 1979-08-15 1979-03-17
## 4  20103 1981-11-30    B09   3200  NE 1982-01-15 1984-01-15 1982-01-15
## 5  20103 1981-11-30    B37   8000   N 1984-01-15 1986-04-15 1984-01-15
## 6  20104 1978-09-01    B09   3200  NE 1982-01-15 1984-01-15 1982-01-15

##    Month_Res
## 1        141
## 2        131
## 3         86
## 4         24
## 5         27
## 6         24

CodePudding user response:

Let's assume that the data is called df:

library(dplyr)
df2 <- df %>% group_by(sector) %>% summarise(Month_res_mean = mean(Month_Res))

  • Related