Question

I would like to extract the first digit after a decimal place from a numeric vector in R. Is there a way to do this without turning it into a character string? For example:

x <- c(1.0,1.1,1.2)

I would like the function to return a vector:

 0,1,2

thanks.

Was it helpful?

Solution

There'll be a bunch of ways, but here's one:

(x %% 1)*10
# [1] 0 1 2

This assumes there's only ever one digit after the decimal place. If that's not the case:

floor((x %% 1)*10)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top