manish p
manish p

Reputation: 187

Add double quotes to character

Is there a way to add double quotes to the character

df <- data.frame(a = c("A","B"), b = c("C","D"))
paste0(df$a,"=",df$b,collapse = ",")
[1] "A=C,B=D"

Can we make if like below

[1] " A="C",B="D" "

Upvotes: 0

Views: 130

Answers (2)

crlwbm
crlwbm

Reputation: 512

glue may be quite useful:

library(tidyverse)
df %>% 
  mutate(str = glue::glue('{a}="{b}"')) %>% 
  pull(str) %>% 
  str_flatten(collapse = ",") %>% 
  cat()

Result:

A="C",B="D"

Upvotes: 0

Martin Gal
Martin Gal

Reputation: 16998

Depending on what exactly are you trying to do, you could use:

> paste0(df$a, '="', df$b, '"', collapse = ",")
[1] "A=\"C\",B=\"D\""

Using cat for printing the output gives you

> cat(paste0(df$a, '="', df$b,'"', collapse = ","))
A="C",B="D"

Upvotes: 1

Related Questions