Skip to content

Instantly share code, notes, and snippets.

View aoyh's full-sized avatar

Aaron Ou aoyh

  • R, SQL, Google Analytics
  • Shanghai, China | Berkeley, US
View GitHub Profile
SELECT t.Country, t.Y1990 AS Metric, '1990' AS `Year` FROM tabular t
UNION
SELECT t.Country, t.Y1991 AS Metric, '1991' AS `Year` FROM tabular t
UNION
SELECT t.Country, t.Y1992 AS Metric, '1992' AS `Year` FROM tabular t
UNION
SELECT t.Country, t.Y1993 AS Metric, '1993' AS `Year` FROM tabular t
UNION
SELECT t.Country, t.Y1994 AS Metric, '1994' AS `Year` FROM tabular t
UNION
# Rcode to turn tabular data to flat records
# method 1, loop
d <- read.csv("http://oyh.coolpage.biz/blog_raw_data/tabular_to_flat/heatmap_data_tabular.txt", sep = "\t", header = T)
names(d)[1] <- "Country" # the input may encounter error in the first character at times. rename it
names(d)[-1] <- "Metric"
head(d) # assign the same name for yearly columns, so as to rbind() later on
dd <- d[, c(1,2)]
dd$Year <- 1990
j <- c(1991:1999)