我需要从向量中删除\"
。这是我的数据:
data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1803224&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-linux-security-masterclass-3-in-1%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1848638&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fmastering-kali-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1426684&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Finformation-gathering-with-kali-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1628300&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-switchblade%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1615700&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fadministrador-de-sistemas-junior-en-windows-server-y-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.809770&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flearn-bash-shell-in-linux-for-beginners-lite%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.574388&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fhow-to-install-linux-ubuntu-server%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1436610&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fcentos-and-ubuntu-managing-packages%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1771266&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-foundation-certified-system-administrator-exam%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1734052&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-server-security%2F"
)
如您所见,每个对象都以\"
开头。如何专门删除这些字符并保留链接?
答案 0 :(得分:9)
您可以尝试一下。请注意,您真正想要的是删除npm install
,而不是\"
(这是在问题的未编辑版本中建议的)。您需要代表字符中的每个元素的第一个"\
。
"
答案 1 :(得分:6)
或者我们可以仅在模式上使用'"'
gsub('"', "", data)
答案 2 :(得分:6)
如果它始终是第一个字符,则只需使用 substring :
substring(data, 2)
这应该比任何 regex 解决方案都要快。
data <- rep(data, 1000)
microbenchmark::microbenchmark(
a = substring(data, 2),
b = gsub("\"", "", data, fixed = TRUE),
c = gsub('"', "", data),
d = gsub('[\"]', '', data),
e = stringr::str_replace(data, '[\"]', ''),
f = gsub("^.","",data)
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# a 2.835013 2.849838 2.933796 2.857393 2.900301 4.446956 100
# b 4.728632 4.739751 4.788882 4.754861 4.795203 5.200185 100
# c 7.388025 7.413684 7.503427 7.458444 7.555520 8.160925 100
# d 7.390876 7.412686 7.530044 7.454453 7.533568 8.535544 100
# e 12.019154 12.205608 12.430870 12.316084 12.581081 13.917336 100
# f 15.712882 15.735975 15.875353 15.770043 15.861275 18.906262 100
答案 3 :(得分:2)
使用fixed = TRUE
将模式作为字符串进行匹配:
gsub("\"", "", data, fixed = TRUE)
答案 4 :(得分:2)
@milan更快:)
使用 stringr 的方法是
library(stringr)
str_replace(data, '[\"]', '')
答案 5 :(得分:2)
这同样有效:
gsub("\"", "", data)
答案 6 :(得分:1)
您也可以删除第一个字符,跳过反斜杠头痛:
gsub("^.","",data)
答案 7 :(得分:0)
我结合使用gsub()和noquote()
for (i in data){
print(gsub('"','',(noquote(i))))
}