gpt4 book ai didi

r - 使用可变偏移量分割字符串

转载 作者:行者123 更新时间:2023-12-01 22:34:06 28 4
gpt4 key购买 nike

我想从字符串中获取大小为 n 和偏移量 o 的所有子字符串。

例如,给定 "abcdef"和大小= 3,偏移量= 1的子串。我想获得一个带有 "abc","bcd", "cde", "def" 的集合。

在 Mathematica 中,这可以通过 Partition 来完成或StringPartition

R中有类似的函数吗?

最佳答案

你可以只写一个包装器(感谢@thelatemail):

subs = function(x, size, offset){
nc = nchar(x)
first = seq(1, nc-size+1L, by=offset)
last = first + size -1L
substring(x, first, last)
}


subs("abcde",3, 1)
[1] "abc" "bcd" "cde"

关于r - 使用可变偏移量分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426149/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com