How to convert NUM to INT in R?

Viewed 153953

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).

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.

2 Answers

You can use convert from hablar to change a column of the data frame quickly.

library(tidyverse)
library(hablar)

x <- tibble(var = c(1.34, 4.45, 6.98))

x %>% 
  convert(int(var))

gives you:

# A tibble: 3 x 1
    var
  <int>
1     1
2     4
3     6
Related