如何在r中打印带数字的字符串

时间:2014-07-11 09:38:17

标签: r string printing

我有两个向量,我正在计算mean和sd for,我想在每个向量的单独行中打印结果

t1<-c(37.96, 37.67,  32.29, 37.67, 38.52, 38.52, 37.67, 38.24, 36.54, 41.07, 38.81, 33.71, 37.11, 45.89)
t2<-c(51.84, 49.57, 46.45, 50.14, 50.42, 52.4, 49, 49, 49.57, 53.54, 49.57, 46.17, 50.14, 57.22)
mean(t1)
sd(t1)
mean(t2)
sd(t2)

结果应为

  

37.97643±3.127839

     

50.35929±2.801649

2 个答案:

答案 0 :(得分:1)

您可以使用sprintf

sprintf("%f±%f", mean(t1), sd(t1))
# [1] "37.976429±3.127839"

sprintf("%f±%f", mean(t2), sd(t2))
# [1] "50.359286±2.801649"

答案 1 :(得分:1)

您还可以使用cat

cat (mean(t1),"±",sd(t2),"\n")
# [1] 37.97643 ± 2.801649 

cat (mean(t2),"±",sd(t2),"\n")
# [1] 50.35929 ± 2.801649