Rでコマンド:意外に役に立つ文字列操作に関するコマンド

意外に役に立つ文字列操作に関するコマンドの紹介です。テキストマイニングの役に立てばと思います。重複数を文字の最後に付与する「make.unique」コマンドなどは役に立つと思います。

実行コマンドはR version 4.2.2で確認しています。

実行コマンド

詳細はコメント、コマンドのヘルプを確認してください。

#文字の重複数を表示:make.uniqueコマンド
x <- c("&#12354;", "&#12354;", "&#12356;", "&#12358;", "&#12358;", "&#12358;", "&#12360;", "&#12362;", "&#12362;", "&#12358;")
make.unique(x)
[1] "&#12354;"   "&#12354;.1" "&#12356;"   "&#12358;"   "&#12358;.1" "&#12358;.2" "&#12360;"   "&#12362;"   "&#12362;.1" "&#12358;.3"

#&#25991;&#23383;&#12398;&#37325;&#35079;&#12434;&#38500;&#12367;:unique&#12467;&#12510;&#12531;&#12489;
#&#25968;&#23383;&#12418;&#36969;&#24540;&#21487;&#33021;&#12391;&#12377;
unique(x)
[1] "&#12354;" "&#12356;" "&#12358;" "&#12360;" "&#12362;"

#&#25991;&#23383;&#21015;&#12434;&#25351;&#23450;&#24133;&#12395;&#20307;&#35009;&#12434;&#25972;&#12360;&#12427;:strtrim&#12467;&#12510;&#12531;&#12489;
x <- c("aaaaa", "bbbbb", "ccc")
strtrim(x, c(2, 3, 1))
[1] "aa"  "bbb" "c" 

#old&#12391;&#35373;&#23450;&#12375;&#12383;&#25991;&#23383;&#21015;&#12434;new&#12391;&#25351;&#23450;&#12375;&#12383;&#25991;&#23383;&#21015;&#12391;&#32622;&#25563;:chartr&#12467;&#12510;&#12531;&#12489;
x <- "&#12354;&#12356;&#12358;&#12360;&#12362;"
chartr(old = "&#12356;&#12360;", new = "bc", x)
[1] "&#12354;b&#12358;c&#12362;"

少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました