Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

word

Extract words from a sentence.


Description

Extract words from a sentence.

Usage

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

Arguments

string

input character vector.

start

integer vector giving position of first word to extract. Defaults to first word. If negative, counts backwards from last character.

end

integer vector giving position of last word to extract. Defaults to first word. If negative, counts backwards from last character.

sep

separator between words. Defaults to single space.

Value

character vector of words from start to end (inclusive). Will be length of longest input argument.

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

Simple, Consistent Wrappers for Common String Operations

v1.4.0
GPL-2 | file LICENSE
Authors
Hadley Wickham [aut, cre, cph], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.