Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to round a vector consists of numbers,NA, and NaN

Tags:

r

What is the best way to round a vector that consists of numbers, NA, and NaN. I don't want to omit the NA or NaN because I want to retain the order of the numbers.

x = c(2,3,4,NA,"NaN",3, 5)
round(x,2)  #does not work 

Edits: Error in round(x, 2) : Non-numeric argument to mathematical function

like image 257
Amateur Avatar asked Oct 27 '25 10:10

Amateur


1 Answers

x is a character vector because it contains a character "NaN". Converting x to numeric will help.

x = c(2,3,4,NA,"NaN",3, 5)
class(x) # To see the class of x
round(as.numeric(x),2)
like image 157
djhurio Avatar answered Oct 29 '25 01:10

djhurio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!