Movatterモバイル変換


[0]ホーム

URL:


Skip to contents

Count occurrences of a substring in a string

Source:R/count.R
strs_count.Rd

strs_count counts the number of times a specified substring occurs in eachelement of a character vector. Optionally, the search can be limited to asubstring of each element, specified bystart andend positions. Thisfunction is similar to Python'sstr.count() method.

Usage

strs_count(string,substring, start=1L, end=-1L)

Arguments

string

A character vector where each element is a string in which tocount occurrences ofsubstring.

substring

The substring to count within each element ofstring.

start

An optional integer specifying the starting position in eachelement ofstring for the search. Defaults to 1, indicating the start ofthe string.

end

An optional integer specifying the ending position in each elementofstring for the search. The default value of -1 indicates the end of thestring.

Value

An integer vector of the same length asstring, with each elementindicating the count ofsubstring in the corresponding element ofstring.

See also

Python str.count() documentation

Examples

strs_count("hello world","o")#> [1] 2strs_count("banana","na")#> [1] 2strs_count("hello world","o", start=6)#> [1] 1strs_count("hello world","o", end=5)#> [1] 1

[8]ページ先頭

©2009-2025 Movatter.jp