Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
R data.table/regex-字符第一次和最后一次出现时tstrsplit_R_Regex_Split - Fatal编程技术网

R data.table/regex-字符第一次和最后一次出现时tstrsplit

R data.table/regex-字符第一次和最后一次出现时tstrsplit,r,regex,split,R,Regex,Split,我正在使用data.table的tstrsplit从一列为多个表创建三列。源列是具有2到4个空格的字符向量。我需要把第一个和最后一个空格分开 对于源列有两个空格的表,解决方案不需要正则表达式: tbl=data.table('source.col'=c('enableconomicativevortals'、'architect impressive niches'、'mesh global deliveries')) 我还没有找到一个正则表达式来处理source.col中n个空格大于2的表 &

我正在使用data.table的
tstrsplit
从一列为多个表创建三列。源列是具有2到4个空格的字符向量。我需要把第一个和最后一个空格分开

对于源列有两个空格的表,解决方案不需要正则表达式:

tbl=data.table('source.col'=c('enableconomicativevortals'、'architect impressive niches'、'mesh global deliveries'))

我还没有找到一个正则表达式来处理
source.col
中n个空格大于2的表

> tbl = data.table('source.col'=c('enable synergistic vortals implement', 'architect compelling niches systems', 'mesh global deliverables enable'))
> tbl
                             source.col
1: enable synergistic vortals implement
2:  architect compelling niches systems
3:      mesh global deliverables enable
我有一个可靠的正则表达式用于在,
'(?!.*)
上进行拆分,但我找到的用于在第一个空格,
^[^]+
上进行拆分的选项会为除最后一个新列以外的所有列返回空字符串

我的问题有两个方面,1)如何在第一个空间上拆分,2)如何将用于在第一个空间上拆分的正则表达式与用于在最后一个空间上拆分的正则表达式(可能有|个)组合起来,以获得如下结果:

> tbl
                             source.col    before                base     after
1: enable synergistic vortals implement    enable synergistic vortals implement
2:  architect compelling niches systems architect   compelling niches   systems
3:      mesh global deliverables enable      mesh global deliverables    enable

在使用
sub
创建分隔符后,我们可以使用
fread
(从
base R


stringr::str_match(x,“^(\\S+)\\S+(.*)\\S+(\\S+$”)
interest…这种方法有什么特别的原因吗?“快吗?”康纳姆
fread
速度很快。如果你的数据传输速度慢,你能检查一下时间吗tstrsplit@ConnerM. 我不明白你的意思,你能用不同的数据更新显示issue@ConnerM. 如果空格的数目是可变的,请使用
\\s+
即一个或多个空格
tbl[,c('before','base','after'):=fread(text=sub(“^(\\w+\\s+(.*)”\\s+(\\w+)”,“\\1,\\2,\\3”,source.col),header=FALSE)]
@ConnerM谢谢,你能用一个新的例子更新一下,使它更清晰吗
> tbl
                             source.col    before                base     after
1: enable synergistic vortals implement    enable synergistic vortals implement
2:  architect compelling niches systems architect   compelling niches   systems
3:      mesh global deliverables enable      mesh global deliverables    enable
library(data.table)
tbl[,c('before', 'base', 'after') := fread(text =
      sub("^(\\w+) (.*) (\\w+)$", "\\1,\\2,\\3", 
        source.col), header = FALSE)]
tbl
#                             source.col    before                base     after
#1: enable synergistic vortals implement    enable synergistic vortals implement
#2:  architect compelling niches systems architect   compelling niches   systems
#3:      mesh global deliverables enable      mesh global deliverables    enable