どうも、ちょげ(@chogetarou)です。
文字列(string)を1文字ずつに分割する方法を紹介します。
方法

文字列(string)を1文字ずつに分割するには、strsplit()を使います。
まず、strsplit()を呼び出します。
そして、strsplit()の第1引数に文字列、第2引数に空文字を指定します。
#text=対象の文字列
result <- strsplit(text, "")
上記のstrsplit()は、対象の文字列(string)を1文字ずつに分割したリストを返します。
もし、分割した結果をベクトルで取得したい場合は、unlist()を使います。
具体的には、unlist()を呼び出し、その引数に上記のstrsplit()の結果を指定します。
#結果をベクトルで取得
result <- unlist(strsplit(text, ""))
使用例
text <- "ABCDEFGHIJK"
result <- strsplit(text, "")
print(result)
出力:
> print(result)
[[1]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K"
使用例2
text <- "ABCDEFGHIJK"
result <- unlist(strsplit(text, ""))
print(result)
出力:
> print(result)
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K"
コメント