Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading