Reputation: 1277
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
Upvotes: 4
Views: 6137
Reputation: 5536
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)
Upvotes: 8
Reputation: 3239
There should not be quotes around NaN. "NaN" is a character string; NaN is numeric. If this happens outside your toy example it suggests there is a problem elsewhere in your code.
x = c(2,3,4,NA,NaN,3, 5)
round(x,2)
# [1] 2 3 4 NA NaN 3 5
Upvotes: 2