Skip to content

Commit

Permalink
Refactoring for clarity -> sequential ifs to elseif chain
Browse files Browse the repository at this point in the history
  • Loading branch information
nfultz committed Jan 17, 2018
1 parent ebaf3ee commit bd4b1fd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
37 changes: 17 additions & 20 deletions R/declare_ra.R
Expand Up @@ -129,38 +129,35 @@ declare_ra <- function(N = NULL,
)
}
# Determine ra_type
if (simple == FALSE) {
ra_type <- "complete"
} else{
ra_type <- "simple"
}
if (!is.null(blocks) & is.null(clusters)) {
ra_type <- "blocked"
}
if (is.null(blocks) & !is.null(clusters)) {
ra_type <- "clustered"
}
if (!is.null(blocks) & !is.null(clusters)) {
ra_type <- "blocked_and_clustered"
}
if (!is.null(permutation_matrix)){
ra_type <- "custom"
}

if (ra_type == "simple" & is.null(clusters)) {
} else if (!is.null(blocks) && !is.null(clusters)) {
ra_type <- "blocked_and_clustered"
} else if (!is.null(clusters)) {
ra_type <- "clustered"
} else if (!is.null(blocks)) {
ra_type <- "blocked"
if (simple) stop("You can't specify 'simple' when using blocked assignment")
} else if (simple == FALSE) {
ra_type <- "complete"
} else {
ra_type <- "simple"

if (!is.null(m)) {
stop("You can't specify 'm' when using simple random assignment.")
}
if (!is.null(m_each)) {
stop("You can't specify 'm_each' when using simple random assignment.")
}
if (!is.null(blocks)) {
stop("You can't specify 'blocks' when using simple random assignment.")
}
if (!is.null(block_m_each)) {
stop("You can't specify 'block_m_each' when using simple random assignment.")
}

}


if (ra_type == "simple") {

ra_function <- function() {
simple_ra(
N = N,
Expand Down
31 changes: 13 additions & 18 deletions R/declare_rs.R
Expand Up @@ -104,29 +104,24 @@ declare_rs <- function(N = NULL,

}
# Determine rs_type
if (simple == FALSE) {
if (!is.null(strata) && !is.null(clusters)) {
rs_type <- "stratified_and_clustered"
} else if (!is.null(clusters)) {
rs_type <- "clustered"
} else if (!is.null(strata)) {
rs_type <- "stratified"
if(simple) stop("You can't specify 'simple' with strata.")
} else if (simple == FALSE) {
rs_type <- "complete"
} else{
} else {
rs_type <- "simple"
}
if (!is.null(strata) & is.null(clusters)) {
rs_type <- "stratified"
}
if (is.null(strata) & !is.null(clusters)) {
rs_type <- "clustered"
}
if (!is.null(strata) & !is.null(clusters)) {
rs_type <- "stratified_and_clustered"
}

if (rs_type == "simple" & is.null(clusters)) {
if (!is.null(n)) {
stop("You can't specify 'n' when using simple random sampling.")
}
if (!is.null(strata)) {
stop("You can't specify 'strata' when using simple random sampling.")
}
}

if (rs_type == "simple") {

rs_function <- function() {
simple_rs(N = N,
prob = prob,
Expand Down

0 comments on commit bd4b1fd

Please sign in to comment.