Code
library(tidyverse)
options(scipen = 999)
set.seed(12)
July 28, 2024
I wanted to create categories of thousands and needed to put numbers into intervals of one, ten, and hundred thousands as well as millions.
Here is the solution I came up with using \(10^{\lfloor \log_{10}(x) \rfloor}\)
seed | x | log_x | floor_log_x | category |
---|---|---|---|---|
3.4 | 3400 | 3.5 | 3 | 1000 |
4.5 | 4500 | 3.7 | 3 | 1000 |
8.4 | 8400 | 3.9 | 3 | 1000 |
1.6 | 16000 | 4.2 | 4 | 10000 |
2.5 | 25000 | 4.4 | 4 | 10000 |
2.6 | 26000 | 4.4 | 4 | 10000 |
6.8 | 68000 | 4.8 | 4 | 10000 |
9.5 | 95000 | 5.0 | 5 | 10000 |
1.2 | 120000 | 5.1 | 5 | 100000 |
1.1 | 1100000 | 6.0 | 6 | 1000000 |
1.3 | 1300000 | 6.1 | 6 | 1000000 |
8.3 | 8300000 | 6.9 | 6 | 1000000 |