0

I am trying to generate a sequence with a unit smaller than 1, but I could not get the right number for the sequence. A reproducible code looks like below:

> stock=3 # initial value
> inflow=0.4
> outflow=0.25
> unit=1/8
> stock=stock+(inflow-outflow)*stock
> for (i in seq(1,10,by=unit)) stock[i+1]=stock[i]+(inflow-outflow)*stock[i]
> stock
 [1]  3.450000  3.967500  4.562625  5.247019  6.034072  6.939182  7.980060  9.177069 10.553629
[10] 12.136673 13.957174
> length(stock)
[1] 11
> seq(1,10,by=unit)
 [1]  1.000  1.125  1.250  1.375  1.500  1.625  1.750  1.875  2.000  2.125  2.250  2.375  2.500  2.625
[15]  2.750  2.875  3.000  3.125  3.250  3.375  3.500  3.625  3.750  3.875  4.000  4.125  4.250  4.375
[29]  4.500  4.625  4.750  4.875  5.000  5.125  5.250  5.375  5.500  5.625  5.750  5.875  6.000  6.125
[43]  6.250  6.375  6.500  6.625  6.750  6.875  7.000  7.125  7.250  7.375  7.500  7.625  7.750  7.875
[57]  8.000  8.125  8.250  8.375  8.500  8.625  8.750  8.875  9.000  9.125  9.250  9.375  9.500  9.625
[71]  9.750  9.875 10.000
> length(seq(1,10,by=unit))
[1] 73

The expected output of stock should have a length of 73, same as the length of the sequence. The numbers now I have 3.450000 3.967500 4.562625 ... 13.957174 is correct but not complete, there should be 62 more values after the number 13.957174. Any idea? Thank you very much.

6
  • What are you trying to do? Are you trying to generate a sequence between 1 to 10 by 0.125 expecting a specific length? Commented Jul 10, 2020 at 5:21
  • yes, exactly, but from what I got it seems I only got a part of the sequence... Commented Jul 10, 2020 at 5:24
  • You can specify either one of by or length,out. seq(1,10, by = 1/8) or seq(1,10, length.out = 100) Commented Jul 10, 2020 at 5:25
  • This code did not do the job: for (i in seq(1,10,by=1/8)) stock[i+1]=stock[i]+(inflow-outflow)*stock[i]. What do you mean? can you be more specific? Commented Jul 10, 2020 at 5:29
  • Can you show your expected output? Commented Jul 10, 2020 at 5:30

1 Answer 1

1

Perhaps, you are looking for something like this :

stock=3 
inflow=0.4
outflow=0.25
unit=1/8
len <- seq(1,10,by=unit)
for (i in seq_along(len[-1])) stock[i+1]=stock[i]+(inflow-outflow)*stock[i]
stock

# [1]     3.00     3.45     3.97     4.56     5.25     6.03     6.94
# [8]     7.98     9.18    10.55    12.14    13.96    16.05    18.46
#[15]    21.23    24.41    28.07    32.28    37.13    42.70    49.10
#[22]    56.46    64.93    74.67    85.88    98.76   113.57   130.61
#[29]   150.20   172.73   198.64   228.43   262.70   302.10   347.41
#[36]   399.53   459.46   528.37   607.63   698.77   803.59   924.13
#[43]  1062.75  1222.16  1405.49  1616.31  1858.75  2137.57  2458.20
#[50]  2826.93  3250.97  3738.62  4299.41  4944.32  5685.97  6538.87
#[57]  7519.70  8647.65  9944.80 11436.52 13152.00 15124.80 17393.52
#[64] 20002.54 23002.92 26453.36 30421.37 34984.57 40232.26 46267.10
#[71] 53207.16 61188.23 70366.47
Sign up to request clarification or add additional context in comments.

1 Comment

ah, now I see what went wrong. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.