R:自动在文本前后添加引号
R: automatically adding quotes infront and behind text
我正在使用 R 编程语言。我有一个名为“a”的数据框,其中有一个名为“V1”的列。 “a”中的每个条目都是一个 URL link,格式如下:
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
我试图在每个项目的前后添加双引号(即“”),因此它们看起来像这样:
1 "https:blah-blah-blah.com/item/123/index.do"
2" https:blah-blah-blah.com/item/124/index.do"
3 "https:blah-blah-blah.com/item/125/index.do"
使用之前的 Whosebug post ( ),我尝试了以下代码:
new_file = cat(gsub("\b", '"',a$V1, perl=T))
和
new_file = cat(gsub("(\w+)", '"\1"', a$V1))
但是这些并没有产生预期的结果。有人可以告诉我我做错了什么吗?
谢谢
修正你的方法:
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a$V1 <- gsub("^", "\"", a$V1)
a$V1 <- gsub("$", "\"", a$V1)
a
#> V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"
^字符表示字符串的开头; $表示结束。
@MrFlicks 解决方案也有效
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a$V1 <- dQuote(a$V1, q=FALSE)
a
#> V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"
由 reprex package (v2.0.0)
于 2021-04-11 创建
依我愚见 paste0
应该可以胜任。
- 实际上
"
和 '
做同样的工作,但同时使用时表现不同。看下面两个案例-
案例 1
a <- data.frame(V1 = c('https:blah-blah-blah.com/item/123/index.do',
'https:blah-blah-blah.com/item/124/index.do',
'https:blah-blah-blah.com/item/125/index.do'))
a
V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
a$V1 <- paste0('"', a$V1, '"')
a
V1
1 "https:blah-blah-blah.com/item/123/index.do"
2 "https:blah-blah-blah.com/item/124/index.do"
3 "https:blah-blah-blah.com/item/125/index.do"
案例 2
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a
V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
a$V1 <- paste0("'", a$V1, "'")
a
V1
1 'https:blah-blah-blah.com/item/123/index.do'
2 'https:blah-blah-blah.com/item/124/index.do'
3 'https:blah-blah-blah.com/item/125/index.do'
我正在使用 R 编程语言。我有一个名为“a”的数据框,其中有一个名为“V1”的列。 “a”中的每个条目都是一个 URL link,格式如下:
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
我试图在每个项目的前后添加双引号(即“”),因此它们看起来像这样:
1 "https:blah-blah-blah.com/item/123/index.do"
2" https:blah-blah-blah.com/item/124/index.do"
3 "https:blah-blah-blah.com/item/125/index.do"
使用之前的 Whosebug post (
new_file = cat(gsub("\b", '"',a$V1, perl=T))
和
new_file = cat(gsub("(\w+)", '"\1"', a$V1))
但是这些并没有产生预期的结果。有人可以告诉我我做错了什么吗?
谢谢
修正你的方法:
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a$V1 <- gsub("^", "\"", a$V1)
a$V1 <- gsub("$", "\"", a$V1)
a
#> V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"
^字符表示字符串的开头; $表示结束。
@MrFlicks 解决方案也有效
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a$V1 <- dQuote(a$V1, q=FALSE)
a
#> V1
#> 1 "https:blah-blah-blah.com/item/123/index.do"
#> 2 "https:blah-blah-blah.com/item/124/index.do"
#> 3 "https:blah-blah-blah.com/item/125/index.do"
由 reprex package (v2.0.0)
于 2021-04-11 创建依我愚见 paste0
应该可以胜任。
- 实际上
"
和'
做同样的工作,但同时使用时表现不同。看下面两个案例-
案例 1
a <- data.frame(V1 = c('https:blah-blah-blah.com/item/123/index.do',
'https:blah-blah-blah.com/item/124/index.do',
'https:blah-blah-blah.com/item/125/index.do'))
a
V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
a$V1 <- paste0('"', a$V1, '"')
a
V1
1 "https:blah-blah-blah.com/item/123/index.do"
2 "https:blah-blah-blah.com/item/124/index.do"
3 "https:blah-blah-blah.com/item/125/index.do"
案例 2
a <- data.frame(V1 = c("https:blah-blah-blah.com/item/123/index.do",
"https:blah-blah-blah.com/item/124/index.do",
"https:blah-blah-blah.com/item/125/index.do"))
a
V1
1 https:blah-blah-blah.com/item/123/index.do
2 https:blah-blah-blah.com/item/124/index.do
3 https:blah-blah-blah.com/item/125/index.do
a$V1 <- paste0("'", a$V1, "'")
a
V1
1 'https:blah-blah-blah.com/item/123/index.do'
2 'https:blah-blah-blah.com/item/124/index.do'
3 'https:blah-blah-blah.com/item/125/index.do'