Search code examples
rstringdouble-quotessingle-quotes

Putting Some Part in Double Quotes in R


I want to put some part of object into double quote like the example given below:

Required Output

"Group 1" = 3, "Group 2" = 3

MWE

Test <- structure("Group 1 = 3, Group 2 = 3", class = "noquote")
Test
[1] Group 1 = 3, Group 2 = 3
as.character(Test)
[1] "Group 1 = 3, Group 2 = 3"

Edited

Actually I have a long character string (here Labs)

Labs  <- c("Group 1", "Group 2")

Test <- noquote(paste(Labs, "= 3", collapse = ", "))
Test
[1] Group 1 = 3, Group 2 = 3

However, I want to have output like this

"Group 1" = 3, "Group 2" = 3

Solution

  • You can use single quotes to let R know where the string begins and ends. That will let you have double quotes inside of it:

    Test <- c('"Group 1" = 3', '"Group 2" = 3')
    

    If you print it, then by default it's going to show you the escape characters. However, you can just cat it, or use some fancier options, depending on your needs.

    cat(Test)
    
    "Group 1" = 3 "Group 2" = 3