【问题标题】:R: automatically adding quotes infront and behind textR:自动在文本前后添加引号
【发布时间】:2021-07-06 12:18:05
【问题描述】:

我正在使用 R 编程语言。我有一个名为“a”的数据框,其中有一列名为“V1”。 “a”中的每个条目都是一个 URL 链接,格式如下:

 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"

使用之前的 stackoverflow 帖子 (How to add quotes around each word in a string in R?),我尝试了以下代码:

new_file = cat(gsub("\\b", '"',a$V1, perl=T))

new_file = cat(gsub("(\\w+)", '"\\1"', a$V1))

但这些并没有产生预期的结果。有人可以告诉我我做错了什么吗?

谢谢

【问题讨论】:

  • 您的数据似乎已经在向量中,请使用dQuote(a$V1, q=FALSE)。例如:cat(dQuote(letters[1:3], q=FALSE))(注意我使用了cat(),因此 R 在打印到控制台时不会转义引号)。无需理会gsub()。但是,您究竟为什么要尝试将这些用引号括起来呢?它只是一个格式化的东西吗?
  • class(a$V1) 是否返回“字符”?如果是这样,则引用字符串,但并不总是打印引号。在打印时添加引号,例如print(a, quote=TRUE)print(a$V1, quote=TRUE)。当您将值写入文件时,它们也会被引用:write.csv(a)
  • 我不知道你为什么想要这个,但你可以试试a <- a %>% mutate(V1 = paste0('"', V1, '"'))这样的东西。你需要 l 先加载dplyr 包。

标签: r url data-manipulation quotes double-quotes


【解决方案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 <- 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 创建

【讨论】:

    【解决方案2】:

    以我的拙见,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'
    

    【讨论】:

      猜你喜欢
      • 2022-12-12
      • 2018-01-27
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多