Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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_Replace - Fatal编程技术网

R 替换子字符串时出现的问题

R 替换子字符串时出现的问题,r,string,replace,R,String,Replace,让字符串s为 structure(",SELECTCOLUMNS(FILTER(CG_PART,((CG_PART[0NIVEL4] = \"4.03.01.003\")) && CG_PART[isEstorno] = 0 ) ,\"Data\",CG_PART[DATA], \"D/C\", CG_PART[D_C],\"Nivel1\", \"(-) Custo de serviços\", \"Nivel2\", \"Custo de Frete de Serviços

让字符串s

structure(",SELECTCOLUMNS(FILTER(CG_PART,((CG_PART[0NIVEL4] = \"4.03.01.003\"))  && CG_PART[isEstorno] = 0 ) ,\"Data\",CG_PART[DATA], \"D/C\", CG_PART[D_C],\"Nivel1\", \"(-) Custo de serviços\", \"Nivel2\", \"Custo de Frete de Serviços PJ\",\"Nivel3\", [0NIVEL4],\"Valor\", IF(CG_PART[D_C] = \"D\",-CG_PART[VALORR],CG_PART[VALORR]), \"Ordem Apresentacao\", 7, \"isSub\", 0)")
我想替换以下内容:

\"Nivel1\", \"(-) Custo de serviços\", \"Nivel2\", \"Custo de Frete de
Serviços PJ\",\"Nivel3\", [0NIVEL4],\"Valor\"
对于这一点:

\"Nivel1\", \"(=) RECEITA OPERACIONAL LÍQUIDA\",\"Nivel2\", \"(=) RECEITA OPERACIONAL 
LÍQUIDA\", \"Nivel3\",\"(=) RECEITA OPERACIONAL LÍQUIDA\",\"Valor\"
所以我使用了这个代码

init.pos <- str_start(s, "\"Nivel1\"",ignore.case = F)
end.pos <- str_end(s, ",\"Valor\"",ignore.case = F)  

substr(s, init.pos, end.pos) <- "\"Nivel1\",  \"(=) RECEITA OPERACIONAL LÍQUIDA\",\"Nivel2\", \"(=) RECEITA OPERACIONAL LÍQUIDA\", \"Nivel3\",\"(=) RECEITA OPERACIONAL LÍQUIDA\", \"Valor\""
init.pos您可以使用新内容替换“Nivel1”和“Valor”之间的内容

tt <- "\"(=) RECEITA OPERACIONAL LÍQUIDA\",\"Nivel2\", \"(=) RECEITA OPERACIONAL LÍQUIDA\", \"Nivel3\",\"(=) RECEITA OPERACIONAL LÍQUIDA\","

gsub("(.*\"Nivel1\").*(\"Valor\".*)", paste("\\1", tt, "\\2"), s, ignore.case = FALSE)
tt
tt <- "\"(=) RECEITA OPERACIONAL LÍQUIDA\",\"Nivel2\", \"(=) RECEITA OPERACIONAL LÍQUIDA\", \"Nivel3\",\"(=) RECEITA OPERACIONAL LÍQUIDA\","

gsub("(.*\"Nivel1\").*(\"Valor\".*)", paste("\\1", tt, "\\2"), s, ignore.case = FALSE)