Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

Is there a way to multiply with seq in R?

I want to create a vector that starts at a predefined value and the next value is the previous times another predefined value.

dcdefmax <- 16500 
dcdefgrowth <- 1.025
dcdefvector <- seq(from = 16500, length.out = 50, by = dcdefgrowth)

The resulting vector will be 16,500 16,500 + 1.025 16,500 + 2 * 1.025 ...

but I want 16,500 16,500 * 1.025 16,500 * 1.025^2.

If there's another way to do this, that's fine. I just figured seq was the obvious choice, but I can't think of a way of doing this without a for loop.

question from:https://stackoverflow.com/questions/66067509/is-there-a-way-to-multiply-with-seq-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

dcdefgrowth^(0:50) will create the sequence to multiply by:

dcdefvector = dcdefmax * dcdefgrowth^(0:50)

dcdefvector
 [1] 16500.00 16912.50 17335.31 17768.70 18212.91 18668.24 19134.94 19613.31 20103.65 20606.24 21121.39
[12] 21649.43 22190.67 22745.43 23314.07 23896.92 24494.34 25106.70 25734.37 26377.73 27037.17 27713.10
[23] 28405.93 29116.08 29843.98 30590.08 31354.83 32138.70 32942.17 33765.72 34609.87 35475.11 36361.99
[34] 37271.04 38202.82 39157.89 40136.83 41140.25 42168.76 43222.98 44303.55 45411.14 46546.42 47710.08
[45] 48902.83 50125.40 51378.54 52663.00 53979.58 55329.07 56712.29

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...