stringr: Simple, Consistent Wrappers for Common String Operations

文字列操作

> library(stringr)

バージョン: 1.1.0


関数名 概略
case Convert case of a string.
invert_match Switch location of matches to location of non-matches.
modifiers Control matching behaviour with modifier functions.
str_c Join multiple strings into a single string.
str_conv Specify the encoding of a string.
str_count Count the number of matches in a string.
str_detect Detect the presence or absence of a pattern in a string.
str_dup Duplicate and concatenate strings within a character vector.
str_extract Extract matching patterns from a string.
str_length The length of a string.
str_locate Locate the position of patterns in a string.
str_match Extract matched groups from a string.
str_order Order or sort a character vector.
str_pad Pad a string.
str_replace Replace matched patterns in a string.
str_replace_na Turn NA into "NA"
str_split Split up a string into pieces.
str_sub Extract and replace substrings from a character vector.
str_subset Keep strings matching a pattern.
str_trim Trim whitespace from start and end of string.
str_wrap Wrap strings into nicely formatted paragraphs.
stringr Fast and friendly string manipulation.
word Extract words from a sentence.

case / str_to_upper / str_to_lower / str_to_title

大文字と小文字の変換

Arguments

  • string
  • locale
> "Hello world" %>% {
+   print(str_to_upper(.))
+   print(str_to_lower(.))
+   str_to_title(.)
+ }
[1] "HELLO WORLD"
[1] "hello world"
[1] "Hello World"

str_c

文字列結合

Arguments

  • ...
  • sep
  • collapse
> str_c("Letter: ", letters)
 [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e"
 [6] "Letter: f" "Letter: g" "Letter: h" "Letter: i" "Letter: j"
[11] "Letter: k" "Letter: l" "Letter: m" "Letter: n" "Letter: o"
[16] "Letter: p" "Letter: q" "Letter: r" "Letter: s" "Letter: t"
[21] "Letter: u" "Letter: v" "Letter: w" "Letter: x" "Letter: y"
[26] "Letter: z"
> str_c("Letter", letters, sep = ": ")
 [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e"
 [6] "Letter: f" "Letter: g" "Letter: h" "Letter: i" "Letter: j"
[11] "Letter: k" "Letter: l" "Letter: m" "Letter: n" "Letter: o"
[16] "Letter: p" "Letter: q" "Letter: r" "Letter: s" "Letter: t"
[21] "Letter: u" "Letter: v" "Letter: w" "Letter: x" "Letter: y"
[26] "Letter: z"
> str_c(letters, collapse = ", ")
[1] "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z"

str_conv

> (x <- rawToChar(as.raw(177)))
[1] "\xb1"
> str_conv(x, "ISO-8859-2")
[1] "ą"
> str_conv(x, "ISO-8859-1")
[1] "±"

str_detect

文字列がパターンに一致するかを返す

> c("apple", "banana", "pear", "pinapple") %>% {
+   str_detect(., pattern = "^a") %>% print()
+   str_detect(., pattern = "[aeiou]") %>% print()
+ }
[1]  TRUE FALSE FALSE FALSE
[1] TRUE TRUE TRUE TRUE
> # c("apple", "banana", "pear", "pinapple") %>% grepl("^a", .)

str_pad

文字列に空白スペースを挿入する

ref) str_trim

> rbind(
+   str_pad("hadley", 30, "left"),
+   str_pad("hadley", 30, "right"),
+   str_pad("hadley", 30, "both")
+ )
     [,1]                            
[1,] "                        hadley"
[2,] "hadley                        "
[3,] "            hadley            "

str_replace / str_replace_all

文字列の部分・全体置換

> fruits <- c("one apple", "two pears", "three bananas")
> str_replace(string      = fruits, 
+             pattern     = "[aeiou]", 
+             replacement = "-")
[1] "-ne apple"     "tw- pears"     "thr-e bananas"
> str_replace_all(fruits, "[aeiou]", "-")
[1] "-n- -ppl-"     "tw- p--rs"     "thr-- b-n-n-s"

str_trim

文字列中の空白スペースを取り除く

ref) str_pad

> str_trim("  String with trailing and leading white space\t")
[1] "String with trailing and leading white space"

word

文章中の単語を取り出す

> sentences <- c("Jane saw a cat", "Jane sat down")
> word(sentences, 1)
[1] "Jane" "Jane"
> c("こんにちは ヤマダ さん", "こんばんは タナカ さん") %>% word(-2)
[1] "ヤマダ" "タナカ"
> c("11,23,222,3444") %>% gsub(",", " ", .) %>% word(1:4)
[1] "11"   "23"   "222"  "3444"