Skip to content

Commit

Permalink
fix leading zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
wjschne committed May 22, 2020
1 parent 5beac66 commit 4cc48b7
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 104 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ Imports:
rlist
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
Suggests:
testthat (>= 2.1.0)
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export(composite_covariance)
export(cor_heat)
export(cor_text)
export(df2matrix)
export(make_clusters)
export(make_indicators)
export(parallel_analysis)
export(pdf2svg)
Expand Down
59 changes: 10 additions & 49 deletions R/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ proportion_round <- function(p, digits = 2) {

#' Rounds proportions to significant digits both near 0 and 1, then converts to percentiles
#'
#' @param p probabiity
#' @param p probability
#' @param digits rounding digits
#' @param remove_leading_zero If TRUE, remove leading zero
#' @param add_percent_character If TRUE, add percent character
#'
#' @return chracter vector
#' @return character vector
#' @export
#'
#' @examples
Expand Down Expand Up @@ -454,7 +454,6 @@ make_indicators <- function(latent, indicators = NULL, mu = 0.8, sigma = 0.05, k
#' @param ... parameters passed to psych::fa.parallel
#' @importFrom rlang .data
#' @import ggplot2
#' @return
#' @export
#'
#' @examples
Expand Down Expand Up @@ -531,17 +530,21 @@ attach_function <- function(f) {
#' Remove leading zero from numbers
#'
#' @param x vector of numbers
#' @param digits rounding digits
#'
#' @return vector of characters
#' @export
#'
#' @examples
#' remove_leading_zero(c(0.5,-0.2))
remove_leading_zero <- function(x) {
remove_leading_zero <- function(x, digits = NA) {
if (!is.na(digits)) {
x <- formatC(x , digits = digits, format = "f")
}
sub("^-0+","-", sub("^0+","",x))
}

#' Convert data.frame and tibbles to matices with named rows and columns
#' Convert data.frame and tibbles to matrices with named rows and columns
#'
#' @param d data.frame or tibble
#' @param first_col_is_row_names TRUE if first column has row names
Expand All @@ -562,49 +565,7 @@ df2matrix <- function(d, first_col_is_row_names = TRUE) {
as.matrix(d, rownames.force = T)
}

#' Create clustered data structures
#'
#' @param data a vector of level-2 sample sizes or a data.frame with level-2 data which includes dsample sizes in one of its columns
#' @param .cluster_id name of cluster id variable in level-2 data
#' @param .cluster_size name of cluster size variable in level-2 data
#' @param .id name of new level-1 id variable
#'
#' @return
#' @export
#'
#' @examples
#' # Number of clusters
#' k <- 20
#' # Level-2 data with cluster id, cluster size, and a level-2 covariate
#' d_l2 <- data.frame(
#' classroom_id = 1:k,
#' classroom_size = rpois(k, lambda = 30),
#' Xj = rnorm(k))
#'
#' # Make level-1 data frame
#' d_l1 <- make_clusters(
#' data = d_l2,
#' .cluster_id = "classroom_id",
#' .cluster_size = "classroom_size",
#' .id = "student_id")
#' d_l1
make_clusters <- function(
data,
.cluster_id = "cluster_id",
.cluster_size = "cluster_size",
.id = "id") {

if (is.numeric(data)) data <- tibble::tibble(!!rlang::ensym(.cluster_id) := 1:length(data), !!rlang::ensym(.cluster_size) := data)

d <- dplyr::mutate(
data,
.clusters = purrr::map2(
!!rlang::ensym(.cluster_id),
!!rlang::ensym(.cluster_size),
rep))
d <- tidyr::unnest(d, .clusters)
d <- dplyr::mutate(d, !!.id := 1:nrow(d))
dplyr::select(d, -.clusters)

}



4 changes: 2 additions & 2 deletions man/df2matrix.Rd

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

45 changes: 0 additions & 45 deletions man/make_clusters.Rd

This file was deleted.

3 changes: 0 additions & 3 deletions man/parallel_analysis.Rd

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

4 changes: 2 additions & 2 deletions man/proportion2percentile.Rd

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

4 changes: 3 additions & 1 deletion man/remove_leading_zero.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-main.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ test_that("equal mu if sigma = 0", {
test_that("between 0 and 1", {
expect_true(all(dplyr::between(rbeta_ms(100, mu = 0.6, sigma = 0.2),0,1)))
})


test_that("remove leading zero", {
expect_equal(remove_leading_zero(0.5, digits = 2), ".50")
expect_equal(remove_leading_zero(-0.5, digits = 2), "-.50")
expect_equal(remove_leading_zero(1.5, digits = 2), "1.50")
})

0 comments on commit 4cc48b7

Please sign in to comment.