Domanda

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.

È stato utile?

Soluzione

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top