Skip to content

Instantly share code, notes, and snippets.

@mollietaylor mollietaylor/gini.R
Created Jan 2, 2013

Embed
What would you like to do?
Calculating a Gini Coefficients for a Number of Locales at Once in R
# generate random data:
city <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
income <- sample(1:100000,
100,
replace = TRUE)
cities <- data.frame(city, income)
# graph our data:
library(ggplot2)
ggplot(cities,
aes(income)) +
stat_density(geom = "path",
position = "identity") +
facet_wrap(~ city, ncol = 2)
library(reldist)
# gini coefficient for city A:
gini(cities[which(cities$city == "A"), ]$income)
# gini coefficients for all cities:
ginicities <- aggregate(income ~ city,
data = cities,
FUN = "gini")
names(ginisec) <- c("city", "gini")
ginisec
write.csv(ginicities, "gini.csv",
row.names = FALSE)
aggregate(income ~ city,
data = cities,
FUN = "mean")
aggregate(income ~ city,
data = cities,
FUN = "median")
aggregate(income ~ city,
data = cities,
FUN = "length")
city income
A 40023
B 5021
C 60601
D 36432
E 40384
F 69831
G 84788
H 30061
I 26479
J 45864
A 26431
B 39166
C 58797
D 61843
E 33388
F 79576
G 43152
H 4250
I 51042
J 85666
A 96550
B 17677
C 67971
D 28128
E 5839
F 66074
G 5463
H 36234
I 55983
J 93604
A 7414
B 44543
C 44583
D 46161
E 97260
F 2876
G 74590
H 90571
I 3553
J 18291
A 49288
B 79271
C 99119
D 35386
E 70901
F 91583
G 86976
H 13125
I 35242
J 59232
A 37374
B 69062
C 99419
D 64812
E 35259
F 88355
G 69011
H 19994
I 43289
J 34800
A 97821
B 13461
C 73937
D 87482
E 12757
F 14247
G 52698
H 31996
I 21037
J 22927
A 84718
B 20290
C 9988
D 52029
E 71193
F 63638
G 47739
H 77749
I 99285
J 60056
A 67837
B 87613
C 12331
D 77029
E 78349
F 46370
G 97738
H 86821
I 32370
J 82038
A 69972
B 57523
C 14672
D 58639
E 19570
F 61386
G 4665
H 95662
I 23503
J 76017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.