如何从 R 中的字符串向量中删除术语“list()”?

问题描述 投票:0回答:1

如何从下面的字符串

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")
r stringr gsub
1个回答
0
投票

根据您的样本数据,您可以使用以下内容来

sub
剔除所有您不想要的东西:

gsub("\"|list\\(|\\)", "", Vector)

# Or since you tagged the question with `stringr`
stringr::str_replace_all(Vector, "\"|list\\(|\\)", "")

两者的输出都是:

# [1] "Nc"  "Year"  "# of Treatments"

它只是查找引号 (

\"
) 或模式
list(
或右括号 (
\\)
)。

© www.soinside.com 2019 - 2024. All rights reserved.