24 Days of R: Day 23

December 23, 2013
By

(This article was first published on PirateGrunt » R, and kindly contributed to R-bloggers)

Penultimate post, I'm going to take a quick look at the Gini indicator for wealth inequality. Data comes from the World Bank.

I've downloaded the zipped file, decompressed it and given it a different name. I'm going to

dfGini = read.csv("./Data/Gini.csv", stringsAsFactors = FALSE, skip = 2)
colnames(dfGini) = gsub("X", "", colnames(dfGini))
library(reshape2)
mdf = melt(dfGini, id.vars = colnames(dfGini)[1:4])
mdf = mdf[!is.na(mdf$value), ]
colnames(mdf)[1:2] = c("Name", "Code")
library(plyr)
dfCount = ddply(mdf, "Code", .fun = nrow)
atLeast5 = dfCount$Code[dfCount$V1 >= 5]
mdf = mdf[mdf$Code %in% atLeast5, ]

This will give us a decent set of data. How does this look when we plot it?

library(ggplot2)
ggplot(mdf, aes(x = variable, y = value, group = Code)) + geom_line()

plot of chunk Plot

Ugh. That looks like nothing. It's a bit late and I'm not all that keen for insight. What countries have had the biggest reduction in income inequality?

dfMove = ddply(mdf, "Code", summarize, Diff = max(value) - min(value))
dfMove = dfMove[order(dfMove$Diff, decreasing = TRUE), ]
bigMoves = dfMove$Code[1:5]

ggplot(mdf[mdf$Code %in% bigMoves, ], aes(x = variable, y = value, group = Code)) + 
    geom_line()

plot of chunk BiggestMovers

That's better, but still not very informative. I'm sleepy and going to bed.

sessionInfo
## function (package = NULL) 
## {
##     z <- list()
##     z$R.version <- R.Version()
##     z$platform <- z$R.version$platform
##     if (nzchar(.Platform$r_arch)) 
##         z$platform <- paste(z$platform, .Platform$r_arch, sep = "/")
##     z$platform <- paste0(z$platform, " (", 8 * .Machine$sizeof.pointer, 
##         "-bit)")
##     z$locale <- Sys.getlocale()
##     if (is.null(package)) {
##         package <- grep("^package:", search(), value = TRUE)
##         keep <- sapply(package, function(x) x == "package:base" || 
##             !is.null(attr(as.environment(x), "path")))
##         package <- sub("^package:", "", package[keep])
##     }
##     pkgDesc <- lapply(package, packageDescription, encoding = NA)
##     if (length(package) == 0) 
##         stop("no valid packages were specified")
##     basePkgs <- sapply(pkgDesc, function(x) !is.null(x$Priority) && 
##         x$Priority == "base")
##     z$basePkgs <- package[basePkgs]
##     if (any(!basePkgs)) {
##         z$otherPkgs <- pkgDesc[!basePkgs]
##         names(z$otherPkgs) <- package[!basePkgs]
##     }
##     loadedOnly <- loadedNamespaces()
##     loadedOnly <- loadedOnly[!(loadedOnly %in% package)]
##     if (length(loadedOnly)) {
##         names(loadedOnly) <- loadedOnly
##         pkgDesc <- c(pkgDesc, lapply(loadedOnly, packageDescription))
##         z$loadedOnly <- pkgDesc[loadedOnly]
##     }
##     class(z) <- "sessionInfo"
##     z
## }
## <bytecode: 0x0000000012974858>
## <environment: namespace:utils>

To leave a comment for the author, please follow the link and comment on his blog: PirateGrunt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.