Skip to content

Instantly share code, notes, and snippets.

@dickoa
Last active August 29, 2015 14:10
Show Gist options
  • Save dickoa/8fea61a64287c8495b69 to your computer and use it in GitHub Desktop.
Save dickoa/8fea61a64287c8495b69 to your computer and use it in GitHub Desktop.
Reading NASA global temperature index using R
### Download file
url <- "http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
tmp <- tempfile()
download.file(url, destfile = tmp, method = "curl")
### Read the file and select colnames
dat <- readLines(tmp)
col_names <- unique(grep("^Year", dat, value = TRUE)) ## select colnames
col_names <- strsplit(col_names, "\\s+")[[1]]
### Remove header and turn '*' to NA
dat <- grep("^\\d+", dat, value = TRUE) ### select lines that start with a number (year)
dat <- gsub("\\*+", "NA", dat) ### *** == NA
### Collapse it to a unique character to and read it using read.table
dat <- paste(dat, collapse = "\n")
d <- read.table(text = dat, header = FALSE, col.names = col_names)
str(d)
## 'data.frame': 135 obs. of 20 variables:
## $ Year : int 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 ...
## $ Jan : int -32 -12 5 -36 -18 -55 -35 -57 -41 -17 ...
## $ Feb : int -27 -15 6 -36 -12 -27 -41 -41 -39 17 ...
## $ Mar : int -22 -1 0 -11 -29 -17 -32 -24 -37 8 ...
## $ Apr : int -29 -1 -22 -18 -34 -35 -21 -32 -23 8 ...
## $ May : int -16 -1 -18 -18 -31 -33 -19 -26 -24 0 ...
## $ Jun : int -23 -25 -30 -6 -34 -38 -28 -23 -19 -9 ...
## $ Jul : int -18 -10 -25 -1 -30 -26 -11 -17 -9 -11 ...
## $ Aug : int -10 -6 -9 -11 -23 -22 -19 -26 -12 -17 ...
## $ Sep : int -19 -16 -9 -17 -27 -16 -10 -22 -9 -17 ...
## $ Oct : int -18 -21 -23 -17 -23 -12 -21 -31 -2 -20 ...
## $ Nov : int -15 -26 -23 -26 -27 -12 -27 -27 0 -31 ...
## $ Dec : int -21 -16 -35 -18 -24 2 -15 -38 -9 -28 ...
## $ J.D : int -21 -12 -15 -18 -26 -24 -23 -30 -19 -10 ...
## $ D.N : int NA -13 -14 -19 -26 -26 -22 -28 -21 -8 ...
## $ DJF : int NA -16 -2 -36 -16 -35 -25 -38 -39 -3 ...
## $ MAM : int -22 -1 -13 -16 -31 -28 -24 -27 -28 6 ...
## $ JJA : int -17 -13 -21 -6 -29 -29 -19 -22 -13 -12 ...
## $ SON : int -18 -21 -18 -20 -26 -13 -19 -26 -4 -22 ...
## $ Year.1: int 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 ...
### Bonus
library(dplyr)
library(tidyr)
library(ggplot2)
d <- d %>%
select(Year, Jan:Dec) %>%
gather(month, temp, Jan:Dec) %>%
mutate(date = paste(Year, month, "01", sep = "-"),
date = as.Date(date, "%Y-%b-%d")) %>%
select(date, temp)
ggplot(d, aes(date, temp)) +
geom_point(shape = 19, size = 0.5) +
geom_line(size = 0.2) +
geom_smooth(colour = "red")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment