word: Extract words from a sentence.

Description Usage Arguments Value Examples

Description

Extract words from a sentence.

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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('..'))

Example output

[1] "Jane" "Jane"
[1] "saw" "sat"
[1] "cat"  "down"
[1] "saw a cat" "sat down" 
[1] "Jane saw a cat" "saw a cat"      "a cat"         
[1] "Jane"           "Jane saw"       "Jane saw a"     "Jane saw a cat"
[1] "abc.def"
[1] "123.4568.999"

stringr documentation built on May 2, 2019, 11:03 a.m.