Home > database >  create a new column with variable but numbers are decimal instead of integer
create a new column with variable but numbers are decimal instead of integer

Time:08-02

I would like to create a new dataset

I gave that code

ch$n_new <- ch$new_ratio*ch$Population

Now I have numbers for ch$n_new like 2.5667 or 2.1900 But I want the number 3 for 2.5667 and 2 for 2.19 no decimal just integer. How can I fix that?

CodePudding user response:

You didnt specify how you wanted to choose the integer - if you want to round to the nearest integer, use round(), if you want the lower integer value (floor) use floor() and if you want the higher integer value (ceiling) use ceiling()

x <- 3.1828

round(x)
floor(x)
ceiling(x)

Output

# > round(x)
# [1] 3
# > floor(x)
# [1] 3
# > ceiling(x)
# [1] 4

For your example, it seems round() would be meet your desired output:

y <- c(2.5667, 2.1900)

round(y)
#> round(x)
#[1] 3 2

  •  Tags:  
  • r
  • Related