【问题标题】:How to round to two decimal places after the point [duplicate]如何在点后四舍五入到小数点后两位[重复]
【发布时间】:2022-01-12 00:32:33
【问题描述】:

我想将X2 列的点后四舍五入到小数点后两位,即我的df 数据库,即156.594054487179,而不是156.59

我该怎么做?

df<-structure(list(X1=c(1,2,3),  X2 = c(156.594054487179, 156.589136904762, 156.589136904762)), class= "data.frame", row.names = c(NA, -3L))

【问题讨论】:

  • 如果要修改数据,请使用round 函数,例如df$X2 = round(df$X2, 2)。您可能最好调整链接副本中的打印选项。这将使您的数据保持不变,但不会在您的控制台中打印这么多数字。

标签: r


【解决方案1】:

这是使用round 函数的dplyr 答案。

library(dplyr)
df_new <- df %>%
  mutate(x3 = round(X2, digits = 2))

产生...

  X1       X2     x3
1  1 156.5941 156.59
2  2 156.5891 156.59
3  3 156.5891 156.59

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2013-03-23
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多