Created
November 15, 2015 22:05
-
-
Save TonyLadson/5609ad2ca33f5f50ad5c to your computer and use it in GitHub Desktop.
Tools for Hystra files. See https://tonyladson.wordpress.com/2015/05/06/reading-in-data-from-supplied-from-hydstra/
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
| require(ggplot2) | |
| require(zoo) | |
| require(dplyr) | |
| # MakeNames_Hydstra | |
| # Hydstra files have names spread across 4 rows. This gist, turns the data into a single string | |
| MakeNames_Hydstra <- function(Hydstra.csv){ | |
| r1 <- as.vector(unlist(Hydstra.csv[1, ])) | |
| r1 <- zoo::na.locf(r1) | |
| r1 <- paste0('X', r1) | |
| r3 <- as.vector(unlist(Hydstra.csv[3, ])) | |
| r3 <- na.locf(r3) | |
| r3 <- tolower(substring(r3,1,1)) | |
| r4 <- as.vector(unlist(Hydstra.csv[4, ])) | |
| r4 <- tolower(substring(r4,1,1)) | |
| my.names <- paste(r1, r3, r4, sep=".") | |
| my.names[1] <- "date.time" | |
| my.names | |
| } | |
| # HydstraSummary | |
| # produce a data frame that lists column name, start of non-missing data, end of non-missing data | |
| # number of data points, number of missing and percentage missing | |
| # date.col = name of the column that contains dates | |
| # df = data frame | |
| HydstraSummary <- function(date.col, df) { | |
| .HydraColumnSummary <- function(my.col, date.col, df){ | |
| start.row <- min(which(!is.na(df[[my.col]]))) | |
| end.row <- max(which(!is.na(df[[my.col]]))) | |
| if(is.finite(start.row) & is.finite(end.row)) { | |
| n.missing <- sum(is.na(df[[my.col]][start.row:end.row])) | |
| n.missing.pc <- round(100* mean(is.na(df[[my.col]][start.row:end.row])),2) | |
| n = end.row - start.row + 1 | |
| start <- df[[date.col]][min(which(!is.na(df[[my.col]])))] | |
| end <- df[[date.col]][max(which(!is.na(df[[my.col]])))] | |
| } else { | |
| n.missing = NA | |
| n.missing.pc = 100 | |
| n = 0 | |
| start = NA | |
| end = NA | |
| } | |
| data.frame(my.col = my.col, start, end, n, n.missing, n.missing.pc) | |
| } | |
| x <- lapply(names(df), .HydraColumnSummary, date.col = date.col, df=df) | |
| out <- do.call(rbind,x) | |
| out | |
| } | |
| # Given the file name of a Hydstra file, returns a data frame | |
| Make_df_Hydstra <- function(fname) { | |
| # Read header | |
| hydstra.head <- read.csv(file = fname, header = FALSE, nrows = 4, na.strings=c(NA,""), stringsAsFactors = FALSE) | |
| # Read data | |
| hydstra.data <- tbl_df(read.csv(file = fname, header = FALSE, nrows = -1, skip = 4, na.strings=c(NA,""), stringsAsFactors = FALSE)) | |
| # Assign names | |
| my.names <- MakeNames_Hydstra(hydstra.head) | |
| my.names[1] <- 'date.time' | |
| names(hydstra.data) <- my.names | |
| # return data frame | |
| hydstra.data | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment