Reputation: 187
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
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
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