Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在R中将字符串拆分为100个单词部分_String_R_Split - Fatal编程技术网

String 在R中将字符串拆分为100个单词部分

String 在R中将字符串拆分为100个单词部分,string,r,split,String,R,Split,我如何将一个巨大的“字符”拆分成更小的字符,每个字符正好包含100个单词。 例如,我过去就是这样用一个词来划分的 myCharSplitByWords <- strsplit(myCharUnSplit, " ")[[1]] myCharSplitByWords也许有一种使用正则表达式的方法,但是在strsplit之后,用“手”将单词分组会更容易: ##示例数据 种子(1) string您可以通过以下方式获得每100个空格前的非空格实例(如果这是您对单词的定义): ind您也可以进行gr

我如何将一个巨大的“字符”拆分成更小的字符,每个字符正好包含100个单词。 例如,我过去就是这样用一个词来划分的

myCharSplitByWords <- strsplit(myCharUnSplit, " ")[[1]]

myCharSplitByWords也许有一种使用正则表达式的方法,但是在
strsplit
之后,用“手”将单词分组会更容易:

##示例数据
种子(1)

string您可以通过以下方式获得每100个空格前的非空格实例(如果这是您对单词的定义):

ind您也可以进行
group
## example data
set.seed(1)
string <- paste0(sample(c(LETTERS[1:10], " "), 1e5, replace=TRUE), collapse="")

## split if there is at least one space
words <- strsplit(string, "\\s+")[[1]]

## build group index
group <- rep(seq(ceiling(length(words)/100)), each=100)[1:length(words)]

## split by group index
words100 <- split(words, group)
ind<-  gregexpr("([^ ]+? +){100}", string)[[1]]
hundredWords <- substr(string, ind, c(ind[-1]-1, nchar(string))