GE03

Temperature Data for Climate Change App

#import the tidyverse library so its functions may be used later
library(tidyverse)  
## -- Attaching packages ---------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1     v purrr   0.2.4
## v tibble  1.4.1     v dplyr   0.7.4
## v tidyr   0.7.2     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## -- Conflicts ------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#import the data from the .csv file into a variable named monthly_temps
monthly_temps <- read.csv("monthly_global_land_and_ocean_temperature_anomalies.csv")

#import the libridate library so its functions may be used to manipulate variables
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
#the mutate function converts the integer into a Date object
#the truncated = 1 removes the day component
monthly_temps <- mutate(monthly_temps, date = ymd(YearMonth, truncated = 1))

#the mutate function extracts the year and month data from the Date object
#stores the year and month as separate variables
#the label = TRUE changes the month identifyer from a number to its name
monthly_temps <- mutate(monthly_temps, year = year(date), month = month(date, label = TRUE))

#the rename function renames the value variable to temperature anomaly
monthly_temps <- rename(monthly_temps, temperature_anomoly = Value)

#the select function drops the YearMonth variable 
monthly_temps <- select(monthly_temps, -YearMonth)

#writes then saves the data as an .RData object
write_csv(monthly_temps, path = "modified_monthly_temps.csv")
save(monthly_temps, file = "monthly_data.RData")

#import the data from the .csv file into a variable named gridded_data
#col_names modifies the data to create a list called monthly_data
gridded_data <- read_csv("gridded_data.csv", col_names = F)
## Parsed with column specification:
## cols(
##   .default = col_integer()
## )
## See spec(...) for full column specifications.
#creates the number_of_months variable which contains 1656 months
number_of_months <- nrow(gridded_data)/37

#creates the monthly_data vector which is a list of months
monthly_data <- vector("list", number_of_months)

#this for loop reads through the information in gridded_data to fill the monthly_data vector
for(i in 1:number_of_months)
{
  monthly_data[[i]] <- gridded_data[((i-1)*37+2):(i*37),]

  #get date for current month in yyyymm format
  integer_date <- gridded_data[((i-1)*37+1),2]*100+gridded_data[((i-1)*37+1),1] 
  
  #convert to Date class
  current_date <- ymd(integer_date ,truncated=1)  
  
  #extract named month
  current_month <- month(current_date, label=T) 
  
  #extract year
  current_year <- year(current_date)
  
  #paste together named month and year to name data
  names(monthly_data)[i] <- paste(current_month, current_year) 
  
  #save monthly data to object named current data
  current_data<- monthly_data[[i]]
  
  #create longitudes object that is a sequence from the first values
  longitutdes <- seq(from = -177.5, to=177.5, by=5)
  
  #create a vector of latitude values and populating latitude variable
  latitudes <- rev(seq(from=-87.5, to=87.5, by=5))
  
  #assign latitute vector values to latitudes
  current_data$latitude <- latitudes 

  #create column variables, setting column names  
  colnames(current_data) [1:72] <- longitutdes 

  #gather all temperature anomalies
  current_data <- gather(current_data, key = "longitude", value = "TempAnom", -latitude) 
  
  #change character values back into numeric
  current_data$longitude <- as.numeric(current_data$longitude)
  
  #convert anomalies back into Celsius
  current_data <-mutate(current_data, 
                        TempAnom = ifelse(TempAnom == -9999, NA, TempAnom/100))
  
  #add changed data into monthly data
  monthly_data[[i]] <- current_data

  
}

#saves the monthly_temps and monthly_data to a file named climate_data.RData
save(monthly_temps, monthly_data, file = "climate_data.RData")
css.php