Extract a digit from a number in r

I would like to extract the first digit after the decimal point from a number 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 want the function to return a vector:

 0,1,2

thanks.

+4
source share
1 answer

There will be many ways, but here is one of them:

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

This means that there is only one digit after the decimal point. If this is not the case:

floor((x %% 1)*10)
+11
source

Source: https://habr.com/ru/post/1527615/


All Articles