在R中打印一个语句,后跟一个向量的每个元素

kkih6yb8  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(77)

有没有人知道如何打印一个语句,并让它后跟一个向量的所有值,而不是多次打印语句,每次都后跟向量中的单个值。
我希望它打印一次语句,然后打印向量的所有元素。这是返回一个例子:

print(sprintf('The times of these 20 trades were %s' , as.POSIXct(timestore)))

The times of these 20 trades were 2023-06-10 19:30:56.377"[2] "The times of these 20 trades were 2023-06-10 19:30:56.377"[3] "The times of these 20 trades were 2023-06-10 19:30:56.377"[4] "The times of these 20 trades were 2023-06-10 19:30:56.377"[5] "The times of these 20 trades were 2023-06-10 19:30:56.377"[6] "The times of these 20 trades were 2023-06-10 19:30:56.377
我希望它打印一次语句,然后打印向量的所有元素。

nszi6y05

nszi6y051#

我们可以用paste(sep = ",")toString()连接这些值:

#REPREX

statement <-"The times of these 20 trades were"

timestore <-c('2023-06-10 19:30:56.377', '2023-06-10 19:30:56.377')

#Solution

paste0(statement, ' ', toString(as.POSIXct(timestore)))

[1] "The times of these 20 trades were 2023-06-10 19:30:56.377, 2023-06-10 19:30:56.377"

我们也可以用数字方式声明交易次数。glue::glue用于动态生成字符串:

glue::glue("The times of these {length(timestore)} trades were {toString(timestore)}")

The times of these 2 trades were 2023-06-10 19:30:56.377, 2023-06-10 19:30:56.377

但是如果我们只想打印几行,一行用于语句,一行用于每个时间戳,那么print(c(statement, timestore))就可以了。

相关问题