49 lines
996 B
R
49 lines
996 B
R
#
|
|
# Load data.
|
|
#
|
|
|
|
htypes <- read.csv("data/household-types.tsv", sep="\t")
|
|
head(htypes)
|
|
|
|
|
|
#
|
|
# Reformat data.
|
|
#
|
|
|
|
years <- unique(htypes$year)
|
|
uhtypes <- unique(htypes$htype)
|
|
|
|
# Start data frame
|
|
htypes_new <- data.frame(year = years)
|
|
|
|
# Add columns
|
|
for (uht in uhtypes) {
|
|
curr <- htypes[htypes$htype == uht,]
|
|
curr_name <- paste0("p_", gsub("-", "_", uht))
|
|
htypes_new[, curr_name] <- curr$p[match(htypes_new$year, curr$year)]
|
|
}
|
|
|
|
# Save formatted data
|
|
write.table(htypes_new,
|
|
file="data/htypes_rectangular.tsv",
|
|
sep="\t",
|
|
row.names=FALSE)
|
|
|
|
|
|
# Transpose?
|
|
htypes_long <- as.data.frame(t(htypes_new[,-1]))
|
|
colnames(htypes_long) <- htypes_new$year
|
|
htypes_long$waymet <- gsub("p_", "", row.names(htypes_long))
|
|
write.table(htypes_long, file="data/htypes_long.tsv", sep="\t", row.names=FALSE)
|
|
|
|
|
|
|
|
#
|
|
# Alluvial diagram
|
|
#
|
|
|
|
# install.packages("alluvial")
|
|
library(alluvial)
|
|
alluvial_ts(htypes[,c("htype", "year", "p")],
|
|
wave = .3, lwd = .5, ygap=1)
|