-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93bed7e
commit 9244d64
Showing
4 changed files
with
28 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
|
||
uniquePresent <- function(x,req.n1=TRUE){ | ||
##' Extract unique non-missing value from vector | ||
##' | ||
##' @param x A vector, either numeric or character. | ||
##' @param reg.n1 Require one unique value? If `TRUE` (default), an | ||
##' error is thrown if non-unique values found. If `FALSE`, all | ||
##' the unique values are returned. | ||
##' @param na.pattern In addition to NA-elements, what text strings | ||
##' should be considered missing? Default is empty strings and | ||
##' strings only containing white spaces (`na.pattern="^ *$"`). | ||
##' @details This function is particularly useful when combining data | ||
##' sets of which only some contain certain | ||
##' variables. `uniquePresent` with `req.n1=TRUE` makes sure the | ||
##' result is a single unique value (e.g., within subjects). A | ||
##' typical use is carrying subject-level covariates from one data | ||
##' set to another in a longitudinal analysis. | ||
##' @export | ||
uniquePresent <- function(x,req.n1=TRUE,na.pattern){ | ||
if(missing(na.regex)) na.pattern <- "^ *$" | ||
|
||
un.x <- unique(x) | ||
un.x <- un.x[un.x!=""&!is.na(un.x)] | ||
un.x <- un.x[!is.na(un.x) & !grepl(pattern=na.pattern,un.x) ] | ||
|
||
if(length(un.x)==0) { | ||
warning("No value found") | ||
return(NA) | ||
} | ||
if(req.n1&&length(un.x)!=1) stop("Number of unique values is not 1.") | ||
un.x | ||
|
||
return(un.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