如何从下面的字符串
Vector
中删除“list()”以获得我的desired_output
?
Vector = c("Nc", "list(\"Year\")", "list(\"# of Treatments\")")
gsub("list","", Vector) # Doesn't fully work
desired_output = c("Nc", "Year", "# of Treatments")
根据您的样本数据,您可以使用以下内容来
sub
剔除所有您不想要的东西:
gsub("\"|list\\(|\\)", "", Vector)
# Or since you tagged the question with `stringr`
stringr::str_replace_all(Vector, "\"|list\\(|\\)", "")
两者的输出都是:
# [1] "Nc" "Year" "# of Treatments"
它只是查找引号 (
\"
) 或模式 list(
或右括号 (\\)
)。