问题描述
我想将Strings的开始和结束位置添加到df中。我知道如何使用str_locate进行操作,但是如何使用str_locate_all绑定所有预期的匹配?
str_locate的示例:
library(dplyr)
library(stringr)
pattern <- "HelloHiHelloHi"
df <- data.frame(
Text = c("Hello","HelloHi","Hi")
)
df <- df %>% rowwise() %>% mutate(Start = str_locate(pattern,Text)[[1]],End = str_locate(pattern,Text)[[2]])
#result
Text Start End
<chr> <int> <int>
1 Hello 1 5
2 HelloHi 1 7
3 Hi 6 7
我希望得到以下结果:
Text Start End
<chr> <int> <int>
1 Hello 1 5
2 Hello 8 12
3 HelloHi 1 7
4 HelloHi 8 14
5 Hi 6 7
6 Hi 13 14
解决方法
GetAliasesResponse getAliasResponse =
client.indices().getAlias(new GetAliasesRequest("alias"),RequestOptions.DEFAULT);
getAliasResponse.getAliases().keySet(); //key has the indices,value the aliases
,
或带有base R
的选项(已使用的stringr
除外)
out <- do.call(rbind,Map(data.frame,Text = df$Text,str_locate_all(pattern,df$Text)))
row.names(out) <- NULL