README.md

jpw

Personal R package to house miscellaneous tidbits of code.

Installation

You can install the development version of jpw from GitHub with:

install.packages("devtools")
devtools::install_github("WhiteJP/jpw")

And load it:

library(jpw)

Examples

Below are some examples of functions in jpw.

Simple statistical calcluations

censor_left(1:10, min = 5)
#>  [1]  5  5  5  5  5  6  7  8  9 10
censor_right(1:10, max = 5)
#>  [1] 1 2 3 4 5 5 5 5 5 5
censor_both(1:10, min = 3, max = 7)
#>  [1] 3 3 3 4 5 6 7 7 7 7
normalise(1:10) # min = 0 max = 1
#>  [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
#>  [8] 0.7777778 0.8888889 1.0000000
scale_minmax(1:10, out_min = -100, out_max = 100) # min = `mn`, max = `mx`
#>  [1] -100.00000  -77.77778  -55.55556  -33.33333  -11.11111   11.11111
#>  [7]   33.33333   55.55556   77.77778  100.00000

Other

vec2prose(c("Peanut", "Butter", "Jelly"), oxford_comma = FALSE)
#> [1] "Peanut, Butter and Jelly"
x <- c(1,  2, 3, NA,  5, NA, 7,  8, NA, 10)
y <- c(NA, 9, 8,  7,  5, NA, 4,  3,  2,  1)

na2na(x > 8 | x == y)
#>  [1]    NA FALSE FALSE    NA  TRUE    NA FALSE FALSE    NA  TRUE
extract_nums_tgthr(c("sdff234", "12aaa34"))
#> [1]  234 1234
extract_nums_all(c("12aaa34", "-12.4 54.3", "adfsdf.344 -1.2 4"))
#> [[1]]
#> [1] 12 34
#> 
#> [[2]]
#> [1] -12.4  54.3
#> 
#> [[3]]
#> [1] 344.0  -1.2   4.0
remove_nums(c("12aaa34",  "adfsdf.344 -1.2 4"), trimws = TRUE)
#> [1] "aaa"    "adfsdf"
int2bin(c(12, 5, NA, 90342))
#> [1] "1100"              "101"               NA                 
#> [4] "10110000011100110"
x <- c(100010, 234,  1, 34)
add_zeros(x) # default to longest nchar in vector
#> [1] "100010" "000234" "000001" "000034"
add_zeros(x, len = 9) # can stipulate longer
#> [1] "000100010" "000000234" "000000001" "000000034"
add_by_position(LETTERS[1:6], "NEW", 5)
#> [1] "A"   "B"   "C"   "D"   "NEW" "E"   "F"
rev_diag(3)
#>      [,1] [,2] [,3]
#> [1,]    0    0    1
#> [2,]    0    1    0
#> [3,]    1    0    0
rev_diag(6, x = 12)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    0    0    0    0    0   12
#> [2,]    0    0    0    0   12    0
#> [3,]    0    0    0   12    0    0
#> [4,]    0    0   12    0    0    0
#> [5,]    0   12    0    0    0    0
#> [6,]   12    0    0    0    0    0
brackets(123)
#> [1] "(123)"
brackets("foo", type = "square")
#> [1] "[foo]"
where(sum)
#> <environment: base>
where("sd")
#> <environment: package:stats>
#> attr(,"name")
#> [1] "package:stats"
#> attr(,"path")
#> [1] "C:/Program Files/R/R-4.3.0/library/stats"


WhiteJP/jpw documentation built on April 17, 2025, 5:47 a.m.