Showing posts with label Generating vectors. Show all posts
Showing posts with label Generating vectors. Show all posts

## Generating vectors

With the (:) column operator Regular sequences of numbers can be very handy for all sorts of reasons. Such sequences can be generated in different ways. The easiest way is to use the column operator (:)
> index <- 1:25
> index
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
[23] 23 24 25
> index <- 25:1
> index
[1] 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4
[23]  3  2  1

### The sequence function seq()

The function seq() together with its arguments from, to, by or length is used to generate more general sequences. Specify the beginning and end of the sequence and either specify the length of the sequence or the increment

> m <- seq (from = .7, to = 6,by = 0.5)
> m
[1] 0.7 1.2 1.7 2.2 2.7 3.2 3.7 4.2 4.7 5.2 5.7
> n<-seq(from=.9,to=10,by=.10)
> n
[1]  0.9  1.0  1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2.0  2.1
[14]  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9  3.0  3.1  3.2  3.3  3.4
[27]  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3  4.4  4.5  4.6  4.7
[40]  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9  6.0
[53]  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3
[66]  7.4  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4  8.5  8.6
[79]  8.7  8.8  8.9  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9
[92] 10.0
The following commands have the same result:
> l <-seq(.2,9,length = 14)
> l
[1] 0.2000000 0.8769231 1.5538462 2.2307692 2.9076923 3.5846154
[7] 4.2615385 4.9384615 5.6153846 6.2923077 6.9692308 7.6461538
[13] 8.3230769 9.0000000
> l<-(.2:4/2)
> l
[1] 0.1 0.6 1.1 1.6

### Black-Scholes formula-R

Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...