I want to generate 100 numbers in the of (0.0001,1000) by equal intervals in R. Can anyone help me with how can I do this?
Example: Generating 7 numbers in the range of (0.001,1000) : 0.001, 0.01, 0.1, 1, 10, 100, 1000,
CodePudding user response:
If I understand you correctly, you want a series of 100 numbers starting at 0.001 and ending at 1000, where the ratio between consecutive numbers is fixed. You can get this with:
x <- 10^seq(log10(0.001), log10(1000), length = 100)
x
#> [1] 1.000000e-03 1.149757e-03 1.321941e-03 1.519911e-03 1.747528e-03
#> [6] 2.009233e-03 2.310130e-03 2.656088e-03 3.053856e-03 3.511192e-03
#> [11] 4.037017e-03 4.641589e-03 5.336699e-03 6.135907e-03 7.054802e-03
#> [16] 8.111308e-03 9.326033e-03 1.072267e-02 1.232847e-02 1.417474e-02
#> [21] 1.629751e-02 1.873817e-02 2.154435e-02 2.477076e-02 2.848036e-02
#> [26] 3.274549e-02 3.764936e-02 4.328761e-02 4.977024e-02 5.722368e-02
#> [31] 6.579332e-02 7.564633e-02 8.697490e-02 1.000000e-01 1.149757e-01
#> [36] 1.321941e-01 1.519911e-01 1.747528e-01 2.009233e-01 2.310130e-01
#> [41] 2.656088e-01 3.053856e-01 3.511192e-01 4.037017e-01 4.641589e-01
#> [46] 5.336699e-01 6.135907e-01 7.054802e-01 8.111308e-01 9.326033e-01
#> [51] 1.072267e 00 1.232847e 00 1.417474e 00 1.629751e 00 1.873817e 00
#> [56] 2.154435e 00 2.477076e 00 2.848036e 00 3.274549e 00 3.764936e 00
#> [61] 4.328761e 00 4.977024e 00 5.722368e 00 6.579332e 00 7.564633e 00
#> [66] 8.697490e 00 1.000000e 01 1.149757e 01 1.321941e 01 1.519911e 01
#> [71] 1.747528e 01 2.009233e 01 2.310130e 01 2.656088e 01 3.053856e 01
#> [76] 3.511192e 01 4.037017e 01 4.641589e 01 5.336699e 01 6.135907e 01
#> [81] 7.054802e 01 8.111308e 01 9.326033e 01 1.072267e 02 1.232847e 02
#> [86] 1.417474e 02 1.629751e 02 1.873817e 02 2.154435e 02 2.477076e 02
#> [91] 2.848036e 02 3.274549e 02 3.764936e 02 4.328761e 02 4.977024e 02
#> [96] 5.722368e 02 6.579332e 02 7.564633e 02 8.697490e 02 1.000000e 03
Or, if you don't like scientific notation,
print(format(round(x, 5), drop0trailing = TRUE), quote = FALSE)
#> [1] 0.001 0.00115 0.00132 0.00152 0.00175 0.00201
#> [7] 0.00231 0.00266 0.00305 0.00351 0.00404 0.00464
#> [13] 0.00534 0.00614 0.00705 0.00811 0.00933 0.01072
#> [19] 0.01233 0.01417 0.0163 0.01874 0.02154 0.02477
#> [25] 0.02848 0.03275 0.03765 0.04329 0.04977 0.05722
#> [31] 0.06579 0.07565 0.08697 0.1 0.11498 0.13219
#> [37] 0.15199 0.17475 0.20092 0.23101 0.26561 0.30539
#> [43] 0.35112 0.4037 0.46416 0.53367 0.61359 0.70548
#> [49] 0.81113 0.9326 1.07227 1.23285 1.41747 1.62975
#> [55] 1.87382 2.15443 2.47708 2.84804 3.27455 3.76494
#> [61] 4.32876 4.97702 5.72237 6.57933 7.56463 8.69749
#> [67] 10 11.49757 13.21941 15.19911 17.47528 20.09233
#> [73] 23.1013 26.56088 30.53856 35.11192 40.37017 46.41589
#> [79] 53.36699 61.35907 70.54802 81.11308 93.26033 107.22672
#> [85] 123.28467 141.74742 162.97508 187.38174 215.44347 247.70764
#> [91] 284.80359 327.45492 376.49358 432.87613 497.70236 572.23677
#> [97] 657.93322 756.46333 869.749 1000
Here, each number is 1.149757 times larger than the preceding number, as we csn see by doing:
sapply(1:99, function(i) x[i 1]/x[i])
#> [1] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [9] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [17] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [25] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [33] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [41] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [49] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [57] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [65] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [73] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [81] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [89] 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757 1.149757
#> [97] 1.149757 1.149757 1.149757
Created on 2022-08-28 with reprex v2.0.2
CodePudding user response:
- We can use the idea of
Geometric Sequence
witha
the first term ,b
the last term andn
the number of terms
geo_seq <- function(a , b , n){
r <- (b/a)^(1/(n-1))
sapply(1:n , \(x) a * r^(x-1))
}
geo_seq(.001 , 1000 , 7)
- Output
[1] 1.000000e-03 1.149757e-03 1.321941e-03 1.519911e-03
[5] 1.747528e-03 2.009233e-03 2.310130e-03 2.656088e-03
[9] 3.053856e-03 3.511192e-03 4.037017e-03 4.641589e-03
[13] 5.336699e-03 6.135907e-03 7.054802e-03 8.111308e-03
[17] 9.326033e-03 1.072267e-02 1.232847e-02 1.417474e-02
[21] 1.629751e-02 1.873817e-02 2.154435e-02 2.477076e-02
[25] 2.848036e-02 3.274549e-02 3.764936e-02 4.328761e-02
[29] 4.977024e-02 5.722368e-02 6.579332e-02 7.564633e-02
[33] 8.697490e-02 1.000000e-01 1.149757e-01 1.321941e-01
[37] 1.519911e-01 1.747528e-01 2.009233e-01 2.310130e-01
[41] 2.656088e-01 3.053856e-01 3.511192e-01 4.037017e-01
[45] 4.641589e-01 5.336699e-01 6.135907e-01 7.054802e-01
[49] 8.111308e-01 9.326033e-01 1.072267e 00 1.232847e 00
[53] 1.417474e 00 1.629751e 00 1.873817e 00 2.154435e 00
[57] 2.477076e 00 2.848036e 00 3.274549e 00 3.764936e 00
[61] 4.328761e 00 4.977024e 00 5.722368e 00 6.579332e 00
[65] 7.564633e 00 8.697490e 00 1.000000e 01 1.149757e 01
[69] 1.321941e 01 1.519911e 01 1.747528e 01 2.009233e 01
[73] 2.310130e 01 2.656088e 01 3.053856e 01 3.511192e 01
[77] 4.037017e 01 4.641589e 01 5.336699e 01 6.135907e 01
[81] 7.054802e 01 8.111308e 01 9.326033e 01 1.072267e 02
[85] 1.232847e 02 1.417474e 02 1.629751e 02 1.873817e 02
[89] 2.154435e 02 2.477076e 02 2.848036e 02 3.274549e 02
[93] 3.764936e 02 4.328761e 02 4.977024e 02 5.722368e 02
[97] 6.579332e 02 7.564633e 02 8.697490e 02 1.000000e 03