
188
11
章
stringr
による文字列
次のコードでは、2番目と3 番目の単語の順序を置き換えます。
sentences %>%
str_replace("([^ ]+) ([^ ]+) ([^ ]+)", "\\1 \\3 \\2") %>%
head(5)
#> [1] "The canoe birch slid on the smooth planks."
#> [2] "Glue sheet the to the dark blue background."
#> [3] "It's to easy tell the depth of a well."
#> [4] "These a days chicken leg is a rare dish."
#> [5] "Rice often is served in round bowls."
1. 文字列のスラッシュをすべてバックスラッシュで置き換える。
2.
replace_all()
を使って、
str_to_lower()
の単純版を実装しなさい。
3.
words
の先頭と最後の文字を置き換えなさい。できた文字列で
words
にあるのはどれか。
11.4.5
分割
str_split()
を使うと文字列を分割できます。例えば、次のように文を単語に分割できます。
sentences %>%
head(5) %>%
str_split(" ")
#> [[1]]
#> [1] "The" "birch" "canoe" "slid" "on" "the" ...