Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glue vector to string

Tags:

r

r-glue

I want to produce this string: All my variables: a, b, c from this variable vars <- c("a", "b", "c") using glue().

My best attempt so far is:

library(glue)
glue('All my variables: {paste(vars, collapse = ", ")}')

Question:

Is there any easier / cleaner way of to implement it that i oversee?

Other attempts:

The following obviously fail, i just want to show that i looked into the docu and made some effort :).

glue('All my variables: {vars}')
glue_data('All my variables: {vars}', .sep = ", ")
like image 861
Tlatwork Avatar asked Mar 19 '26 14:03

Tlatwork


2 Answers

you can also use glue::glue_collapse() :

vars <- c("a", "b", "c")
glue("All my variables : {glue_collapse(vars,  sep = ', ')}")
#> All my variables : a, b, c
like image 67
Moody_Mudskipper Avatar answered Mar 22 '26 04:03

Moody_Mudskipper


You can just do,

paste('All my variables:', toString(vars))
#[1] "All my variables: a, b, c"
like image 27
Sotos Avatar answered Mar 22 '26 02:03

Sotos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!