Skip to content

Commit e9a1327

Browse files
authored
Merge pull request #458 from Crunch-io/flatline
first pass at creating flatline variables
2 parents 96a82a6 + 25ab1ba commit e9a1327

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Collate:
137137
'progress.R'
138138
'project-folder.R'
139139
'projects.R'
140+
'row-distinct.R'
140141
'search.R'
141142
'session.R'
142143
'share.R'
@@ -171,3 +172,5 @@ Collate:
171172
'versions.R'
172173
'weight.R'
173174
'zcl.R'
175+
Remotes:
176+
rstudio/rmarkdown

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export(restoreVersion)
252252
export(rmdir)
253253
export(rollup)
254254
export(rollupResolution)
255+
export(rowDistinct)
255256
export(rstandard)
256257
export(saveVersion)
257258
export(scoreCatToFeat)
@@ -269,6 +270,7 @@ export(shojiURL)
269270
export(shojifyDatasetMetadata)
270271
export(slideCategories)
271272
export(slides)
273+
export(straightlineResponse)
272274
export(streaming)
273275
export(subtitle)
274276
export(subtitles)

R/row-distinct.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Create variables useful for determining whether a row's values are suspicious
2+
#'
3+
#' `rowDistinct()` finds the number of unique values given per row of variables in an array
4+
#' `CrunchVariable`. `straightlineResponse()` returns a `selection` variable that indicates
5+
#' whether the responses are identical. When a row has all columns that are missing of the
6+
#' same type, it will return `Selected`, but will missing if any other number of values is missing
7+
#' (or there are multiple types of missing).
8+
#'
9+
#' @param x A `CrunchVariable`that is an an array, that unique values should be counted across.
10+
#' @param ... Optional attributes, like `name`, to set on the new variable (passed to `VarDef()`)
11+
#' @param na.rm Whether to count missing data as a separate category (all missing categories will be
12+
#' lumped together)
13+
#'
14+
#' @return A Variable Definition, which can be used to create a new `CrunchVariable`
15+
#' @export
16+
rowDistinct <- function(x, ..., na.rm = TRUE) {
17+
if (!is.Array(x)) halt("x must be an array variable")
18+
19+
if (na.rm) {
20+
unique_func <- function(x) length(unique(as.character(x[!is.na(x)])))
21+
} else {
22+
unique_func <- function(x) length(unique(as.character(x)))
23+
}
24+
25+
VarDef(
26+
apply(as.vector(x), 1, unique_func),
27+
...
28+
)
29+
}
30+
31+
#' @export
32+
#' @rdname rowDistinct
33+
straightlineResponse <- function(x, ...) {
34+
if (!is.Array(x)) halt("x must be an array variable")
35+
36+
subvar_aliases <- aliases(subvariables(x))
37+
if (length(subvar_aliases) == 1) stop("Array must have more than 1 subvariable.")
38+
VarDef(
39+
Reduce(
40+
`&`,
41+
lapply(subvar_aliases[-1], function(sv) x[[sv]] == x[[subvar_aliases[1]]])
42+
),
43+
...
44+
)
45+
}

man/rowDistinct.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-row-distinct.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
with_mock_crunch({
2+
ds <- loadDataset("test ds")
3+
4+
test_that("rowDistinct works (na.rm = TRUE)", {
5+
expect_equal(
6+
rowDistinct(ds$catarray, name = "x"),
7+
VarDef(
8+
as.integer(c(2, 2, 2, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 2)), # nolint
9+
name = "x"
10+
)
11+
)
12+
})
13+
14+
test_that("rowDistinct works (na.rm = FALSE)", {
15+
expect_equal(
16+
rowDistinct(ds$catarray, name = "x", na.rm = FALSE),
17+
VarDef(
18+
as.integer(c(2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 3, 1, 2, 1, 2, 2, 2)), # nolint
19+
name = "x"
20+
)
21+
)
22+
})
23+
24+
test_that("error for non-array var rowDistinct", {
25+
expect_error(rowDistinct(ds$textvar), "x must be an array variable")
26+
})
27+
28+
test_that("straightlineResponse works", {
29+
expect_equal(
30+
straightlineResponse(ds$catarray, name = "x"),
31+
VarDef(
32+
ds$catarray$subvar1 == ds$catarray$subvar2 & ds$catarray$subvar3 == ds$catarray$subvar2, #nolint
33+
name = "x"
34+
)
35+
)
36+
})
37+
38+
test_that("error for non-array var straightline", {
39+
expect_error(straightlineResponse(ds$textvar), "x must be an array variable")
40+
})
41+
})

0 commit comments

Comments
 (0)