Description Usage Arguments Value Note Examples
View source: R/VectorSentence.R
Create a natural language, comma separated list
1 2 3 4 5 6 7 8 | VectorSentence(
x,
y = NA,
OxfordComma = T,
End = "and ",
hyphenate = 3,
messageLevel = 0
)
|
x |
a vector of values, some subset of which are to be listed in the ouput string |
y |
a logical vector of the same length as /codex, indicating whether each element should be included |
OxfordComma |
logical, should the Oxford (or serial) comma be used? |
End |
what word or phrase should be inserted prior to the final element in the list? |
hyphenate |
what is the min # of consecutive elements that will be hyphenated? If <2, no hyphenation. |
atomic character
This function creates a string that lists the selected elements of a vector. The elements will be separated by commas, including the Oxford comma (if OxfordComma = T). If there are runs of consecutive elements selected, they will be grouped using hyphenation (if hyphenate > 1). Hyphenation refers to abbreviating a run of consecutive elements by using just the first and last, separated by a hyphen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | # Sample Data 1
x = c(as.character(1:4), "5a", "5b", "5c", as.character(6:41),"D1", "D2", "D3a", "D4a", "D5", "D6", "Essay", letters[5:20])
y = sample(c(T, F), size = length(x), replace = TRUE) # elements to use
if (sum(y) < length(y)/2){y = !y} # make sure at least half of the elements are True
# Test Run 1
# look at the stuff
VectorSentence(x,y) #basic call
VectorSentence(x,y, hyphenate = 4, End = "and also ") # change the minimum hyphenation and the final words
VectorSentence(x,y, hyphenate = 8, End = "and also ")
VectorSentence(x, y, OxfordComma = F, End = "and ", hyphenate = 2) # turn off the oxford comma and hyphenate pairs
VectorSentence(x, y, OxfordComma = T, End = "", hyphenate = 4) # eliminate the final words
VectorSentence(x, y, OxfordComma = F, End = "", hyphenate = 3) # no final words or oxford comma
VectorSentence(x, y, OxfordComma = T, End = "", hyphenate = 1) # no hyphenation at all
# Sample Data 2
y = rep(sample(c(T, F), size = round(length(x)/2), replace = TRUE), each =2)
# Test Run 2
y
VectorSentence(x,y)
VectorSentence(x,y, hyphenate = 3, End = "and also ")
VectorSentence(x,y, hyphenate = 8, End = "and also ")
VectorSentence(x, y, OxfordComma = F, End = "and ", hyphenate = 2)
VectorSentence(x, y, OxfordComma = T, End = "", hyphenate = 4)
VectorSentence(x, y, OxfordComma = F, End = "", hyphenate = 3)
VectorSentence(x, y, OxfordComma = T, End = "", hyphenate = 1)
# Sample Data 3
x = c("a","b","c","d")
# Test Run 3
VectorSentence(x)
VectorSentence(x, hyphenate = 5)
VectorSentence(x, OxfordComma = F, End = "", hyphenate = 1)
VectorSentence(x, OxfordComma = F, End = "and lastly ", hyphenate = 1)
VectorSentence(x, OxfordComma = F, End = "and lastly ", hyphenate = 2)
# Sample Data 4
x = letters[1:10]
y = c(T,T,T,T,F,T,T,T,T,T)
# Test Run 4
VectorSentence(x,y)
VectorSentence(x,y, hyphenate = 5)
VectorSentence(x,y, OxfordComma = F, End = "", hyphenate = 1)
VectorSentence(x,y, OxfordComma = F, End = "and lastly ", hyphenate = 1)
VectorSentence(x,y, OxfordComma = F, End = "and lastly ", hyphenate = 2)
VectorSentence(x,y, OxfordComma = T, End = "and lastly ", hyphenate = 2)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.