Surround every element by single quote and separate by comma

library(tidyverse)
# toy data
df <- tibble(v = c(-3:3))
df
#> # A tibble: 7 x 1
#>       v
#>   <int>
#> 1    -3
#> 2    -2
#> 3    -1
#> 4     0
#> 5     1
#> 6     2
#> 7     3

Given the above data, how can I achieve the following?

#> # A tibble: 1 x 1
#>   wanted                              
#>   <chr>                               
#> 1 '-3', '-2', '-1', '0', '1', '2', '3'

Note that every number is surrounder by '' and separated by a comma.

df %>% mutate(wanted = paste("'",v,"'",sep=""))

@startz Thanks! But I wanted only one row.

Your idea still helps, though. For example, the below code does the job I wanted:

library(tidyverse)
# toy data
df <- tibble(v = c(-3:3))
df %>%
  mutate(wanted = paste("'",v,"'",sep=""),
         wanted = toString(wanted)) %>% 
  select(wanted) %>% 
  slice(1)
#> # A tibble: 1 x 1
#>   wanted                              
#>   <chr>                               
#> 1 '-3', '-2', '-1', '0', '1', '2', '3'

Created on 2022-01-16 by the reprex package (v2.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.