문제

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.

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top