如何在NUM中将NUM转换为INT? [英] How to convert NUM to INT in R?

查看:289
本文介绍了如何在NUM中将NUM转换为INT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数字格式转换为R中的整数。这对于我使用java代码运行某些模拟(将此特定数据读取为int)的项目的一部分非常重要。

I am trying to convert numeric format to an integer in R. This is essential to a part of the project where I am using java code to run some simulations (which reads this particular data as int).

我试过 round(x $ var,0) trunc(x $ VAR)。它们都成功运行,但当我 str(x)时, x $ var 仍然是 NUM 。 x是一个数据帧。

I tried both round(x$var, 0) and trunc(x$var). Both of them run successfully, but when I str(x), x$var is still num. x is a dataframe.

推荐答案

使用 as.integer

set.seed(1)
x <- runif(5, 0, 100)
x
[1] 26.55087 37.21239 57.28534 90.82078 20.16819


as.integer(x)
[1] 26 37 57 90 20

上课测试:

xx <- as.integer(x)
str(xx)
 int [1:5] 26 37 57 90 20

这篇关于如何在NUM中将NUM转换为INT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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