Last active
April 30, 2020 16:57
-
-
Save jebyrnes/7bfae01b90b5434cdb24a0cd8fc9c519 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(tidyverse) | |
library(readr) | |
get_buoy <- function(a_year){ | |
one_buoy <- read_buoy(a_year) %>% | |
format_buoy %>% | |
make_monthly_buoy | |
return(one_buoy) | |
} | |
read_buoy <- function(a_year, buoy_number = 44013, | |
buoy_dir = "./data/buoydata/"){ | |
#make a file name | |
buoy_file <- str_c(buoy_dir, buoy_number, "_", a_year, ".csv") | |
#read in the file | |
one_buoy <- read_csv(buoy_file, | |
na = c("99", "999", | |
"99.00", "9999.00", | |
"99.0", "9999.0", | |
"999.0")) | |
#return the file | |
return(one_buoy) | |
} | |
format_buoy <- function(a_buoy_df){ | |
a_buoy_df <- a_buoy_df %>% | |
fix_year_names %>% | |
fix_bad_rows %>% | |
fix_bad_years | |
return(a_buoy_df) | |
} | |
#bad names | |
fix_year_names <- function(a_buoy_df){ | |
names(a_buoy_df) <- names(a_buoy_df) %>% | |
str_replace("^YY$", "YYYY") %>% | |
str_replace("X\\.YY", "YYYY") | |
return(a_buoy_df) | |
} | |
fix_bad_rows <- function(a_buoy_df){ | |
if(is.character(a_buoy_df$MM[1])){ | |
a_buoy_df <- a_buoy_df[-1,] %>% | |
mutate_all(as.numeric) | |
} | |
return(a_buoy_df) | |
} | |
make_monthly_buoy <- function(a_buoy_df){ | |
buoydata <- a_buoy_df %>% | |
select(YYYY, MM, WVHT, WTMP) %>% | |
rename(Year = YYYY, | |
Month = MM, | |
Wave_Height = WVHT, | |
Temperature_c = WTMP) %>% | |
group_by(Year, Month) %>% | |
summarise(Wave_Height = mean(Wave_Height, na.rm=T), | |
Temperature_c = mean(Temperature_c, na.rm=T)) %>% | |
ungroup() | |
return(buoydata) | |
} | |
library(tidyverse) | |
library(readr) | |
get_buoy <- function(a_year){ | |
one_buoy <- read_buoy(a_year) %>% | |
format_buoy %>% | |
make_monthly_buoy | |
return(one_buoy) | |
} | |
read_buoy <- function(a_year, buoy_number = 44013, | |
buoy_dir = "./data/buoydata/"){ | |
#make a file name | |
buoy_file <- str_c(buoy_dir, buoy_number, "_", a_year, ".csv") | |
#read in the file | |
one_buoy <- read_csv(buoy_file, | |
na = c("99", "999", | |
"99.00", "9999.00", | |
"99.0", "9999.0", | |
"999.0")) | |
#return the file | |
return(one_buoy) | |
} | |
format_buoy <- function(a_buoy_df){ | |
a_buoy_df <- a_buoy_df %>% | |
fix_year_names %>% | |
fix_bad_rows %>% | |
fix_bad_years | |
return(a_buoy_df) | |
} | |
#bad names | |
fix_year_names <- function(a_buoy_df){ | |
names(a_buoy_df) <- names(a_buoy_df) %>% | |
str_replace("^YY$", "YYYY") %>% | |
str_replace("X\\.YY", "YYYY") | |
return(a_buoy_df) | |
} | |
fix_bad_rows <- function(a_buoy_df){ | |
if(is.character(a_buoy_df$MM[1])){ | |
a_buoy_df <- a_buoy_df[-1,] %>% | |
mutate_all(as.numeric) | |
} | |
return(a_buoy_df) | |
} | |
make_monthly_buoy <- function(a_buoy_df){ | |
buoydata <- a_buoy_df %>% | |
select(YYYY, MM, WVHT, WTMP) %>% | |
rename(Year = YYYY, | |
Month = MM, | |
Wave_Height = WVHT, | |
Temperature_c = WTMP) %>% | |
group_by(Year, Month) %>% | |
summarise(Wave_Height = mean(Wave_Height, na.rm=T), | |
Temperature_c = mean(Temperature_c, na.rm=T)) %>% | |
ungroup() | |
return(buoydata) | |
} | |
fix_bad_years <- function(a_buoy_df){ | |
#start with a buoy data frame | |
a_buoy_df <- a_buoy_df %>% | |
#if the YYYY col is less than 1900, add 1900 to it, otherwise (else) do nothing | |
mutate(YYYY = ifelse(YYYY < 1900, YYYY+1900, YYYY)) | |
return(a_buoy_df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment