-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from orichters/master
add unitjoin to undo unitsplit
- Loading branch information
Showing
11 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' joins a data.frame or vector of strings with variable and unit separated into | ||
#' a data.frame with variable and unit joined as 'variable (unit)'. | ||
#' Use magclass::unitsplit to split them again | ||
#' | ||
#' @param x data.frame or vector of strings | ||
#' @param col column name. Default: variable | ||
#' @param unit vector of strings. If NULL, col 'unit' in x is used | ||
#' @return data.frame or vector of strings, dependent on x | ||
#' | ||
#' @export | ||
unitjoin <- function(x, unit = NULL, col = "variable") { | ||
# check whether is a data.frame | ||
if (is.null(ncol(x))) { | ||
if (length(x) == 0 || is.null(unit)) { | ||
# return empty variable vector or if no unit specified | ||
return(x) | ||
} else { | ||
# paste unit to it, keeping factor type | ||
if (is.factor(x)) { | ||
return(as.factor(paste0(x, " (", unit, ")"))) | ||
} else { | ||
return(paste0(x, " (", unit, ")")) | ||
} | ||
} | ||
} else { # is data.frame | ||
# no unit is specified -> take from unit column | ||
stopifnot(col %in% colnames(x)) | ||
if (is.null(unit)) { | ||
x[col] <- unitjoin(x[[col]], x[["unit"]]) | ||
x["unit"] <- NULL | ||
} else { | ||
# use what is specified | ||
x[col] <- unitjoin(x[[col]], unit) | ||
} | ||
return(x) | ||
} | ||
} |
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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