Skip to content

Commit

Permalink
vec2string: add arg to combine other words
Browse files Browse the repository at this point in the history
  • Loading branch information
kbroman committed Aug 21, 2019
1 parent 077bc51 commit d84c865
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: broman
Version: 0.69-5
Date: 2019-04-11
Version: 0.70-1
Date: 2019-08-21
Title: Karl Broman's R Code
Description: Miscellaneous R functions, including functions related to
graphics (mostly for base graphics), permutation tests, running
Expand Down
6 changes: 6 additions & 0 deletions NEWS
@@ -1,6 +1,12 @@
Revision history for the R/broman package
-----------------------------------------

Version 0.70-1, 2019-08-21:

In `vec2string()`, add `conjunction` argument, to allow a different
combining word than `"and"`.


Version 0.69-5, 2019-04-11:

Added function ciplot() for plotting a set of confidence intervals.
Expand Down
9 changes: 6 additions & 3 deletions R/vec2string.R
Expand Up @@ -3,20 +3,23 @@
#' Turn a vector into a single character string with the items separated by commas and an "and".
#'
#' @param x A vector
#' @param conjunction Word used to combine the strings
#'
#' @export
#'
#' @examples
#' vec2string(letters[1:2])
#' vec2string(letters[1:4])
#' vec2string(letters[1:4], "or")
vec2string <-
function(x)
function(x, conjunction="and")
{
n <- length(x)

if(n==0) return("")
if(n==1) return(paste(x))
if(n==2) return(paste(x, collapse=" and "))
if(n==2) return(paste(x, collapse=paste0(" ", conjunction, " ")))

paste(paste(x[-n], collapse=", "), x[n], sep=", and ")
paste(paste(x[-n], collapse=", "), x[n],
sep=paste0(", ", conjunction, " "))
}
5 changes: 4 additions & 1 deletion man/vec2string.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-vec2string.R
Expand Up @@ -7,4 +7,9 @@ test_that("vec2string works", {
expect_equal(vec2string(c("a", "b")), "a and b")
expect_equal(vec2string(c("a", "b", "c", "d")), "a, b, c, and d")

expect_equal(vec2string(NULL, "or"), "")
expect_equal(vec2string("a", "or"), "a")
expect_equal(vec2string(c("a", "b"), "or"), "a or b")
expect_equal(vec2string(c("a", "b", "c", "d"), "or"), "a, b, c, or d")

})

0 comments on commit d84c865

Please sign in to comment.