トップ 履歴 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

stringr

*disclaimer
639200

[R]
[R.package]

stringr



正規表現のオプション

  • ignore_case=T
  • multiline=T

str_starts() 文字列の検索

sp.dat.long.score %>% dplyr::filter(str_starts(name, "Mostafa"))
  • longフォーマットのデータのうち、見出しnameのところで、
  • "Mostafa"で始まる文字列からなる項目を含む行のみを選び出す。


str_c() 文字列の結合

  • 例:ID = str_c(Lang,Year,PID,SID, sep="_"
    • Lang, Year, PID, SIDをアンダースコアでつないで、新しくIDという文字列にする

SbyS.dat %>% mutate(ID = str_c(Lang,Year,PID,SID,  sep="_")) %>% head()

Lang Year PID SID sentence SL MDD Omega MHD ID
CN 2 2001 1 大学に入ってもう一年が経ちました 5 1.50 0.5000000 1.50 0.5000000 1.50 CN_2_2001_1


str_which() 文字列がある行番号を調べる

str_which(カラム名,"文字列")
> head(df1)
    A B  C
1 AAA 2 20
2 BBB 3 30
3 AAA   40
4 BBB 4 30
5 CCC 5 60
> str_which(df1$A, "AAA")
[1] 1 3


str_detect() 該当する文字列があるかどうか調べる

str_detect(データ, "正規表現")
  • subset() と合わせて使うと便利
    • データフレーム中の特定の列に「ある種の文字列」があるかどうかを調べて、その文字列を含む行だけを選び出す。
      • 「ある種の文字列」の例:小文字の連続で書かれている「単語」が複数あるもの
fragJBnozeroMW <- subset(fragJBnozero, str_detect(fragJBnozero[,3], "[:lower:]+ +[^a-z]*[:lower:]+"), select=c("total","MW"))

> fragJBnozeroMW
     total                                MW
251    256                    a [NN] of [NP]
278    253                       do n't [VP]
285    252                  the [NN] of [NP]
291    219                       do not [VP]
306    235                      want to [VP]
325    240                             a lot
330    213                       for example
341    210                     a lot of [NP]


str_extract() 指定したパターンが該当した文字列を抽出する

  • 正規表現で複数のパターンの文字列が該当する場合、個々に該当したパターンを出力

str_replace(文字列, 置換対象表現, 置換後表現)


str_remove(文字列, 削除表現)

  • 上の置換で、何もなしで置換と同等

str_remove_all(文字列, 削除表現)

  • 文字列中に出現しているすべての該当パタンを削除
    • _allなしだと、最初のものだけしか処理しない

str_count(文字列, '\\w+') 単語数のカウント

  • 一文字以上の英字の連続(\w+)の数を数える

https://www.statology.org/word-count-in-r/


参考サイト
https://heavywatal.github.io/rstats/stringr.html