R改变小数位,没有关闭 [英] R change decimal place without round off

查看:131
本文介绍了R改变小数位,没有关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> signif(1.89,digits=2)
[1] 1.9

我想有1.8

推荐答案

这有点笨拙,但它会工作,保持一切数字:

This is a bit clunky, but it will work and keep everything numeric:

x <- 1.829380

trunc.dec <- function(x,n) {
  floor(x*10^n)/10^n
}

结果:

trunc.dec(x,1)
#[1] 1.8
trunc.dec(x,2)
#[1] 1.82
trunc.dec(x,3)
#[1] 1.829
trunc.dec(x,4)
#[1] 1.8293

这篇关于R改变小数位,没有关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆