- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the rate of return for a vector values in R?
To find the rate of return for a vector values, we can use the formula for rate of return. For example, if we have a vector called x then the rate of return can be calculated by using the syntax diff(x)/x[-length(x)]. The output will be in decimal form and if we want to convert it to percentage then the output needs to be multiplied with 100, we can also input the same within the formula as (diff(x)/x[-length(x)])*100.
Example
x1<-rpois(100,80) x1
Output
[1] 59 74 66 82 88 78 66 63 68 70 82 77 73 82 87 86 78 83 [19] 101 75 70 77 71 77 79 68 90 81 85 79 81 99 64 64 74 90 [37] 90 71 70 88 74 91 89 80 96 78 80 74 72 90 101 63 89 77 [55] 67 81 74 79 76 75 82 95 65 74 80 77 73 83 110 92 93 71 [73] 87 84 70 74 101 72 89 68 94 77 71 87 89 87 66 80 67 65 [91] 84 80 92 85 86 73 75 78 78 81
Example
RateOfReturn_x1<-diff(x1)/x1[-length(x1)] RateOfReturn_x1
Output
[1] 0.25423729 -0.10810811 0.24242424 0.07317073 -0.11363636 -0.15384615 [7] -0.04545455 0.07936508 0.02941176 0.17142857 -0.06097561 -0.05194805 [13] 0.12328767 0.06097561 -0.01149425 -0.09302326 0.06410256 0.21686747 [19] -0.25742574 -0.06666667 0.10000000 -0.07792208 0.08450704 0.02597403 [25] -0.13924051 0.32352941 -0.10000000 0.04938272 -0.07058824 0.02531646 [31] 0.22222222 -0.35353535 0.00000000 0.15625000 0.21621622 0.00000000 [37] -0.21111111 -0.01408451 0.25714286 -0.15909091 0.22972973 -0.02197802 [43] -0.10112360 0.20000000 -0.18750000 0.02564103 -0.07500000 -0.02702703 [49] 0.25000000 0.12222222 -0.37623762 0.41269841 -0.13483146 -0.12987013 [55] 0.20895522 -0.08641975 0.06756757 -0.03797468 -0.01315789 0.09333333 [61] 0.15853659 -0.31578947 0.13846154 0.08108108 -0.03750000 -0.05194805 [67] 0.13698630 0.32530120 -0.16363636 0.01086957 -0.23655914 0.22535211 [73] -0.03448276 -0.16666667 0.05714286 0.36486486 -0.28712871 0.23611111 [79] -0.23595506 0.38235294 -0.18085106 -0.07792208 0.22535211 0.02298851 [85] -0.02247191 -0.24137931 0.21212121 -0.16250000 -0.02985075 0.29230769 [91] -0.04761905 0.15000000 -0.07608696 0.01176471 -0.15116279 0.02739726 [97] 0.04000000 0.00000000 0.03846154
Example
x2<-rpois(100,50) x2
Output
[1] 58 53 67 49 49 52 57 41 51 50 44 61 55 54 53 49 53 52 63 42 50 46 60 57 55 [26] 44 47 59 54 35 66 46 48 40 55 52 49 59 59 59 44 58 44 50 55 45 58 48 67 40 [51] 38 40 58 48 49 51 50 54 35 34 52 62 61 58 47 53 49 54 48 47 43 44 60 55 61 [76] 45 62 51 66 53 57 43 45 47 57 48 52 54 42 55 51 43 45 42 49 38 48 46 54 55
Example
RateOfReturn_x2<-diff(x2)/x2[-length(x2)] RateOfReturn_x2
Output
[1] -0.08620690 0.26415094 -0.26865672 0.00000000 0.06122449 0.09615385 [7] -0.28070175 0.24390244 -0.01960784 -0.12000000 0.38636364 -0.09836066 [13] -0.01818182 -0.01851852 -0.07547170 0.08163265 -0.01886792 0.21153846 [19] -0.33333333 0.19047619 -0.08000000 0.30434783 -0.05000000 -0.03508772 [25] -0.20000000 0.06818182 0.25531915 -0.08474576 -0.35185185 0.88571429 [31] -0.30303030 0.04347826 -0.16666667 0.37500000 -0.05454545 -0.05769231 [37] 0.20408163 0.00000000 0.00000000 -0.25423729 0.31818182 -0.24137931 [43] 0.13636364 0.10000000 -0.18181818 0.28888889 -0.17241379 0.39583333 [49] -0.40298507 -0.05000000 0.05263158 0.45000000 -0.17241379 0.02083333 [55] 0.04081633 -0.01960784 0.08000000 -0.35185185 -0.02857143 0.52941176 [61] 0.19230769 -0.01612903 -0.04918033 -0.18965517 0.12765957 -0.07547170 [67] 0.10204082 -0.11111111 -0.02083333 -0.08510638 0.02325581 0.36363636 [73] -0.08333333 0.10909091 -0.26229508 0.37777778 -0.17741935 0.29411765 [79] -0.19696970 0.07547170 -0.24561404 0.04651163 0.04444444 0.21276596 [85] -0.15789474 0.08333333 0.03846154 -0.22222222 0.30952381 -0.07272727 [91] -0.15686275 0.04651163 -0.06666667 0.16666667 -0.22448980 0.26315789 [97] -0.04166667 0.17391304 0.01851852
Example
x3<-sample(1:100,100,replace=TRUE) x3
Output
[1] 68 56 58 33 53 60 75 31 2 39 7 34 12 65 82 79 69 37 [19] 14 88 34 74 85 94 34 13 73 13 9 61 99 45 26 48 87 99 [37] 42 55 37 48 85 16 33 86 22 31 64 13 1 26 34 23 37 40 [55] 51 3 86 55 47 77 50 93 73 43 82 37 11 29 44 34 86 99 [73] 32 62 31 11 25 30 54 8 40 22 85 41 65 61 78 76 49 7 [91] 54 74 36 84 100 68 23 68 60 93
Example
RateOfReturn_x3<-diff(x3)/x3[-length(x3)] RateOfReturn_x3
Output
[1] -0.17647059 0.03571429 -0.43103448 0.60606061 0.13207547 0.25000000 [7] -0.58666667 -0.93548387 18.50000000 -0.82051282 3.85714286 -0.64705882 [13] 4.41666667 0.26153846 -0.03658537 -0.12658228 -0.46376812 -0.62162162 [19] 5.28571429 -0.61363636 1.17647059 0.14864865 0.10588235 -0.63829787 [25] -0.61764706 4.61538462 -0.82191781 -0.30769231 5.77777778 0.62295082 [31] -0.54545455 -0.42222222 0.84615385 0.81250000 0.13793103 -0.57575758 [37] 0.30952381 -0.32727273 0.29729730 0.77083333 -0.81176471 1.06250000 [43] 1.60606061 -0.74418605 0.40909091 1.06451613 -0.79687500 -0.92307692 [49] 25.00000000 0.30769231 -0.32352941 0.60869565 0.08108108 0.27500000 [55] -0.94117647 27.66666667 -0.36046512 -0.14545455 0.63829787 -0.35064935 [61] 0.86000000 -0.21505376 -0.41095890 0.90697674 -0.54878049 -0.70270270 [67] 1.63636364 0.51724138 -0.22727273 1.52941176 0.15116279 -0.67676768 [73] 0.93750000 -0.50000000 -0.64516129 1.27272727 0.20000000 0.80000000 [79] -0.85185185 4.00000000 -0.45000000 2.86363636 -0.51764706 0.58536585 [85] -0.06153846 0.27868852 -0.02564103 -0.35526316 -0.85714286 6.71428571 [91] 0.37037037 -0.51351351 1.33333333 0.19047619 -0.32000000 -0.66176471 [97] 1.95652174 -0.11764706 0.55000000
Example
x4<-sample(1:10,100,replace=TRUE) x4
Output
[1] 3 2 5 10 10 10 3 2 6 3 5 9 8 9 4 2 2 10 7 5 7 5 10 4 4 [26] 6 6 9 8 3 9 5 9 10 5 5 3 2 3 1 8 9 5 10 9 6 10 4 4 8 [51] 6 2 10 10 10 6 4 9 4 9 1 8 3 8 5 1 10 7 4 2 10 9 4 10 3 [76] 2 3 10 1 4 7 3 1 9 10 2 1 8 5 7 10 1 5 3 1 6 9 2 1 4
Example
RateOfReturn_x4<-diff(x4)/x4[-length(x4)] RateOfReturn_x4
Output
[1] -0.3333333 1.5000000 1.0000000 0.0000000 0.0000000 -0.7000000 [7] -0.3333333 2.0000000 -0.5000000 0.6666667 0.8000000 -0.1111111 [13] 0.1250000 -0.5555556 -0.5000000 0.0000000 4.0000000 -0.3000000 [19] -0.2857143 0.4000000 -0.2857143 1.0000000 -0.6000000 0.0000000 [25] 0.5000000 0.0000000 0.5000000 -0.1111111 -0.6250000 2.0000000 [31] -0.4444444 0.8000000 0.1111111 -0.5000000 0.0000000 -0.4000000 [37] -0.3333333 0.5000000 -0.6666667 7.0000000 0.1250000 -0.4444444 [43] 1.0000000 -0.1000000 -0.3333333 0.6666667 -0.6000000 0.0000000 [49] 1.0000000 -0.2500000 -0.6666667 4.0000000 0.0000000 0.0000000 [55] -0.4000000 -0.3333333 1.2500000 -0.5555556 1.2500000 -0.8888889 [61] 7.0000000 -0.6250000 1.6666667 -0.3750000 -0.8000000 9.0000000 [67] -0.3000000 -0.4285714 -0.5000000 4.0000000 -0.1000000 -0.5555556 [73] 1.5000000 -0.7000000 -0.3333333 0.5000000 2.3333333 -0.9000000 [79] 3.0000000 0.7500000 -0.5714286 -0.6666667 8.0000000 0.1111111 [85] -0.8000000 -0.5000000 7.0000000 -0.3750000 0.4000000 0.4285714 [91] -0.9000000 4.0000000 -0.4000000 -0.6666667 5.0000000 0.5000000 [97] -0.7777778 -0.5000000 3.0000000
- Related Articles
- How to return the same index for consecutively duplicated values in an R vector?
- How to find the range for 95% of all values in an R vector?
- How to create a random vector for a range of values in R?
- How to replace values in a vector with values in the same vector in R?
- How to find the number of distinct values in an R vector?
- How to find the number of positive values in an R vector?
- How to find the starting position for consecutive values given the length of consecutive values in an R vector?
- How to find the absolute pairwise difference among values of a vector in R?
- How to find the number of unique values in a vector by excluding missing values in R?
- How to extract the names of vector values from a named vector in R?
- How to determine the percentiles of a vector values in R?
- How to find the frequency of repeated and unique values in a vector in R?
- How to find the position of one or more values in a vector into another vector that contains same values in R?
- How to find the n number of largest values in an R vector?
- How to find the mean of every n values in an R vector?

Advertisements