R,将字符串拆分为字符对

时间:2016-06-13 19:49:16

标签: r

如何以下列方式拆分R中的字符串?请看例子,

example:

c("ex", "xa", "am", "mp", "pl", "le")

2 个答案:

答案 0 :(得分:10)

x = "example"
substring(x, first = 1:(nchar(x) - 1), last = 2:nchar(x))
# [1] "ex" "xa" "am" "mp" "pl" "le"

当然,您可以将其包装成函数,也可以省略非字母(我不知道冒号是否应该是您字符串的一部分)等等。

要对字符串向量执行此操作,可以将其用作lapply的匿名函数:

lapply(month.name, function(x) substring(x, first = 1:(nchar(x) - 1), last = 2:nchar(x)))
# [[1]]
# [1] "Ja" "an" "nu" "ua" "ar" "ry"
# 
# [[2]]
# [1] "Fe" "eb" "br" "ru" "ua" "ar" "ry"
# 
# [[3]]
# [1] "Ma" "ar" "rc" "ch"
# ...

或者将其变为命名函数并按名称使用它。如果你经常使用它,这将是有意义的。

str_split_pairs = function(x) {
    substring(x, first = 1:(nchar(x) - 1), last = 2:nchar(x))
}

lapply(month.name, str_split_pairs)
## same result as above

答案 1 :(得分:0)

这是另一种选择(虽然它比@Gregor的答案慢):

x=c("example", "stackoverflow", "programming")

lapply(x, function(i) {
  i = unlist(strsplit(i,""))
  paste0(i, lead(i))[-length(i)]
})
[[1]]
[1] "ex" "xa" "am" "mp" "pl" "le"

[[2]]
[1] "st" "ta" "ac" "ck" "ko" "ov" "ve" "er" "rf" "fl" "lo" "ow"

[[3]]
[1] "pr" "ro" "og" "gr" "ra" "am" "mm" "mi" "in" "ng"