Select words or letters based on their position in character strings.

keepWords(
  str,
  slc = 1,
  collapse = " ",
  na.rm = FALSE,
  split_words = "[[:punct:][:space:]]+"
)

keepLetters(
  str,
  slc = 1,
  collapse = "",
  na.rm = FALSE,
  rm_punct = "[[:punct:][:space:]]+"
)

keepInitials(str, split_words = "[\n\t\r\f\b[:punct:] ]+", collapse = "")

wordCount(str, split_words = "[[:punct:][:space:]]+")

Arguments

str

an input character vector (or a list) from which words will be extracted.

slc

a vector of integer indicating the selected positions of the words (or letters) to be kept.

collapse

character string used to separate selected words (or letters), if NULL, then selection is not collapsed and a list is returned.

na.rm

a logical. Should missing values be removed?

split_words

a character string containing a regular expression used to split words.

rm_punct

a character string containing a regular expression used to remove punctuation characters.

Value

A vector (or a list) of the selected words.

Functions

  • keepLetters: A vector (or a list) of the selected letters.

  • keepInitials: A vector (or a list) of initials.

  • wordCount: A vector of the number of words for every character strings passed as an input.

See also

Examples

keepWords(loremIpsum(), 1:3)
#> [1] "Lorem ipsum dolor"
keepWords(c(loremIpsum(),'Another character string!'), slc = c(1,4))
#> [1] "Lorem sit" "Another NA"
keepWords(c(loremIpsum(),'A second character string.'), slc = c(1,4), na.rm = TRUE, collapse = '/')
#> [1] "Lorem/sit" "A/string"
strex <- c('Lorem ipsum', 'dolor sit', ' amet;') keepLetters(strex, c(1,4))
#> [1] "Le" "do" "at"
keepLetters(strex, c(1,4), collapse = "")
#> [1] "Le" "do" "at"
keepInitials("National Basketball Association")
#> [1] "NBA"
wordCount(c("two words!", "... and three words"))
#> [1] 2 3
wordCount(loremIpsum())
#> [1] 201