word: Extract words from a sentence

View source: R/word.R

wordR Documentation

Extract words from a sentence

Description

Extract words from a sentence

Usage

word(string, start = 1L, end = start, sep = fixed(" "))

Arguments

string

Input vector. Either a character vector, or something coercible to one.

start, end

Pair of integer vectors giving range of words (inclusive) to extract. If negative, counts backwards from the last word.

The default value select the first word.

sep

Separator between words. Defaults to single space.

Value

A character vector with the same length as string/start/end.

Examples

sentences <- c("Jane saw a cat", "Jane sat down")
word(sentences, 1)
word(sentences, 2)
word(sentences, -1)
word(sentences, 2, -1)

# Also vectorised over start and end
word(sentences[1], 1:3, -1)
word(sentences[1], 1, 1:4)

# Can define words by other separators
str <- 'abc.def..123.4568.999'
word(str, 1, sep = fixed('..'))
word(str, 2, sep = fixed('..'))

stringr documentation built on Nov. 15, 2023, 1:08 a.m.