How to split string in R in following way ? Look at example, please
如何以下列方式在R中拆分字符串?请看例子
example:
c("ex", "xa", "am", "mp", "pl", "le")
?
c(“ex”,“xa”,“am”,“mp”,“pl”,“le”)?
2 个解决方案
#1
9
x = "example"
substring(x, first = 1:(nchar(x) - 1), last = 2:nchar(x))
# [1] "ex" "xa" "am" "mp" "pl" "le"
You could, of course, wrap it into a function, maybe omit non-letters (I don't know if the colon was supposed to be part of your string or not), etc.
当然,你可以将它包装成一个函数,也可以省略非字母(我不知道冒号是否应该是你的字符串的一部分),等等。
To do this to a vector of strings, you can use it as an anonymous function with lapply
:
要对字符串向量执行此操作,可以将其用作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"
# ...
Or make it into a named function and use it by name. This would make sense if you'll use it somewhat frequently.
或者将其变为命名函数并按名称使用它。如果您经常使用它,这将是有意义的。
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
#2
0
Here's another option (though it's slower than @Gregor's answer):
这是另一种选择(虽然它比@Gregor的回答慢):
x=c("example", "*", "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"
#1
9
x = "example"
substring(x, first = 1:(nchar(x) - 1), last = 2:nchar(x))
# [1] "ex" "xa" "am" "mp" "pl" "le"
You could, of course, wrap it into a function, maybe omit non-letters (I don't know if the colon was supposed to be part of your string or not), etc.
当然,你可以将它包装成一个函数,也可以省略非字母(我不知道冒号是否应该是你的字符串的一部分),等等。
To do this to a vector of strings, you can use it as an anonymous function with lapply
:
要对字符串向量执行此操作,可以将其用作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"
# ...
Or make it into a named function and use it by name. This would make sense if you'll use it somewhat frequently.
或者将其变为命名函数并按名称使用它。如果您经常使用它,这将是有意义的。
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
#2
0
Here's another option (though it's slower than @Gregor's answer):
这是另一种选择(虽然它比@Gregor的回答慢):
x=c("example", "*", "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"