Skip to content

Commit

Permalink
Merge pull request #27 from jferreri/master
Browse files Browse the repository at this point in the history
Conflicts resolved and README.Rmd and vignette updated.
  • Loading branch information
geanders committed Apr 27, 2016
2 parents 46bb9ea + 7eaf830 commit 22fbf9b
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 256 deletions.
7 changes: 3 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ output: html_document

Added new functions to convert wind speed and precipitation:

- **`speed_to_knots`:** Convert a vector of wind speed values to knots
- **`knots_to_speed`:** Convert a vector of wind speed values in knots to a specified wind speed unit
- **`inches_to_metric`:** Convert a vector of precipitation measurements in inches to measures in metric units (millimeters or centimeters)
- **`metric_to_inches`:** Convert a vector of precipitation measurements in metric units to measures in inches
- **`convert_wind_speed`:** Convert a vector of wind speed measurements between common units of wind speed measure (in knots, miles per hour, meters per second, feet per second, and kilometers per hour).
- **`convert_precip`:** Convert a vector of precipitation measurements between common units of precipitation measure (in inches, millimeters, and centimeters).
- **`convert_temperature`:** Convert a vector of temperature values between common units of temperature measure (in degrees Celsius, Fahrenheit, and Kelvins)

26 changes: 23 additions & 3 deletions R/rainmeasure_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@
#' loveland
#' @export
convert_precip <- function(precip, old_metric, new_metric, round = 2){
if(length(precip[precip < 0])){
precip[precip < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative precipitation. Since precipitation",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
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)
out <- inches_to_metric(mid, unit = new_metric, round = round)
}
return(out)
}
Expand Down Expand Up @@ -91,6 +97,13 @@ convert_precip <- function(precip, old_metric, new_metric, round = 2){
#'
#' @export
inches_to_metric <- function(inches, unit, round = 2) {
if(length(inches[inches < 0])){
inches[inches < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative precipitation. Since precipitation",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
if(unit == "mm"){
metric <- inches * 25.4
} else if(unit == "cm"){
Expand Down Expand Up @@ -140,7 +153,14 @@ inches_to_metric <- function(inches, unit, round = 2) {
#'
#' @export
metric_to_inches <- function(metric, unit.from, round = 2) {
if(unit.from == "mm"){
if(length(metric[metric < 0])){
metric[metric < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative precipitation. Since precipitation",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
if(unit.from == "mm"){
inches <- metric / 25.4
} else if(unit.from == "cm"){
inches <- metric / 2.54
Expand Down
24 changes: 23 additions & 1 deletion R/wind_conversions.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@
#'
#' @export
convert_wind_speed <- function(wind_speed, old_metric, new_metric, round = 1){
if(length(wind_speed[wind_speed < 0])){
wind_speed[wind_speed < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative wind speeds. Since wind speed",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
if(old_metric == new_metric){
stop("`old_metric` and `new_metric` must be different.")
}

if(old_metric == "knots"){
out <- knots_to_speed(wind_speed, unit = new_metric, round = round)
} else if (new_metric == "knots"){
Expand Down Expand Up @@ -98,6 +104,13 @@ convert_wind_speed <- function(wind_speed, old_metric, new_metric, round = 1){
#'
#' @export
speed_to_knots <- function(x, unit, round = 1) {
if(length(x[x < 0])){
x[x < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative wind speeds. Since wind speed",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
if(unit == "mph"){
knots <- x * 0.8689762
} else if(unit == "mps"){
Expand Down Expand Up @@ -152,7 +165,15 @@ speed_to_knots <- function(x, unit, round = 1) {
#' foco
#'
#' @export

knots_to_speed <- function(knots, unit, round = 1) {
if(length(knots[knots < 0])){
knots[knots < 0] <- NA
warning(paste("Some of the observations in the data gave",
"negative wind speeds. Since wind speed",
"cannot have a negative value, these",
"observations were set to 'NA'."))
}
if(unit == "mph"){
x <- knots * 1.1507794
} else if(unit == "mps"){
Expand All @@ -171,3 +192,4 @@ knots_to_speed <- function(knots, unit, round = 1) {
return(x)
}


Loading

0 comments on commit 22fbf9b

Please sign in to comment.