*disclaimer
380916
stringr
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() 指定したパターンが該当した文字列を抽出する
- 正規表現で複数のパターンの文字列が該当する場合、個々に該当したパターンを出力
https://sugiura-ken.org/wiki/