Skip to content

Commit

Permalink
Add convert_precip function.
Browse files Browse the repository at this point in the history
Although, there's some bug in going between mm and cm. We'll need to check that out some more.
  • Loading branch information
Brooke Anderson committed Apr 18, 2016
1 parent 1b9f3f5 commit 46bb9ea
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(celsius.to.fahrenheit)
export(celsius.to.kelvin)
export(convert_precip)
export(convert_temperature)
export(convert_wind_speed)
export(dewpoint.to.humidity)
Expand Down
53 changes: 52 additions & 1 deletion R/rainmeasure_conversion.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
#' Convert between precipitation metrics
#'
#' This function allows you to convert among the following precipitation metrics:
#' inches, millimeters, and centimeters.
#'
#' @param precip A numerical vector of precipitation to be converted.
#' @param old_metric The metric from which you want to convert. Possible options
#' are:
#' \itemize{
#' \item \code{inches}: Inches
#' \item \code{mm}: Millimeters
#' \item \code{cm}: Centimeters
#' }
#' @inheritParams convert_temperature
#'
#' @return A numeric vector with precipitation converted to the metric specified
#' by the argument \code{new_metric}.
#'
#' @author
#' Brooke Anderson \email{brooke.anderson@@colostate.edu},
#' Joshua Ferreri \email{joshua.m.ferreri@@gmail.com}
#'
#' @examples
#'
#' data(breck)
#' breck$Precip.mm <- convert_precip(breck$Precip.in,
#' old_metric = "inches", new_metric = "mm", round = 2)
#' breck
#'
#' data(loveland)
#' loveland$Precip.in <- convert_precip(loveland$Precip.mm,
#' old_metric = "mm", new_metric = "inches", round = NULL)
#' loveland$Precip.cm <- convert_precip(loveland$Precip.mm,
#' old_metric = "mm", new_metric = "cm", round = 3)
#' loveland
#' @export
convert_precip <- function(precip, old_metric, new_metric, round = 2){
if(old_metric == new_metric){
stop("`old_metric` and `new_metric` must be different.")
}

if(old_metric == "inches"){
out <- inches_to_metric(precip, unit = new_metric, round = round)
} else if (new_metric == "inches"){
out <- metric_to_inches(precip, unit.from = old_metric, round = round)
} else {
mid <- metric_to_inches(precip, unit.from = old_metric, round = NULL)
out <- inches_to_metric(precip, unit = new_metric, round = round)
}
return(out)
}

#' Convert from inches to standard metric units of measure for precipitation
#'
#' \code{inches_to_metric} creates a numeric vector of precipitation in
Expand Down Expand Up @@ -34,7 +86,6 @@
#' data(breck)
#' breck$Precip.mm <- inches_to_metric(breck$Precip.in,
#' unit = "mm",
#' round.out = TRUE,
#' round = 2)
#' breck
#'
Expand Down
2 changes: 1 addition & 1 deletion R/wind_conversions.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' }
#' @inheritParams convert_temperature
#'
#' @return A numeric vector with temperature converted to the metric specified
#' @return A numeric vector with wind speed converted to the metric specified
#' by the argument \code{new_metric}.
#'
#' @author
Expand Down
52 changes: 52 additions & 0 deletions man/convert_precip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/convert_wind_speed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/inches_to_metric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46bb9ea

Please sign in to comment.