【问题标题】:zeros in c() after round()round() 后 c() 中的零
【发布时间】:2017-12-07 09:44:53
【问题描述】:

我正在尝试使用以下内容打印矢量:

total_nb_visits <- 20302079
total_nb_visits_y1 <- 19299803
total_nb_visitors <- 17707555
total_nb_visitors_y1 <- 17196674
CVR <- 0.02274954
CVR_y1 <- 0.02293334

当我这样做时:

exportable <- rbind(c(total_nb_visits,total_nb_visits_y1,((total_nb_visits-total_nb_visits_y1)/total_nb_visits_y1)*100),
                    c(total_nb_visitors,total_nb_visitors_y1,((total_nb_visitors-total_nb_visitors_y1)/total_nb_visitors_y1)*100),
                    c(CVR,CVR_y1,((CVR-CVR_y1)/CVR_y1)))

我得到:

exportable
                  [,1]              [,2]         [,3]
[1,] 20302079.00000000 19299803.00000000  5.193192905
[2,] 17707555.00000000 17196674.00000000  2.970812844
[3,]        0.02274954        0.02293334 -0.008014533

我想得到:

                  [,1]              [,2]         [,3]
[1,]          20302079          19299803  5.193192905
[2,]          17707555          17196674  2.970812844
[3,]        0.02274954        0.02293334 -0.008014533

round() 不工作...

我要补充一点,我也一直在玩options(scipen=999)删除科学记数法...

谁能帮帮我?

谢谢!

【问题讨论】:

  • 您不能在矩阵中存储不同的类型,您想要的输出显示整数,同时仍包含小数。例如,在表格中的演示可能会有所不同,但对象的存储将保留为您的结果。
  • 谢谢@KevinArseneau。我能做些什么来打印显示的表格?如果不是将数据存储在矩阵中,而是使用另一种类型的对象,它可以工作吗?

标签: r rounding scientific-notation


【解决方案1】:

你可以像这样使用 prettynum:

exportable <- as.data.table(
  rbind(
    c(prettyNum(total_nb_visits), 
      total_nb_visits_y1,  round(((total_nb_visits - total_nb_visits_y1) / total_nb_visits_y1) * 100, 9)), 
    c(total_nb_visitors, 
      total_nb_visitors_y1, 
      round(((total_nb_visitors - total_nb_visitors_y1) / total_nb_visitors_y1) * 100, 9)), 
    c(CVR,CVR_y1, round((CVR-CVR_y1) / CVR_y1, 9))))

唯一的缺点是它会将值转换为字符,但你会得到漂亮的输出!

【讨论】:

  • 所以不可能将它们保持为数字?我必须将它们转换成字符?
猜你喜欢
  • 2014-09-15
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多