Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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

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
stringr str_locate_all未在dplyr字符串中返回正确的索引_R_Dplyr_Stringr - Fatal编程技术网

stringr str_locate_all未在dplyr字符串中返回正确的索引

stringr str_locate_all未在dplyr字符串中返回正确的索引,r,dplyr,stringr,R,Dplyr,Stringr,我试图使用str_locate_all来查找dplyr链中第三次出现的“/”的索引,但它没有返回正确的索引 ga.categoryViews.2016 <- ga.data %>% mutate(province = str_sub(pagePath,2,3), index = str_locate_all(pagePath, '/')[[1]][,"start"][3], category = str_sub(pagePath

我试图使用str_locate_all来查找dplyr链中第三次出现的“/”的索引,但它没有返回正确的索引

  ga.categoryViews.2016 <- ga.data %>%
    mutate(province = str_sub(pagePath,2,3),
           index = str_locate_all(pagePath, '/')[[1]][,"start"][3],
           category = str_sub(pagePath, 
                              str_locate_all(pagePath, '/')[[1]][,"start"][3] + 1,
                              ifelse(str_detect(pagePath,'\\?'), str_locate(pagePath, '\\?') - 1, str_length(pagePath))
                              )
             )
ga.categoryview.2016%
突变(省=str_sub(pagePath,2,3),
index=str_locate_all(页面路径“/”)[[1][,“开始”][3],
类别=str_sub(页面路径,
str_locate_all(pagePath,“/”)[[1][,“start”][3]+1,
ifelse(str\u detect(pagePath,\\?)、str\u locate(pagePath,\\?)-1、str\u length(pagePath))
)
)
它返回的一个例子是

第一列是pagePath,第四列是索引

它似乎总是返回12的索引

感谢您的帮助


谢谢,

您需要使用
rowwise()
,即

library(dplyr)
library(stringr)

df %>% 
 rowwise() %>% 
 mutate(new = str_locate_all(v1, '/')[[1]][,2][3])

Source: local data frame [2 x 2]
Groups: <by row>

# A tibble: 2 x 2
#                              v1   new
#                           <chr> <int>
#1 /on/srgsfsfs-gfdgdg/dfgsdfg-df    20
#2        /on/sgsddg-dfgsd/dfg-dg    17
库(dplyr)
图书馆(stringr)
df%>%
行()
突变(new=str_locate_all(v1,“/”)[[1]][2][3])
来源:本地数据帧[2 x 2]
组:
#一个tibble:2x2
#v1新
#                            
#1/on/srgsfsfs gfdgdg/dfgsdfg df 20
#2/on/sgsddg dfgsd/dfg dg 17
数据

x <- c('/on/srgsfsfs-gfdgdg/dfgsdfg-df', '/on/sgsddg-dfgsd/dfg-dg')
df <- data.frame(v1 = x, stringsAsFactors = F)

df
#                              v1
#1 /on/srgsfsfs-gfdgdg/dfgsdfg-df
#2        /on/sgsddg-dfgsd/dfg-dg
x