Skip to content

Commit

Permalink
Export uniquePresent
Browse files Browse the repository at this point in the history
  • Loading branch information
philipdelff committed Apr 15, 2024
1 parent 93bed7e commit 9244d64
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion R/NMreadTabSlow.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
##' slowly, and most often `NMreadTab` is a better function to
##' use. However, `NMreadTabslow` also works for table files that
##' contain incompatible tables.

##' @keywords internal
##'
### Polishing needed. Don't export.

NMreadTabSlow <- function(file){
Expand Down
27 changes: 23 additions & 4 deletions R/uniquePresent.R
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)
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ reference:
- NMorderColumns
- compareCols
- listMissings
- uniquePresent
- mergeCheck
- renameByContents
- flagsAssign
Expand Down Expand Up @@ -75,6 +76,7 @@ reference:
- NMreadCsv
- NMreadExt
- NMreadPhi
- NMreadShk
- NMreadTab
- NMreadParsText
- NMinfo
Expand Down
1 change: 1 addition & 0 deletions devel/todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ Not warning. Warning if standard variable?
*** flagsAssign
**** TODO [#A] flag.n should be factor
And flagsCount should retain them
Was this supposed to be flag.c?
**** DONE flagsCount should not require a flag table
**** DONE flagsCount should throw an error if not all flags are covered by provided flag table
**** CANCELLED flagsAssign and flagsCount should not offer increasing option.
Expand Down

0 comments on commit 9244d64

Please sign in to comment.