我有这样的字符串:
string <- "1, 2, \"something, else\""
Run Code Online (Sandbox Code Playgroud)
我想使用tidyr::separate_rows()with sep==",",但字符串引用部分内的逗号让我困惑。我想删除某些内容和其他内容之间的逗号(但只有这个逗号)。
这是一个更复杂的玩具示例:
string <- c("1, 2, \"something, else\"", "3, 5, \"more, more, more\"", "6, \"commas, are fun\", \"no, they are not\"")
string
#[1] "1, 2, \"something, else\""
#[2] "3, 5, \"more, more, more\""
#[3] "6, \"commas, are fun\", \"no, they are not\""
Run Code Online (Sandbox Code Playgroud)
我想去掉嵌入引号内的所有逗号。期望的输出:
[1] "1, 2, \"something else\""
[2] "3, 5, \"more more more\""
[3] "6, \"commas are fun\", \"no they are not\""
Run Code Online (Sandbox Code Playgroud)
您可以定义一个小函数来进行替换。
library(stringr)
rmcom <- function(x) gsub(",", "", x)
str_replace_all(string, "(\"[[:alnum:]]+,[ [:alnum:],]*\")", rmcom)
[1] "1, 2, \"something else\""
[2] "3, 5, \"more more more\""
[3] "6, \"commas are fun\", \"no they are not\""
Run Code Online (Sandbox Code Playgroud)