Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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
stringr::str_locate返回给定模式的第一次出现_R_String_Stringr - Fatal编程技术网

stringr::str_locate返回给定模式的第一次出现

stringr::str_locate返回给定模式的第一次出现,r,string,stringr,R,String,Stringr,虽然我想在字符串中查找子字符串的位置,但我有以下代码和结果。我认为返回的值应该是3和1,但两条记录我都得到了3。我怎样才能得到正确的结果?我将R3.5.3与Stringr1.4.0一起使用 t1 <- tibble(x = c("aaded", "dedere")) t1 # A tibble: 2 x 1 x <chr> 1 aaded 2 dedere bb <- t1 %>% mutate(str

虽然我想在字符串中查找子字符串的位置,但我有以下代码和结果。我认为返回的值应该是3和1,但两条记录我都得到了3。我怎样才能得到正确的结果?我将R3.5.3与Stringr1.4.0一起使用

t1 <- tibble(x = c("aaded", "dedere"))
t1
# A tibble: 2 x 1
  x     
  <chr> 
1 aaded 
2 dedere

bb <- t1 %>% mutate(str_locate(x, "de")[1])
bb
# A tibble: 2 x 2
  x      `str_locate(x, "de")[1]`
  <chr>                     <int>
1 aaded                         3
2 dedere                        3

t1您的索引不正确

您仅对第1个值进行了子集设置,该值将在其余列中循环使用

library(dplyr)
library(stringr)

t1 %>% mutate(start = str_locate(x, "de")[, 1])

#  x      start
#  <chr>  <int>
#1 aaded      3
#2 dedere     1
库(dplyr)
图书馆(stringr)
t1%>%突变(开始=str_locate(x,“de”)[,1])
#x起点
#    
#1 AAD 3
#第二天

您的索引不正确。尝试
t1%>%mutate(start=str_locate(x,“de”)[,1])
@linuxiaobai很高兴能帮上忙!点击投票按钮旁边的复选标记,您可以自由选择最适合您的选项:-)每篇文章只能接受一个答案。