Reputation: 17
I have a DF and I need to group_by "Province" summarising by population and creating 1 data frame for each province? I trying by looping but it doesen work!
shape %>%
group_by(provincia) %>%
summarise(poblacion = sum(personas)) %>%
as.data.frame()
}
thanx
Upvotes: 0
Views: 37
Reputation: 635
Put the as.data.frame() before the group_by(). When grouping and summarizing on an sf object it is combining the spatial polygons based on the grouping variable, similar to a dissolve in GIS. If there could be any NA in your data set would want to include na.rm=TRUE when summing
shape %>%
as.data.frame() %>%
group_by(provincia) %>%
summarise(poblacion = sum(personas, na.rm=T))
If you want to seperate out the result into separate data frames you could use group_split()
shape %>%
as.data.frame() %>%
group_by(provincia) %>%
summarise(poblacion = sum(personas, na.rm=T)) %>%
group_split(provincia)
Upvotes: 1