Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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
在字符串中向后计数,直到模式R_R_String_Dplyr_Tidyverse - Fatal编程技术网

在字符串中向后计数,直到模式R

在字符串中向后计数,直到模式R,r,string,dplyr,tidyverse,R,String,Dplyr,Tidyverse,我试图从物品描述中提取UPC。描述的前面有不同数量的/,但UPC总是在最后一个/,因此我使用了字符计数,但是,根据包大小,末尾的字符数是可变的。在复制过程中,您可以在第一行看到结尾处应该是什么样子,但是第二行删除了UPC的第一个数字,并拾取了/。正在寻找一种通过DPLYR内联完成此任务的方法。我的原始代码在复制下 test <- structure(list(Month = structure(c(17987, 17987), class = "Date"),store

我试图从物品描述中提取UPC。描述的前面有不同数量的/,但UPC总是在最后一个/,因此我使用了字符计数,但是,根据包大小,末尾的字符数是可变的。在复制过程中,您可以在第一行看到结尾处应该是什么样子,但是第二行删除了UPC的第一个数字,并拾取了/。正在寻找一种通过DPLYR内联完成此任务的方法。我的原始代码在复制下

test <- structure(list(Month = structure(c(17987, 17987), class = "Date"),store_id = c("7005", "7005"), UPC = c("000004150860081","00001200050404/"), `Item Description` = c("ACQUA PANNA SPRING WATER/EACH/000004150860081/1","AQUAFINA 24PK/24PK/000001200050404/24"), `Cals Item Description` = c(NA_character_,NA_character_), `Sub-Category` = c(NA_character_, NA_character_), Category = c(NA_character_, NA_character_), Department = c(NA_character_,NA_character_), `Sales Dollars` = c(17.43, 131.78), Units = c(7,528), Cost = c(8.4, 112.2), `Gross Margin` = c(9.03, 19.58), `Gross Margin %` = c(0.5181, 0.1486)), row.names = c(NA,-2L), class = c("tbl_df", "tbl", "data.frame")) 
test%
突变(日期=lubridate::mdy(str_sub(textbox43,start=-10)))%>%
突变(store_id=str_sub(textbox6,start=1,end=4))%>%
突变(项目描述=文本框57)%>%
过滤器(!is.na(项目描述),项目描述!=“”)%>%
变异(美元=文本框58,
单位=文本框59,
成本=文本框61,
gm=textbox66,
gm_pct=textbox67)%>%
突变(UPC=str\U子项(项目描述,开始=-17,结束=-3))
这就是你想要的吗

sub("^.*/([^/]+)/[^/]*$",
     "\\1",
     test$`Item Description`)
返回:

[1] "000004150860081" "000001200050404"
编辑:您要求的是
dplyr
style:

test %>%
  mutate(item_id = sub("^.*/([^/]+)/[^/]*$",
                       "\\1",
                       test$`Item Description`))
这是你想要的吗

sub("^.*/([^/]+)/[^/]*$",
     "\\1",
     test$`Item Description`)
返回:

[1] "000004150860081" "000001200050404"
编辑:您要求的是
dplyr
style:

test %>%
  mutate(item_id = sub("^.*/([^/]+)/[^/]*$",
                       "\\1",
                       test$`Item Description`))

请提供其他人可以复制的东西。请提供其他人可以复制的东西。