Created
January 30, 2016 07:50
-
-
Save thmcmahon/c3b9604ed1661d67360b to your computer and use it in GitHub Desktop.
Interpolation, makes it happen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(zoo) | |
path <- "~/Desktop/Total Deaths - Assault - All Ages _ tom.csv" | |
df <- read.csv(path, na.strings = "...", skip = 1) | |
# transpose the dataframe (flip it) | |
df <- setNames(data.frame(t(df[,-1])), df[,1]) | |
# This just makes the names prettier | |
names(df) <- gsub("^ ", "", gsub("\\d", "", names(df))) | |
# Falkland Islands only has one value so there's nothing to interpolate | |
# so we'll set them all to 0 | |
df$`Falkland Islands (Malvinas)` <- 0 | |
# This is where the magic happens | |
df_i <- data.frame( # create a data_frame called df_i | |
lapply(df, # to each column apply the following function | |
# This function approximates the values, and then rounds it to the nearest | |
# whole number | |
function(x) round(na.approx(x, rule = 2)))) | |
# Make the row and column names pretty again | |
names(df_i) <- names(df) | |
row.names(df_i) <- 1983:2012 | |
# write out the results | |
write.csv(df_i, file = "assaults_interpolated.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment