R语言中如何打印一次语句后输出向量全部元素
R语言实现单次打印语句后输出所有向量元素
你的问题出在sprintf会根据传入向量的长度生成对应数量的字符串,所以才会重复打印语句。下面是几种解决办法:
分开打印语句和向量
最直接的方式就是先输出一次说明语句,再单独打印整个向量:# 打印单次说明 cat("The times of these 20 trades were:\n") # 输出所有时间元素 print(as.POSIXct(timestore))合并向量元素后一次性输出
用toString把向量转成单个逗号分隔的字符串,再和语句拼接,这样sprintf只会生成一条完整内容:time_elements <- toString(as.POSIXct(timestore)) print(sprintf('The times of these 20 trades were: %s', time_elements))如果想要更整齐的换行格式,用
cat控制输出样式:cat("The times of these 20 trades were:\n ", paste(as.POSIXct(timestore), collapse = "\n "))
内容的提问来源于stack exchange,提问作者jacksherry137
相关产品推荐
相关产品推荐

