15

R中一个非常基本的问题,但解决方案尚不清楚。

如何将字符向量拆分为其单个字符,即paste(..., sep='')or的反面stringr::str_c()

任何比这更笨重的东西:

sapply(1:26, function(i) { substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,i) } )
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

是否可以以其他方式完成,例如使用strsplit()stringr::*其他方式?

4

3 回答 3

25

是的,strsplit会做的。 strsplit返回一个列表,因此您可以使用unlist将字符串强制转换为单个字符向量,或使用列表索引[[1]]来访问第一个元素。

x <- paste(LETTERS, collapse = "")

unlist(strsplit(x, split = ""))
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
#[20] "T" "U" "V" "W" "X" "Y" "Z"

或(注意实际上没有必要命名split参数)

strsplit(x, "")[[1]]
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
#[20] "T" "U" "V" "W" "X" "Y" "Z"

您也可以拆分NULL或拆分character(0)相同的结果。

于 2014-04-12T10:03:27.303 回答
5

str_extract_all()fromstringr提供了一种执行此操作的好方法:

str_extract_all("ABCDEFGHIJKLMNOPQRSTUVWXYZ", boundary("character"))

[[1]]
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U"
[22] "V" "W" "X" "Y" "Z"
于 2020-02-07T13:29:11.393 回答
0

为了清楚起见,这是逐步呈现的;在实践中,将创建一个函数。

查找任何字符按顺序重复的次数

the_string <- "BaaaaaaH"
# split string into characters
the_runs <- strsplit(the_string, "")[[1]]
# find runs
result <- rle(the_runs)
# find values that are repeated
result$values[which(result$lengths > 1)]
#> [1] "a"
# retest with more runs
the_string <- "BaabbccH"
# split string into characters
the_runs <- strsplit(the_string, "")[[1]]
# find runs
result <- rle(the_runs)
# find values that are repeated
result$values[which(result$lengths > 1)]
#> [1] "a" "b" "c"
于 2021-09-26T00:55:37.057 回答