Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ setGeneric("is.selected<-", function (x, value) standardGeneric("is.selected<-")

setGeneric("ids", function (x) standardGeneric("ids"))
setGeneric("ids<-", function (x, value) standardGeneric("ids<-"))
setGeneric("is.dichotomized", function (x) standardGeneric("is.dichotomized"))
setGeneric("dichotomize", function (x, i) standardGeneric("dichotomize"))
setGeneric("undichotomize", function (x) standardGeneric("undichotomize"))
setGeneric("is.dichotomized", function (categories) standardGeneric("is.dichotomized"))
setGeneric("dichotomize", function (categories, selection) standardGeneric("dichotomize"))
setGeneric("undichotomize", function (categories) standardGeneric("undichotomize"))
setGeneric("value", function (x) standardGeneric("value"))
setGeneric("value<-", function (x, value) standardGeneric("value<-"))
setGeneric("name", function (x) standardGeneric("name"))
Expand Down
52 changes: 31 additions & 21 deletions R/dichotomize.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@
#' `undichotomize` strips that selection information. Dichotomize converts
#' a Categorical Array to a Multiple Response, and undichotomize does the reverse.
#'
#' @param x Categories or a Variable subclass that has Categories
#' @param i For the `dichotomize` methods, the numeric or logical indices
#' @param categories Categories or a Variable subclass that has Categories
#' @param selection For the `dichotomize` methods, the numeric or logical indices
#' of the categories to mark as "selected", or if character, the Category
#' "names". Note that unlike some other categorical variable methods,
#' numeric indices are positional, not with reference to category ids.
#' @return Categories or the Variable, (un)dichotomized accordingly
#' @name dichotomize
#' @aliases dichotomize is.dichotomized undichotomize
#' @seealso [`describe-category`]
#' @examples
#' \dontrun{
#' ds$sub1 <- factor(1:nrow(ds))
#' ds$sub2 <- factor(1:nrow(ds))
#' ds$arr <- makeArray(ds[, c("sub1", "sub2")], "array")
#' ds$arr <- dichotomize(ds$arr, 3)
#' class(ds$arr)
#' ds$arr <- undichotomize(ds$arr)
#' class(ds$arr)
#' }
NULL

#' @rdname dichotomize
#' @export
setMethod("is.dichotomized", "Categories",
function (x) any(vapply(x, is.selected, logical(1))))
function (categories) any(vapply(categories, is.selected, logical(1))))

.dichotomize.categories <- function (x, i) {
.dichotomize.categories <- function (categories, selection) {
## Internal method for dichtomizing Categories (or lists)
is.selected(x[i]) <- TRUE
return(x)
is.selected(categories[selection]) <- TRUE
return(categories)
}

#' @rdname dichotomize
Expand All @@ -37,35 +47,35 @@ setMethod("dichotomize", c("Categories", "numeric"), .dichotomize.categories)
setMethod("dichotomize", c("Categories", "logical"), .dichotomize.categories)
#' @rdname dichotomize
#' @export
setMethod("dichotomize", c("Categories", "character"), function (x, i) {
ind <- names(x) %in% i
setMethod("dichotomize", c("Categories", "character"), function (categories, selection) {
ind <- names(categories) %in% selection
if (!any(ind)) {
halt("Category not found") ## make nicer error message
}
return(dichotomize(x, ind))
return(dichotomize(categories, ind))
})

#' @rdname dichotomize
#' @export
setMethod("undichotomize", "Categories", function (x) {
is.selected(x) <- FALSE
return(x)
setMethod("undichotomize", "Categories", function (categories) {
is.selected(categories) <- FALSE
return(categories)
})

.dichotomize.var <- function (x, i) {
newcats <- dichotomize(categories(x), i)
categories(x) <- newcats
.dichotomize.var <- function (categories, selection) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I don't think categories as an argument makes sense: when the method/function is for a variable (or is defined both for Categories and CategoricalArrayVariable). categories(categories)?

newcats <- dichotomize(categories(categories), selection)
categories(categories) <- newcats
if (is.dichotomized(newcats)) {
## Do this to avoid needing to refresh the variable catalog
x@tuple@body$type <- "multiple_response"
categories@tuple@body$type <- "multiple_response"
}
invisible(CrunchVariable(tuple(x)))
invisible(CrunchVariable(tuple(categories)))
}
.undichotomize.var <- function (x) {
categories(x) <- undichotomize(categories(x))
.undichotomize.var <- function (categories) {
categories(categories) <- undichotomize(categories(categories))
## Do this to avoid needing to refresh the variable catalog
x@tuple@body$type <- "categorical_array"
invisible(CrunchVariable(tuple(x)))
categories@tuple@body$type <- "categorical_array"
invisible(CrunchVariable(tuple(categories)))
}

#' @rdname dichotomize
Expand Down
33 changes: 22 additions & 11 deletions man/dichotomize.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/test-flip-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ with_test_authentication({
})
ds <- addVariables(ds, newvars)
test_that("The flipped variables are created", {
print(names(ds))
expect_true(all(c("Home, flipped", "Work, flipped", "Pet, flipped") %in% names(variables(ds))))
expect_true(all(c("Home, flipped", "Work, flipped", "Pet, flipped") %in% names(ds)))
expect_identical(names(subvariables(ds[["Home, flipped"]])),
Expand Down