I wish to print the following text,
x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
with the contents of the set {a,...,j} being an integer vector.
x = 1:10
print(c("x = {", x, "}"), quote=FALSE)
#[1] x = { 1 2 3 4 5 6 7 8 9 10 }
x = 1:10
noquote(paste(c("x = {",x,"}"),sep=","))
#[1] x = { 1 2 3 4 5 6 7 8 9 10 }
Both of these have the same output and the same two issues-- too many spaces and no commas between vector entries. I'm aware this is a very beginner question but any tips?
print
is not designed to paste element together, use paste
instead:
paste0("x = {", paste(x, collapse = ", "), "}")
#[1] "x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"
A tidyverse
alternative:
library(stringr)
library(glue)
glue("x = {{{str_flatten_comma(x)}}}")
#[1] "x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.