Remove rows under condition in R - r

I have this dataframe
time power hr fr VE VO2 VCO2 id
1 1462.0104166666667 25 90 24 20 632 549 LM01-PRD-S1
2 1462.0194444444444 25 92 23 21 679 597 LM01-PRD-S1
3 1462.0305555555556 25 93 22 21 675 607 LM01-PRD-S1
4 1462.0416666666667 25 93 20 19 680 577 LM01-PRD-S1
5 1462.0520833333333 40 96 20 22 745 660 LM01-PRD-S1
6 1462.0618055555556 40 98 21 22 764 675 LM01-PRD-S1
7 1462.0722222222223 40 100 21 22 789 703 LM01-PRD-S1
8 1462.0826388888888 40 100 20 23 805 734 LM01-PRD-S1
9 1462.09375 55 105 22 26 911 843 LM01-PRD-S1
10 1462.1041666666667 55 105 20 25 881 831 LM01-PRD-S1
11 1462.1131944444444 55 109 19 25 895 847 LM01-PRD-S1
12 1462.1229166666667 55 112 21 25 908 868 LM01-PRD-S1
13 1462.1347222222223 70 120 21 28 981 947 LM01-PRD-S1
14 1462.1451388888888 70 120 21 29 1044 1021 LM01-PRD-S1
15 1462.1548611111111 70 122 22 27 1066 1031 LM01-PRD-S1
16 1462.1652777777779 70 127 19 30 1136 1122 LM01-PRD-S1
17 1462.1770833333333 85 130 20 32 1181 1218 LM01-PRD-S1
18 1462.1868055555556 85 141 21 32 1194 1216 LM01-PRD-S1
19 1462.1958333333334 85 139 22 34 1231 1295 LM01-PRD-S1
20 1462.2069444444444 85 139 19 32 1193 1268 LM01-PRD-S1
21 1462.2166666666667 100 139 21 31 1192 1274 LM01-PRD-S1
22 1462.2291666666667 100 146 21 38 1363 1460 LM01-PRD-S1
23 1462.2395833333333 100 150 28 50 1551 1801 LM01-PRD-S1
24 1462.2479166666667 100 148 30 51 1499 1810 LM01-PRD-S1
25 1462.2597222222223 115 150 30 55 1564 1883 LM01-PRD-S1
26 1462.2708333333333 115 153 31 56 1544 1892 LM01-PRD-S1
27 1462.2805555555556 115 157 33 59 1545 2012 LM01-PRD-S1
28 1462.2881944444443 115 157 34 62 1647 2091 LM01-PRD-S1
29 NA NA NA RÈcupÈ ration NA NA LM01-PRD-S1
30 1462.0027777777777 65 157 39 61 1466 1940 LM01-PRD-S1
31 1462.0131944444445 20 153 32 58 1518 1939 LM01-PRD-S1
32 1462.0236111111112 20 148 28 50 1422 1748 LM01-PRD-S1
33 1462.0333333333333 20 144 26 46 1222 1555 LM01-PRD-S1
34 1462.0430555555556 20 141 22 37 963 1209 LM01-PRD-S1
35 1462.0541666666666 20 133 22 42 1165 1464 LM01-PRD-S1
36 1462.0645833333333 20 133 24 47 1021 1384 LM01-PRD-S1
37 1462.0743055555556 20 130 22 40 914 1228 LM01-PRD-S1
38 1462.0854166666666 20 130 23 38 847 1128 LM01-PRD-S1
39 1462.0944444444444 20 120 18 32 755 998 LM01-PRD-S1
40 1462.1069444444445 0 117 17 29 674 904 LM01-PRD-S1
41 1462.1173611111112 0 115 20 27 587 805 LM01-PRD-S1
42 1462.1277777777777 0 113 20 28 536 803 LM01-PRD-S1
43 1462.1368055555556 0 112 18 26 489 744 LM01-PRD-S1
44 1462.1479166666666 0 110 18 25 457 703 LM01-PRD-S1
45 1462.1590277777777 0 103 19 23 419 633 LM01-PRD-S1
46 1462.16875 0 103 17 24 479 672 LM01-PRD-S1
47 1462.1791666666666 0 103 19 21 423 560 LM01-PRD-S1
48 1462.1902777777777 0 100 19 22 459 609 LM01-PRD-S1
49 1462.1993055555556 0 101 18 22 440 599 LM01-PRD-S1
50 1462.004861111111 0 98 18 22 410 572 LM01-PRD-S1
51 1.0416666666666666E-2 35 102 16 18 659 576 LB02-PRD-S1
52 1.9444444444444445E-2 35 101 17 19 729 613 LB02-PRD-S1
53 3.0555555555555555E-2 35 105 15 28 977 851 LB02-PRD-S1
54 4.0972222222222222E-2 35 96 16 28 886 852 LB02-PRD-S1
55 4.9999999999999996E-2 50 90 16 16 593 504 LB02-PRD-S1
56 6.1111111111111116E-2 50 106 18 17 737 552 LB02-PRD-S1
57 7.2222222222222229E-2 50 108 19 23 1053 775 LB02-PRD-S1
58 8.2638888888888887E-2 50 117 17 30 1236 1008 LB02-PRD-S1
59 9.2361111111111116E-2 65 113 18 29 1181 983 LB02-PRD-S1
60 0.10347222222222223 65 114 15 31 1167 1016 LB02-PRD-S1
61 0.11388888888888889 65 118 16 31 1167 1052 LB02-PRD-S1
62 0.12430555555555556 65 114 17 28 1104 967 LB02-PRD-S1
63 0.13402777777777777 80 120 17 35 1318 1172 LB02-PRD-S1
64 0.1451388888888889 80 117 16 32 1236 1153 LB02-PRD-S1
65 0.15486111111111112 80 122 17 31 1168 1094 LB02-PRD-S1
66 0.16458333333333333 80 122 17 34 1312 1205 LB02-PRD-S1
67 0.1763888888888889 95 126 18 37 1311 1274 LB02-PRD-S1
68 0.18611111111111112 95 129 18 35 1248 1201 LB02-PRD-S1
69 0.19722222222222222 95 131 15 33 1275 1196 LB02-PRD-S1
70 0.20625000000000002 95 134 18 39 1444 1381 LB02-PRD-S1
71 0.21736111111111112 110 134 19 43 1539 1472 LB02-PRD-S1
72 0.22847222222222222 110 136 19 41 1417 1406 LB02-PRD-S1
73 0.2388888888888889 110 137 20 43 1496 1437 LB02-PRD-S1
74 0.25 110 139 20 44 1561 1539 LB02-PRD-S1
75 0.25972222222222224 125 142 21 46 1561 1560 LB02-PRD-S1
76 0.26944444444444443 125 146 21 46 1535 1552 LB02-PRD-S1
77 0.28055555555555556 125 148 23 51 1698 1703 LB02-PRD-S1
78 0.29166666666666669 125 150 23 53 1725 1776 LB02-PRD-S1
79 0.30069444444444443 140 151 22 52 1726 1760 LB02-PRD-S1
80 0.31180555555555556 140 151 23 53 1713 1763 LB02-PRD-S1
81 0.32222222222222224 140 153 25 55 1807 1836 LB02-PRD-S1
82 0.33263888888888887 140 155 26 58 1897 1941 LB02-PRD-S1
83 0.34375 155 153 26 59 1929 1963 LB02-PRD-S1
84 0.35347222222222219 155 157 26 57 1843 1908 LB02-PRD-S1
85 0.36388888888888887 155 160 28 65 1942 2065 LB02-PRD-S1
86 0.375 155 164 26 64 2011 2131 LB02-PRD-S1
87 0.38472222222222219 170 166 26 65 2048 2178 LB02-PRD-S1
88 0.39583333333333331 170 166 26 64 2069 2171 LB02-PRD-S1
89 0.40625 170 169 25 64 2165 2269 LB02-PRD-S1
90 0.41666666666666669 170 169 28 76 2328 2539 LB02-PRD-S1
91 0.42638888888888887 185 169 30 76 2189 2449 LB02-PRD-S1
92 0.4368055555555555 185 171 29 73 2225 2411 LB02-PRD-S1
93 0.44722222222222219 185 171 29 68 2170 2292 LB02-PRD-S1
94 0.45763888888888887 185 171 31 82 2458 2712 LB02-PRD-S1
95 0.4680555555555555 200 171 33 89 2443 2780 LB02-PRD-S1
96 0.47847222222222219 200 173 33 87 2465 2784 LB02-PRD-S1
97 0.48888888888888887 200 176 32 88 2536 2853 LB02-PRD-S1
98 0.5 200 176 34 93 2571 2899 LB02-PRD-S1
99 0.51041666666666663 215 176 36 98 2529 2924 LB02-PRD-S1
100 0.52083333333333337 215 179 36 105 2602 3087 LB02-PRD-S1
101 0.53125 215 179 39 111 2795 3282 LB02-PRD-S1
102 0.54097222222222219 215 181 40 118 2679 3240 LB02-PRD-S1
103 0.55208333333333337 230 179 40 113 2649 3160 LB02-PRD-S1
104 0.56180555555555556 230 179 41 111 2601 3055 LB02-PRD-S1
105 0.57291666666666663 230 176 42 116 2639 3129 LB02-PRD-S1
106 0.58263888888888882 230 181 43 126 2683 3277 LB02-PRD-S1
107 0.59375 245 181 47 123 2597 3160 LB02-PRD-S1
108 0.60416666666666663 245 181 48 128 2482 3122 LB02-PRD-S1
109 NA NA NA RÈcupÈ ration NA NA LB02-PRD-S1
110 9.7222222222222224E-3 20 179 42 108 2320 2830 LB02-PRD-S1
111 2.013888888888889E-2 20 173 40 106 2134 2594 LB02-PRD-S1
112 3.125E-2 20 171 37 103 1869 2531 LB02-PRD-S1
113 4.0972222222222222E-2 20 166 38 97 1438 2207 LB02-PRD-S1
114 5.1388888888888894E-2 20 164 36 88 1192 1918 LB02-PRD-S1
115 6.1805555555555558E-2 20 155 37 81 1121 1746 LB02-PRD-S1
116 7.0833333333333331E-2 20 142 32 71 1072 1585 LB02-PRD-S1
117 8.1944444444444445E-2 20 151 26 56 961 1345 LB02-PRD-S1
118 9.2361111111111116E-2 20 148 28 58 996 1367 LB02-PRD-S1
119 0.10277777777777779 20 144 24 49 858 1189 LB02-PRD-S1
120 0.11319444444444444 20 141 25 49 722 1053 LB02-PRD-S1
121 0.125 0 136 25 42 611 895 LB02-PRD-S1
122 0.13472222222222222 0 131 26 42 642 893 LB02-PRD-S1
123 0.1451388888888889 0 129 28 44 612 874 LB02-PRD-S1
124 0.15555555555555556 0 126 24 36 544 728 LB02-PRD-S1
125 0.16527777777777777 0 127 26 40 658 840 LB02-PRD-S1
126 0.1763888888888889 0 130 23 31 511 665 LB02-PRD-S1
127 0.18611111111111112 0 126 24 39 646 815 LB02-PRD-S1
128 0.19652777777777777 0 120 25 38 527 716 LB02-PRD-S1
129 0.20694444444444446 0 120 24 36 509 684 LB02-PRD-S1
130 1462.0104166666667 25 101 20 18 712 584 GC03-PRD-S1
131 1462.0208333333333 25 99 20 17 673 551 GC03-PRD-S1
132 1462.03125 25 97 20 17 686 559 GC03-PRD-S1
133 1462.0402777777779 25 96 20 16 639 524 GC03-PRD-S1
134 1462.0506944444444 40 99 19 16 647 518 GC03-PRD-S1
135 1462.0604166666667 40 105 19 16 669 543 GC03-PRD-S1
136 1462.0729166666667 40 107 21 18 723 598 GC03-PRD-S1
137 1462.0826388888888 40 107 25 19 746 605 GC03-PRD-S1
138 1462.0916666666667 55 109 23 20 775 645 GC03-PRD-S1
139 1462.1020833333334 55 111 20 20 780 671 GC03-PRD-S1
140 1462.1118055555555 55 116 21 21 811 710 GC03-PRD-S1
141 1462.1243055555556 55 113 17 22 858 765 GC03-PRD-S1
142 1462.1340277777779 70 117 21 23 900 789 GC03-PRD-S1
143 1462.1458333333333 70 117 20 23 953 843 GC03-PRD-S1
144 1462.15625 70 120 20 25 980 882 GC03-PRD-S1
145 1462.1652777777779 70 122 22 26 1000 916 GC03-PRD-S1
146 1462.1763888888888 85 122 23 27 1049 961 GC03-PRD-S1
147 1462.1868055555556 85 126 23 28 1072 992 GC03-PRD-S1
148 1462.1965277777779 85 131 22 29 1110 1056 GC03-PRD-S1
149 1462.2076388888888 85 130 22 30 1066 1047 GC03-PRD-S1
150 1462.2173611111111 100 129 21 28 1166 1057 GC03-PRD-S1
151 1462.2284722222223 100 137 27 34 1346 1247 GC03-PRD-S1
152 1462.2395833333333 100 137 22 34 1272 1261 GC03-PRD-S1
153 1462.25 100 136 20 33 1222 1235 GC03-PRD-S1
154 1462.2590277777779 115 139 23 36 1321 1321 GC03-PRD-S1
155 1462.2701388888888 115 142 23 37 1340 1377 GC03-PRD-S1
156 1462.2798611111111 115 144 24 38 1362 1418 GC03-PRD-S1
157 1462.2909722222223 115 150 27 44 1470 1579 GC03-PRD-S1
158 1462.3013888888888 130 151 27 45 1466 1618 GC03-PRD-S1
159 1462.3125 130 153 31 54 1686 1875 GC03-PRD-S1
160 1462.3222222222223 130 155 33 59 1679 1998 GC03-PRD-S1
161 1462.3326388888888 130 157 33 59 1676 2021 GC03-PRD-S1
162 1462.3423611111111 145 157 33 61 1700 2041 GC03-PRD-S1
163 1462.3534722222223 145 160 35 64 1764 2120 GC03-PRD-S1
164 1462.3638888888888 145 160 36 67 1765 2182 GC03-PRD-S1
165 1462.3743055555556 145 162 40 71 1762 2208 GC03-PRD-S1
166 1462.0006944444444 145 162 39 69 1754 2208 GC03-PRD-S1
167 NA NA NA RÈcupÈ ration NA NA GC03-PRD-S1
168 1462.0097222222223 20 155 38 68 1687 2124 GC03-PRD-S1
169 1462.0194444444444 20 148 39 67 1576 1996 GC03-PRD-S1
170 1462.0298611111111 20 142 35 62 1390 1842 GC03-PRD-S1
171 1462.0409722222223 20 136 35 58 1189 1632 GC03-PRD-S1
172 1462.05 20 127 26 46 991 1337 GC03-PRD-S1
173 1462.0604166666667 20 117 21 26 776 896 GC03-PRD-S1
174 1462.0715277777779 20 115 22 31 855 1012 GC03-PRD-S1
175 1462.0819444444444 20 111 23 30 783 950 GC03-PRD-S1
176 1462.0930555555556 20 109 23 30 756 939 GC03-PRD-S1
177 1462.1020833333334 20 100 23 28 702 870 GC03-PRD-S1
178 1462.1131944444444 20 104 23 29 685 853 GC03-PRD-S1
179 1462.1236111111111 20 90 19 20 471 594 GC03-PRD-S1
180 1462.1340277777779 0 96 20 20 494 607 GC03-PRD-S1
181 1462.1444444444444 0 94 20 19 439 559 GC03-PRD-S1
182 1462.1548611111111 0 93 20 19 425 561 GC03-PRD-S1
183 1462.1638888888888 0 90 19 17 357 480 GC03-PRD-S1
184 1462.175 0 91 18 16 345 443 GC03-PRD-S1
185 1462.1854166666667 0 96 21 18 370 480 GC03-PRD-S1
186 1462.1958333333334 0 92 20 16 324 420 GC03-PRD-S1
187 1462.2076388888888 0 92 20 16 324 414 GC03-PRD-S1
188 1462.0083333333334 0 93 20 15 309 391 GC03-PRD-S1
189 1462.0104166666667 60 127 27 40 1267 1274 GT04-PRD-S1
190 1462.0201388888888 60 131 29 40 1264 1274 GT04-PRD-S1
191 1462.0305555555556 60 133 30 40 1281 1298 GT04-PRD-S1
192 1462.0402777777779 60 134 29 42 1304 1360 GT04-PRD-S1
193 1462.0513888888888 80 134 28 40 1274 1324 GT04-PRD-S1
194 1462.0625 80 137 28 40 1337 1335 GT04-PRD-S1
195 1462.0729166666667 80 144 29 45 1485 1501 GT04-PRD-S1
196 1462.0833333333333 80 144 30 50 1573 1630 GT04-PRD-S1
197 1462.0930555555556 100 148 30 47 1380 1478 GT04-PRD-S1
198 1462.1034722222223 100 150 30 49 1520 1576 GT04-PRD-S1
199 1462.1145833333333 100 153 31 50 1553 1589 GT04-PRD-S1
200 1462.1243055555556 100 151 31 55 1735 1818 GT04-PRD-S1
201 1462.1340277777779 120 153 32 65 1905 2146 GT04-PRD-S1
202 1462.1444444444444 120 151 32 62 1748 2026 GT04-PRD-S1
203 1462.1555555555556 120 160 31 61 1799 2041 GT04-PRD-S1
204 1462.1652777777779 120 160 30 64 1810 2105 GT04-PRD-S1
205 1462.1756944444444 140 164 33 73 1895 2314 GT04-PRD-S1
206 1462.1861111111111 140 162 33 72 1966 2345 GT04-PRD-S1
207 1462.1972222222223 140 166 36 79 2021 2470 GT04-PRD-S1
208 1462.2083333333333 140 166 35 76 2022 2450 GT04-PRD-S1
209 1462.2180555555556 160 164 37 78 2115 2491 GT04-PRD-S1
210 1462.2284722222223 160 169 40 82 2147 2583 GT04-PRD-S1
211 1462.2388888888888 160 169 38 83 2190 2647 GT04-PRD-S1
212 1462.2493055555556 160 173 38 85 2202 2713 GT04-PRD-S1
213 1462.2604166666667 180 171 38 88 2332 2837 GT04-PRD-S1
214 1462.2701388888888 180 171 41 95 2321 2937 GT04-PRD-S1
215 1462.28125 180 176 39 94 2358 2994 GT04-PRD-S1
216 1462.2909722222223 180 176 42 104 2339 3086 GT04-PRD-S1
217 1462.2979166666667 200 176 44 105 2444 3186 GT04-PRD-S1
218 NA NA NA RÈcupÈ ration NA NA GT04-PRD-S1
219 1462.0034722222222 125 179 42 97 2304 2957 GT04-PRD-S1
220 1462.0131944444445 30 171 38 92 2266 2900 GT04-PRD-S1
221 1462.0236111111112 30 166 36 93 2136 2851 GT04-PRD-S1
222 1462.0347222222222 30 166 35 91 1829 2619 GT04-PRD-S1
223 1462.0444444444445 30 162 34 83 1576 2306 GT04-PRD-S1
224 1462.0548611111112 30 160 31 65 1411 1904 GT04-PRD-S1
225 1462.0652777777777 30 155 36 78 1439 2013 GT04-PRD-S1
226 1462.0763888888889 30 153 34 69 1337 1832 GT04-PRD-S1
227 1462.0861111111112 30 153 34 66 1283 1716 GT04-PRD-S1
228 1462.0965277777777 30 144 28 49 1012 1303 GT04-PRD-S1
229 1462.1069444444445 30 134 25 41 897 1147 GT04-PRD-S1
230 1462.1180555555557 0 130 25 40 756 1051 GT04-PRD-S1
231 1462.1284722222222 0 126 20 28 500 741 GT04-PRD-S1
232 1462.1381944444445 0 123 23 27 533 712 GT04-PRD-S1
233 1462.1486111111112 0 123 23 29 548 737 GT04-PRD-S1
234 1462.1590277777777 0 117 24 24 415 560 GT04-PRD-S1
235 1462.16875 0 114 21 27 610 728 GT04-PRD-S1
236 1462.1798611111112 0 111 19 23 508 612 GT04-PRD-S1
237 1462.1902777777777 0 113 21 26 548 666 GT04-PRD-S1
238 1462.2006944444445 0 113 23 27 552 683 GT04-PRD-S1
239 1462.0020833333333 0 114 22 28 547 702 GT04-PRD-S1
I would like to remove all rows after words "ration" in the column VE BUT only for each id.
Meaning that I would like to remove lines 29 to 50, 109 to 129, 167 to 188, and from 218 to 239.
The word "ration" is repeated several times, and please take into account that I have several ID (I can not include it in my question because it is too long).
I tried to create at the end of each id but it did not work.
Thank you for your help!

With dplyr:
data %>%
group_by(id) %>%
filter(cumsum(VE == "ration") == 0)

Assuming for all the id you'll have a row with "ration", you can use dplyr like
library(dplyr)
df %>% group_by(id) %>% slice(1:(which.max(VE == "ration") -1))

Related

Split a data.frame into n random groups with x rows each

Assume a data.frame as follows:
df <- data.frame(name = paste0("Person",rep(1:30)),
number = sample(1:100, 30, replace=TRUE),
focus = sample(1:500, 30, replace=TRUE))
I want to split the above data.frame into 9 groups, each with 9 observations. Each person can be assigned to multiple groups (replacement), so that all 9 groups have all 10 observations (since 9 groups x 9 observations require 81 rows while the df has only 30).
The output will ideally be a large list of 1000 data.frames.
Are there any efficient ways of doing this? This is just a sample data.frame. The actual df has ~10k rows and will require 1000 groups each with 30 rows.
Many thanks.
Is this what you are looking for?
res <- replicate(1000, df[sample.int(nrow(df), 30, TRUE), ], FALSE)
df I used
df <- data.frame(name = paste0("Person",rep(1:1e4)),
number = sample(1:100, 1e4, replace=TRUE),
focus = sample(1:500, 1e4, replace=TRUE))
Output
> res[1:3]
[[1]]
name number focus
529 Person529 5 351
9327 Person9327 4 320
1289 Person1289 78 164
8157 Person8157 46 183
6939 Person6939 38 61
4066 Person4066 26 103
132 Person132 34 39
6576 Person6576 36 397
5376 Person5376 47 456
6123 Person6123 10 18
5318 Person5318 39 42
6355 Person6355 62 212
340 Person340 90 256
7050 Person7050 19 198
1500 Person1500 42 208
175 Person175 34 30
3751 Person3751 99 441
3813 Person3813 93 492
7428 Person7428 72 142
6840 Person6840 58 45
6501 Person6501 95 499
5124 Person5124 16 159
3373 Person3373 38 36
5622 Person5622 40 203
8761 Person8761 9 225
6252 Person6252 75 444
4502 Person4502 58 337
5344 Person5344 24 233
4036 Person4036 59 265
8764 Person8764 45 1
[[2]]
name number focus
8568 Person8568 87 360
3968 Person3968 67 468
4481 Person4481 46 140
8055 Person8055 73 286
7794 Person7794 92 336
1110 Person1110 6 434
6736 Person6736 4 58
9758 Person9758 60 49
9356 Person9356 89 300
9719 Person9719 100 366
4183 Person4183 5 124
1394 Person1394 87 346
2642 Person2642 81 449
3592 Person3592 65 358
579 Person579 21 395
9551 Person9551 39 495
4946 Person4946 73 32
4081 Person4081 98 270
4062 Person4062 27 150
7698 Person7698 52 436
5388 Person5388 89 177
9598 Person9598 91 474
8624 Person8624 3 464
392 Person392 82 483
5710 Person5710 43 293
4942 Person4942 99 350
3333 Person3333 89 91
6789 Person6789 99 259
7115 Person7115 100 320
1431 Person1431 77 263
[[3]]
name number focus
201 Person201 100 272
4674 Person4674 27 410
9728 Person9728 18 275
9422 Person9422 2 396
9783 Person9783 45 37
5552 Person5552 76 109
3871 Person3871 49 277
3411 Person3411 64 24
5799 Person5799 29 131
626 Person626 31 122
3103 Person3103 2 76
8043 Person8043 90 384
3157 Person3157 90 392
7093 Person7093 11 169
2779 Person2779 83 2
2601 Person2601 77 122
9003 Person9003 50 163
9653 Person9653 4 235
9361 Person9361 100 391
4273 Person4273 83 383
4725 Person4725 35 436
2157 Person2157 71 486
3995 Person3995 25 258
3735 Person3735 24 221
303 Person303 81 407
4838 Person4838 64 198
6926 Person6926 90 417
6267 Person6267 82 284
8570 Person8570 67 317
2670 Person2670 21 342

Moving values in a column to a specific row

I have dataset in my bachelor thesis.
This is just a short version of the dataset (still too much to post it here, sorry),
but the case is, I have to make a list for each year with Return12 for year 9 compared to the SROE-value for year 8, Return12 year 10 with SROE for year 9, etc.
The company ID is in the second column and have to match the SROE and Return12, so the companies dont get mixed up.
I have tried with some different functions, but it does not solve my case.
I have tried this code, but it does not work like i want it to.
year.order2 <- ret12[order(ret12$year, ret12$SROE, ret12$Return12, na.last = T, decreasing = T),]
yeartest <- transform(year.order2, Return12 = c(NA, Return12[-nrow(year.order2)]))
id year SROE Return12 OSEBXr12
1814 166 15 21.06345172 -0.160281717 -0.059150
582 53 15 13.32248824 0.101805502 -0.059150
868 79 15 9.45765120 0.916125271 -0.059150
395 36 15 8.91144364 -0.046316276 -0.059150
318 29 15 6.87606276 -0.177370031 -0.059150
1286 118 15 6.75801735 1.069037413 -0.059150
329 30 15 6.44705341 0.398571429 -0.059150
1319 121 15 6.40374891 0.617598303 -0.059150
956 88 15 6.38674853 1.382978723 -0.059150
373 34 15 6.24516436 -0.057201646 -0.059150
1154 106 15 5.94762885 0.289787481 -0.059150
626 57 15 5.13702836 0.110565500 -0.059150
1539 141 15 5.06195259 -0.235493373 -0.059150
1704 156 15 4.96444477 0.381119862 -0.059150
967 89 15 4.49788681 -0.168720150 -0.059150
1451 133 15 4.16517045 0.291875733 -0.059150
1803 165 15 3.84209438 0.258043950 -0.059150
197 18 15 3.79648512 -0.111447964 -0.059150
1528 140 15 3.55027135 0.426369168 -0.059150
1737 159 15 3.43669721 0.413409961 -0.059150
241 22 15 3.12265152 -0.677819083 -0.059150
54 5 15 2.99865083 0.594527262 -0.059150
1660 152 15 2.98208332 -0.116092772 -0.059150
1693 155 15 2.72894105 0.315186197 -0.059150
1055 97 15 2.59080259 -0.288888889 -0.059150
1011 93 15 2.52560311 -0.245794393 -0.059150
989 91 15 2.37809304 0.006064505 -0.059150
450 41 15 2.34690356 0.012987013 -0.059150
1770 162 15 2.32962182 -0.223670187 -0.059150
802 73 15 2.28667081 0.120406075 -0.059150
1462 134 15 2.24283341 -0.028824886 -0.059150
549 50 15 2.15182069 -0.468315301 -0.059150
285 26 15 2.08732229 -0.271859964 -0.059150
10 1 15 1.97156443 0.482100054 -0.059150
340 31 15 1.93518075 0.899027442 -0.059150
1176 108 15 1.92194207 -0.139118199 -0.059150
1066 98 15 1.84783091 0.389512481 -0.059150
1748 160 15 1.83224060 -0.043163852 -0.059150
1781 163 15 1.80810900 -0.130968785 -0.059150
1407 129 15 1.76854453 0.548725101 -0.059150
208 19 15 1.71286374 0.280728627 -0.059150
824 75 15 1.60228776 0.279648956 -0.059150
1132 104 15 1.55899607 0.768918919 -0.059150
703 64 15 1.53559636 -0.772245890 -0.059150
43 4 15 1.53499544 0.692307692 -0.059150
1352 124 15 1.51347913 -0.537500000 -0.059150
1198 110 15 1.49695624 0.277698086 -0.059150
120 11 15 1.44169619 1.032124757 -0.059150
1550 142 15 1.43922411 -0.075267137 -0.059150
406 37 15 1.43645465 -0.661764706 -0.059150
263 24 15 1.40896986 -0.440000000 -0.059150
1297 119 15 1.39968959 0.005263158 -0.059150
1363 125 15 1.37839665 -0.206253758 -0.059150
857 78 15 1.36001779 0.223417484 -0.059150
109 10 15 1.32579947 -0.289615397 -0.059150
186 17 15 1.31100145 0.184308881 -0.059150
912 84 15 1.30878950 -0.259685157 -0.059150
472 43 15 1.30834391 -0.153648794 -0.059150
659 60 15 1.30399083 0.112676367 -0.059150
1671 153 15 1.29325084 -0.270092033 -0.059150
1825 167 15 1.23598258 0.128611436 -0.059150
1594 146 15 1.22400548 0.497863248 -0.059150
758 69 15 1.21047789 0.287523953 -0.059150
1440 132 15 1.20156226 0.011987913 -0.059150
835 76 15 1.18580289 0.840000000 -0.059150
846 77 15 1.17520829 -0.459869848 -0.059150
1682 154 15 1.16911115 -0.532894737 -0.059150
615 56 15 1.16624567 0.047679182 -0.059150
1506 138 15 1.16262451 -0.587982833 -0.059150
813 74 15 1.15630435 -0.142465321 -0.059150
560 51 15 1.14421086 -0.342842083 -0.059150
681 62 15 1.14286094 0.086538462 -0.059150
1616 148 15 1.13999073 0.680258790 -0.059150
1077 99 15 1.11327454 -0.136842105 -0.059150
142 13 15 1.10908331 -0.143200645 -0.059150
274 25 15 1.09553083 -0.313725490 -0.059150
1330 122 15 1.04979784 0.033805889 -0.059150
362 33 15 1.04725763 -0.132558140 -0.059150
1209 111 15 1.02722876 -0.548148148 -0.059150
692 63 15 1.02647920 0.725768322 -0.059150
175 16 15 0.99187168 -0.396054628 -0.059150
1429 131 15 0.98206425 0.233683616 -0.059150
1605 147 15 0.97786358 -0.213469388 -0.059150
890 82 15 0.97771373 -0.653846154 -0.059150
1231 113 15 0.97322215 -0.511597374 -0.059150
780 71 15 0.95005051 1.476789384 -0.059150
1165 107 15 0.94223541 -0.295668550 -0.059150
1033 95 15 0.93440462 -0.353345780 -0.059150
1572 144 15 0.93264693 -0.029200673 -0.059150
1374 126 15 0.92729234 -0.635913313 -0.059150
1495 137 15 0.91475915 0.399845573 -0.059150
648 59 15 0.86536731 -0.742239807 -0.059150
1044 96 15 0.86369475 -0.691011236 -0.059150
1792 164 15 0.86246004 -0.429643121 -0.059150
714 65 15 0.85465118 -0.725639927 -0.059150
1484 136 15 0.84167952 -0.067357513 -0.059150
604 55 15 0.82344486 0.050573980 -0.059150
593 54 15 0.81699442 -0.054545455 -0.059150
527 48 15 0.81574188 -0.541223969 -0.059150
1308 120 15 0.78682497 0.090975610 -0.059150
164 15 15 0.75097824 0.113962960 -0.059150
1121 103 15 0.73844731 2.831560758 -0.059150
1649 151 15 0.72396064 -0.835227273 -0.059150
571 52 15 0.71393614 -0.670806343 -0.059150
153 14 15 0.71010885 0.199908967 -0.059150
637 58 15 0.70854122 -0.896969697 -0.059150
945 87 15 0.70774797 1.402923537 -0.059150
1473 135 15 0.70710688 -0.828676471 -0.059150
1110 102 15 0.69097013 -0.168269231 -0.059150
1396 128 15 0.68369318 0.250000000 -0.059150
1253 115 15 0.67988410 -0.365682093 -0.059150
1759 161 15 0.67049105 -0.367088608 -0.059150
65 6 15 0.66389168 -0.540526316 -0.059150
219 20 15 0.64685798 -0.721890547 -0.059150
252 23 15 0.62984681 -0.463291139 -0.059150
1726 158 15 0.61402854 -0.436956522 -0.059150
1418 130 15 -0.79965248 -0.478260870 -0.059150
1549 142 14 23.11684369 -0.178885860 0.112869
317 29 14 20.23086241 0.644505325 0.112869
1318 121 14 8.58091313 1.007734881 0.112869
988 91 14 7.90873362 -0.269991974 0.112869
867 79 14 6.44919884 0.275177305 0.112869
328 30 14 6.40544835 -0.059629331 0.112869
1175 108 14 6.32482120 0.540399113 0.112869
1351 124 14 5.50407132 0.000000000 0.112869
1538 141 14 5.46883066 -0.179431025 0.112869
581 53 14 5.14092813 0.250627538 0.112869
1527 140 14 4.68597924 -0.071334648 0.112869
790 72 14 4.27448237 0.044596913 0.112869
240 22 14 3.72618752 -0.489240506 0.112869
1307 120 14 3.65873267 -0.125047671 0.112869
1505 138 14 3.62463735 -0.107279693 0.112869
9 1 14 3.60998039 0.042533569 0.112869
1285 118 14 3.59967078 -0.431940200 0.112869
196 18 14 3.56824369 0.612544044 0.112869
1692 155 14 3.37111155 0.194139194 0.112869
339 31 14 3.07045337 0.698513384 0.112869
1747 160 14 3.00051614 0.290793547 0.112869
1670 153 14 2.99930554 0.157513854 0.112869
801 73 14 2.98911215 0.056882806 0.112869
1593 146 14 2.97564257 -0.372822300 0.112869
1659 152 14 2.87893104 0.340008624 0.112869
1373 126 14 2.65668093 -0.350949463 0.112869
1197 110 14 2.61785580 0.233531310 0.112869
1703 156 14 2.60464237 0.181673156 0.112869
1769 162 14 2.57249329 -0.213467753 0.112869
53 5 14 2.43112019 0.332925097 0.112869
1131 104 14 2.42196540 0.075172554 0.112869
1736 159 14 2.39665786 0.767601043 0.112869
636 58 14 2.31838774 -0.550000000 0.112869
284 26 14 2.17325240 -0.390076974 0.112869
262 24 14 2.15494774 -0.393939394 0.112869
1516 139 14 2.12650027 -0.437762869 0.112869
1010 93 14 2.00163574 0.789297659 0.112869
372 34 14 1.95460996 0.435506242 0.112869
548 50 14 1.95355193 -0.680153093 0.112869
1076 99 14 1.91838973 0.490926925 0.112869
1450 133 14 1.82737021 0.193544383 0.112869
460 42 14 1.78104084 -0.418705036 0.112869
1153 106 14 1.77902235 0.399846259 0.112869
1813 166 14 1.77211578 0.612540666 0.112869
185 17 14 1.76411747 0.363361388 0.112869
680 62 14 1.72407748 -0.460580913 0.112869
691 63 14 1.71478535 -0.364864865 0.112869
1494 137 14 1.69161571 0.106408757 0.112869
614 56 14 1.65779152 0.084792900 0.112869
1780 163 14 1.62571349 -0.230977814 0.112869
702 64 14 1.62480728 -0.512077295 0.112869
1208 111 14 1.59762131 -0.560260586 0.112869
592 54 14 1.59175502 -0.316087737 0.112869
735 67 14 1.44799035 -0.155844156 0.112869
273 25 14 1.43157370 0.280334728 0.112869
1406 129 14 1.42599186 0.694259743 0.112869
559 51 14 1.41919468 -0.374564480 0.112869
42 4 14 1.40889942 0.090621089 0.112869
97 9 14 1.40809834 -0.439447147 0.112869
207 19 14 1.40806506 0.306086405 0.112869
394 36 14 1.39957200 0.086343404 0.112869
405 37 14 1.39021190 -0.165848871 0.112869
526 48 14 1.35374016 -0.713503901 0.112869
163 15 14 1.34048347 -0.411522634 0.112869
1571 144 14 1.32108835 -0.082097870 0.112869
1725 158 14 1.31531715 -0.252032520 0.112869
625 57 14 1.31394786 0.171449830 0.112869
658 60 14 1.31200671 -0.240147697 0.112869
1054 97 14 1.31136914 -0.566265060 0.112869
1461 134 14 1.26710097 -0.410000000 0.112869
1065 98 14 1.26289838 0.747161593 0.112869
1758 161 14 1.26275618 0.519879889 0.112869
1164 107 14 1.23857503 0.234883721 0.112869
449 41 14 1.23381575 0.247141768 0.112869
823 75 14 1.23084920 0.505203218 0.112869
1043 96 14 1.19762524 0.311233886 0.112869
779 71 14 1.19489416 -0.061111111 0.112869
75 7 14 1.18112630 -0.057826087 0.112869
856 78 14 1.11758807 0.568019472 0.112869
416 38 14 1.10565310 -0.661290323 0.112869
944 87 14 1.08837331 1.441337890 0.112869
1296 119 14 1.03762698 0.478599222 0.112869
812 74 14 0.98145205 -0.151996606 0.112869
1604 147 14 0.97324462 -0.218089603 0.112869
570 52 14 0.91405742 -0.240557389 0.112869
966 89 14 0.90857225 0.015850587 0.112869
834 76 14 0.90626954 0.351351351 0.112869
603 55 14 0.90553614 0.120000000 0.112869
1681 154 14 0.90081211 0.032258065 0.112869
218 20 14 0.87615674 -0.359872611 0.112869
1483 136 14 0.87269220 -0.077086255 0.112869
1362 125 14 0.85469080 -0.389500734 0.112869
119 11 14 0.83707515 0.777252896 0.112869
911 84 14 0.82895276 -0.295532646 0.112869
1648 151 14 0.82599899 -0.200000000 0.112869
1252 115 14 0.82477468 -0.329849570 0.112869
1472 135 14 0.82033134 2.177570093 0.112869
1230 113 14 0.81397746 -0.556864986 0.112869
889 82 14 0.81389255 -0.428571429 0.112869
1439 132 14 0.77224072 0.378296306 0.112869
427 39 14 0.74909867 -0.315168827 0.112869
757 69 14 0.72448797 0.024644523 0.112869
152 14 14 0.70740409 -0.442857143 0.112869
1395 128 14 0.68401333 -0.118942731 0.112869
1186 109 14 0.66692557 0.205473743 0.112869
669 61 14 0.66651108 -0.411764706 0.112869
316 29 13 27.67796398 0.475557461 0.150159
1493 137 13 13.56200755 0.045216461 0.150159
1746 160 13 10.99968277 0.051940109 0.150159
1768 162 13 7.53413090 0.213153491 0.150159
239 22 13 6.95310312 0.046357616 0.150159
327 30 13 6.34254755 0.296625222 0.150159
1372 126 13 5.45573520 0.332028548 0.150159
1537 141 13 4.60641610 0.489180155 0.150159
789 72 13 4.34881818 2.761290323 0.150159
558 51 13 4.08086211 -0.043070363 0.150159
1306 120 13 4.07077399 0.099983769 0.150159
547 50 13 4.03234457 -0.061875401 0.150159
1702 156 13 3.95355025 0.181487296 0.150159
1548 142 13 3.89783966 -0.137992832 0.150159
8 1 13 3.80333107 0.220912940 0.150159
1174 108 13 3.77325183 0.147305733 0.150159
800 73 13 3.70624126 0.011274207 0.150159
1669 153 13 3.61855059 -0.172420877 0.150159
987 91 13 3.39669062 0.741665461 0.150159
690 63 13 3.39536421 4.414634146 0.150159
1735 159 13 3.19989358 0.082035101 0.150159
1185 109 13 2.84971719 1.499371647 0.150159
580 53 13 2.75702524 0.503066618 0.150159
1042 96 13 2.70983419 0.378172589 0.150159
1658 152 13 2.70327736 0.131344886 0.150159
195 18 13 2.69289531 0.042694820 0.150159
283 26 13 2.66181946 -0.081758086 0.150159
866 79 13 2.51063140 0.072275199 0.150159
1779 163 13 2.44508433 0.179777004 0.150159
613 56 13 2.43963461 0.013710104 0.150159
52 5 13 2.35899802 0.332536076 0.150159
1075 99 13 2.16278220 0.224624625 0.150159
1570 144 13 2.06554782 0.074010846 0.150159
525 48 13 2.03023407 -0.038518650 0.150159
1504 138 13 1.98459096 0.167515938 0.150159
1812 166 13 1.91311640 -0.066946965 0.150159
151 14 13 1.90382668 0.093750000 0.150159
338 31 13 1.84584193 0.091777983 0.150159
591 54 13 1.80853387 0.203619909 0.150159
272 25 13 1.78598713 1.728310502 0.150159
184 17 13 1.76112921 0.009867010 0.150159
1009 93 13 1.74752529 0.980132450 0.150159
1691 155 13 1.67190657 -0.197058824 0.150159
118 11 13 1.65524659 0.278160919 0.150159
437 40 13 1.61806035 0.014064277 0.150159
811 74 13 1.58880643 -0.153717037 0.150159
63 6 13 1.58381410 -0.165931982 0.150159
448 41 13 1.58348698 -0.090444673 0.150159
1592 146 13 1.57872558 0.332024277 0.150159
1724 158 13 1.55216473 -0.247964202 0.150159
1284 118 13 1.52735674 11.877689760 0.150159
1251 115 13 1.48634481 -0.318053446 0.150159
1130 104 13 1.46625297 2.543506189 0.150159
404 37 13 1.46229134 1.193756728 0.150159
1361 125 13 1.42651972 1.569811321 0.150159
1086 100 13 1.38033411 -0.930241731 0.150159
668 61 13 1.37085612 0.422074816 0.150159
569 52 13 1.34391491 0.158614113 0.150159
1438 132 13 1.34255306 0.693896980 0.150159
1317 121 13 1.34071609 0.732839506 0.150159
96 9 13 1.33847231 -0.259324009 0.150159
1614 148 13 1.31243851 0.120572362 0.150159
1603 147 13 1.30535127 0.085560151 0.150159
1229 113 13 1.25070855 -0.050632911 0.150159
1053 97 13 1.23077193 -0.135416667 0.150159
855 78 13 1.23018452 0.366298881 0.150159
217 20 13 1.22733648 -0.071005917 0.150159
1328 122 13 1.21687585 -0.146341463 0.150159
459 42 13 1.21666304 -0.379464286 0.150159
1163 107 13 1.21262434 0.137566138 0.150159
965 89 13 1.18173008 0.858823529 0.150159
206 19 13 1.16441222 0.208124942 0.150159
943 87 13 1.14644451 -0.767595508 0.150159
679 62 13 1.14255503 0.532591415 0.150159
1757 161 13 1.13108091 -0.477468616 0.150159
1647 151 13 1.12639353 1.244897959 0.150159
701 64 13 1.11588786 -0.577551020 0.150159
778 71 13 1.10966802 -0.090909091 0.150159
371 34 13 1.06990074 0.665256076 0.150159
1295 119 13 1.02445843 -0.284122563 0.150159
734 67 13 1.00937090 -0.478555305 0.150159
1449 133 13 0.99690576 -0.016393442 0.150159
261 24 13 0.99320149 -0.423580786 0.150159
1196 110 13 0.95969164 0.062824513 0.150159
1482 136 13 0.95074932 0.033018868 0.150159
822 75 13 0.92045109 0.259812898 0.150159
1526 140 13 0.91234577 0.362339810 0.150159
1207 111 13 0.90909480 0.100358423 0.150159
1405 129 13 0.90775970 0.475961538 0.150159
657 60 13 0.90306208 3.213508197 0.150159
602 55 13 0.86449560 0.999999999 0.150159
183 17 12 12.19940204 0.008113526 0.172201
1492 137 12 9.88702902 0.092625661 0.172201
194 18 12 8.51621898 0.228817367 0.172201
546 50 12 6.80902674 0.305842246 0.172201
1536 141 12 5.96245535 0.167890067 0.172201
326 30 12 5.95646725 -0.018736383 0.172201
1811 166 12 5.81124269 0.268209538 0.172201
1371 126 12 5.51770644 0.189542484 0.172201
392 36 12 4.50167386 0.269362552 0.172201
447 41 12 4.21790966 -0.091686379 0.172201
216 20 12 4.15954590 -0.417241379 0.172201
557 51 12 4.15186011 0.208791209 0.172201
799 73 12 3.65748981 0.149911331 0.172201
1547 142 12 3.38614688 0.861518705 0.172201
1569 144 12 3.14547059 0.034114646 0.172201
1503 138 12 3.08482928 0.108910891 0.172201
1173 108 12 2.98874897 0.144519795 0.172201
524 48 12 2.96726211 -0.106050749 0.172201
1668 153 12 2.82006942 0.445982767 0.172201
568 52 12 2.78044947 0.252641825 0.172201
1646 151 12 2.73729474 0.447373101 0.172201
1305 120 12 2.66133730 0.095990358 0.172201
51 5 12 2.62860138 0.453081557 0.172201
62 6 12 2.58973221 0.733007177 0.172201
1734 159 12 2.54991113 0.148192719 0.172201
986 91 12 2.50720858 -0.142857143 0.172201
1591 146 12 2.48325687 -0.034137931 0.172201
1745 160 12 2.47639237 -0.089179887 0.172201
612 56 12 2.46973545 0.172545127 0.172201
1657 152 12 2.45588097 0.324941037 0.172201
238 22 12 2.45041065 -0.460714286 0.172201
1008 93 12 2.38060628 -0.174863388 0.172201
304 28 12 2.38003456 0.366390417 0.172201
689 63 12 2.35868026 -0.250000000 0.172201
282 26 12 2.21960529 0.154440154 0.172201
579 53 12 2.20123380 0.352456021 0.172201
865 79 12 2.13139273 0.154602047 0.172201
40 4 12 2.08207931 -0.016197073 0.172201
964 89 12 2.07859137 0.053780182 0.172201
1767 162 12 1.97715726 0.626024053 0.172201
1294 119 12 1.85382604 -0.028205128 0.172201
1283 118 12 1.83548975 1.479564033 0.172201
656 60 12 1.79853322 0.837349397 0.172201
271 25 12 1.76021383 -0.354611726 0.172201
590 54 12 1.69943635 -0.059671471 0.172201
1701 156 12 1.68628544 0.106797253 0.172201
1690 155 12 1.65291529 -0.096632588 0.172201
7 1 12 1.65211344 0.140809487 0.172201
1074 99 12 1.64881940 1.134615385 0.172201
1327 122 12 1.57204072 -0.132275132 0.172201
1261 116 12 1.56805209 -0.303030303 0.172201
1041 96 12 1.54257211 -0.428985507 0.172201
667 61 12 1.51717722 -0.136690647 0.172201
810 74 12 1.50738969 0.341888698 0.172201
95 9 12 1.50651922 -0.041340782 0.172201
1778 163 12 1.48473960 0.302116311 0.172201
161 15 12 1.43426560 0.150862069 0.172201
700 64 12 1.43398480 -0.579232099 0.172201
1360 125 12 1.40030030 -0.735612579 0.172201
1184 109 12 1.32326647 0.180575205 0.172201
1316 121 12 1.30587844 0.250000000 0.172201
920 85 12 1.28268472 0.102362205 0.172201
1228 113 12 1.26764800 -0.159574468 0.172201
1712 157 12 1.23739802 0.329823748 0.172201
403 37 12 1.23470156 0.140577041 0.172201
205 19 12 1.22583617 0.270007622 0.172201
1756 161 12 1.22561713 -0.900392157 0.172201
150 14 12 1.12110726 0.015873016 0.172201
117 11 12 1.08773604 0.087500000 0.172201
1679 154 12 1.01730540 0.273743017 0.172201
1085 100 12 0.99614726 -0.166723569 0.172201
733 67 12 0.97046135 -0.439240506 0.172201
755 69 12 0.95406148 -0.279629630 0.172201
821 75 12 0.92934814 0.729949635 0.172201
854 78 12 0.92924014 0.717964824 0.172201
942 87 12 0.92652589 -0.818032787 0.172201
1129 104 12 0.91253834 0.007936508 0.172201
1602 147 12 0.90914950 0.281739130 0.172201
1404 129 12 0.90468701 0.625000000 0.172201
1030 95 12 0.89828972 -0.130977229 0.172201
73 7 12 0.88164808 0.494091309 0.172201
1107 102 12 0.87885010 0.020431646 0.172201
1437 132 12 0.87071649 0.336884800 0.172201
1052 97 12 0.86737375 -0.587002557 0.172201
777 71 12 0.83710890 0.119688299 0.172201
348 32 12 0.82375721 -0.643449133 0.172201
458 42 12 0.81877908 -0.321212121 0.172201
337 31 12 0.81128328 -0.149206441 0.172201
1063 98 12 0.79934806 1.473684210 0.172201
678 62 12 0.79695044 -0.140710383 0.172201
249 23 12 0.79493117 0.318363273 0.172201
1723 158 12 0.78859160 0.429162228 0.172201
370 34 12 0.78024017 0.066666667 0.172201
436 40 12 0.77619489 0.129891304 0.172201
601 55 12 0.77193287 1.220248668 0.172201
139 13 12 -3.32258505 1.585585585 0.172201
578 53 11 45.76543378 0.228037564 -0.076420
182 17 11 11.61880543 0.092649764 -0.076420
1491 137 11 11.10701743 -0.099834528 -0.076420
1667 153 11 10.52229111 0.116544776 -0.076420
1106 102 11 9.11660210 -0.163753237 -0.076420
985 91 11 6.73195560 -0.561824388 -0.076420
193 18 11 6.11699997 0.039019964 -0.076420
1535 141 11 6.06085500 -0.413730672 -0.076420
303 28 11 5.95462907 -0.139209467 -0.076420
50 5 11 5.63631194 0.148517965 -0.076420
325 30 11 4.70780092 0.348885348 -0.076420
963 89 11 3.42521562 -0.080424577 -0.076420
798 73 11 3.39272134 -0.051378752 -0.076420
1744 160 11 3.03847970 -0.091287843 -0.076420
61 6 11 2.96322084 -0.215844382 -0.076420
1733 159 11 2.87609409 -0.130428605 -0.076420
160 15 11 2.86881209 -0.292682927 -0.076420
94 9 11 2.54069565 2.162544170 -0.076420
545 50 11 2.50990774 -0.033396566 -0.076420
1689 155 11 2.34290938 0.311896826 -0.076420
1656 152 11 2.29133789 0.118316301 -0.076420
611 56 11 2.19636482 -0.335638698 -0.076420
589 54 11 2.19581833 -0.253275109 -0.076420
1590 146 11 2.16014050 -0.348423877 -0.076420
281 26 11 2.09792109 -0.213768116 -0.076420
1502 138 11 2.08817684 0.068783069 -0.076420
864 79 11 2.05294729 -0.062938980 -0.076420
248 23 11 2.05179688 -0.296842105 -0.076420
6 1 11 1.98610066 -0.365344741 -0.076420
1172 108 11 1.86819779 -0.097636799 -0.076420
116 11 11 1.82851695 -0.284436228 -0.076420
1810 166 11 1.75851688 -0.260376164 -0.076420
1568 144 11 1.74672801 0.097011298 -0.076420
204 19 11 1.68777151 -0.416080308 -0.076420
1645 151 11 1.68005446 -0.540000000 -0.076420
391 36 11 1.61064785 -0.184871744 -0.076420
446 41 11 1.52375216 -0.135309245 -0.076420
1700 156 11 1.52348562 0.155791335 -0.076420
1128 104 11 1.50795925 -0.045454545 -0.076420
1546 142 11 1.50634306 1.017021276 -0.076420
1051 97 11 1.46633126 -0.464692483 -0.076420
1304 120 11 1.46249821 0.043560606 -0.076420
1326 122 11 1.43587335 0.125000000 -0.076420
270 25 11 1.42570485 -0.281250000 -0.076420
1007 93 11 1.40832289 -0.170318258 -0.076420
820 75 11 1.35452065 -0.432659647 -0.076420
369 34 11 1.31666123 -0.375000000 -0.076420
1601 147 11 1.31193259 0.244933411 -0.076420
1403 129 11 1.29946491 -0.471343246 -0.076420
556 51 11 1.28903970 -0.213177517 -0.076420
853 78 11 1.27235228 -0.444786553 -0.076420
1755 161 11 1.26301155 0.853197674 -0.076420
237 22 11 1.25496888 -0.496402878 -0.076420
941 87 11 1.23050445 -0.227848101 -0.076420
1766 162 11 1.19046710 -0.155750557 -0.076420
567 52 11 1.18480736 0.004008746 -0.076420
1777 163 11 1.13617940 -0.100416011 -0.076420
655 60 11 1.11783748 -0.520271663 -0.076420
1227 113 11 1.11366867 -0.262745098 -0.076420
Here is an example with fake data and two methods to achieve your goal. As I don't know how you want to 'compare' these values, I am just using a simple formula as an illustration.
# Generate data
df <- data.frame(id = rep(1:10,3),
year = rep(14:16,each=10),
SROE = rnorm(30,10,4),
Return12 = rnorm(30,0,2)
)
library(dplyr)
Option 1
This method uses a left_join, joining the measurement of this year with that of a previous year. To match data of the current year with the previous year, I first mutate the year of the second data.frame with +1. The x and y in the columns generated represend the left (x) and the right (y) data.frame in the join (ie. left %>% left_join(right))
# join data previous year
df %>%
left_join(df %>% mutate(year = year+1),by=c('id','year')) %>%
mutate(compare = Return12.x - SROE.y)
Option 2
This method uses the dplyr::lag function. As this method depends on sorting your data and getting info from the previous row, you must be 100% sure that every id has all the years of interest. If not, for example because of missing observation, you will have to deal with that, for example with the function dplyr::complete
# Order data and use lag function
df %>%
arrange(id, year) %>%
group_by(id) %>%
mutate(compare = Return12 - lag(SROE))

R: Using decompose() in data.table

Hi I have a time series data as follows:
Code Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1001 2009 183 175 151 173 169 169 158 132 91 91 146 114
1001 2010 76 103 130 103 78 72 64 96 89 91 61 62
1001 2011 73 50 99 90 74 112 113 111 112 112 97 137
1001 2012 105 140 160 129 162 161 150 167 151 161 114 120
1001 2013 140 137 153 128 137 137 135 148 116 134 121 95
1001 2014 135 145 144 110 109 130 110 58 100 109 67 66
1015 2009 21 19 19 21 17 29 56 35 46 33 42 45
1015 2010 46 29 55 62 49 48 44 37 39 46 33 39
1015 2011 59 36 52 41 36 38 42 43 37 37 37 35
1015 2012 46 53 55 41 69 41 38 42 37 50 46 48
1015 2013 64 43 58 43 50 39 29 48 45 26 51 55
1015 2014 40 54 64 58 76 59 69 66 57 60 58 55
1031 2009 2408 2370 2799 3460 3263 3102 2769 2749 3018 3283 3343 3193
1031 2010 3130 3069 3776 3348 3341 4129 3920 4131 4152 4044 4241 3522
1031 2011 3454 3768 5217 4242 4624 5105 4712 6064 5546 6049 5957 4670
1031 2012 4959 3554 2163 1274 1452 1248 1303 1278 916 906 522 324
1031 2013 537 442 417 389 469 423 328 246 291 387 201 122
1031 2014 249 203 42 30 29 36 39 16 36 23 11 19
I am trying to find the decomposition of the timeseries by Code column as different Codes have different trends and seasonality during various months.
I tried using data.table but it gives me error. Following is the code that I am using:
sa_data_ssn_cnt_ts <- data.table(sa_data_ssn_cnt_ts)
sa_data_ssn_cnt_si <- sa_data_ssn_cnt_ts[,list(SI = decompose(sa_data_ssn_cnt_ts, type = "multiplicative", filter = NULL)), by = sa_data_ssn_cnt_ts$site_id]
Error that I get:
Error in decompose(sa_data_ssn_cnt_ts, type = "multiplicative", filter = NULL) : time series has no or less than 2 periods
What is it that I am messing up here?
Is there any other way that I can get the decompositions by Code column?
Thanks a lot for the help.

continuous value supplied to discrete scale

I am new to ggplot2. In fact, I only discovered it last week and I haven't quite figured out yet how to use aesthetics and scales etc. There is probably a very easy solution to my problem but I couldn't find a satisfying answer online.
Sorry for the size of the message, but all the data used is in the following script:
dados
Fres Vc Lu
1 466 30 10
2 416 30 10
3 465 30 10
4 416 30 10
5 464 30 10
6 416 30 10
7 476 30 10
8 412 30 10
9 468 30 10
10 410 30 10
11 470 30 10
12 407 30 10
13 468 30 10
14 412 30 10
15 469 30 10
16 414 30 10
17 469 30 10
18 412 30 10
19 467 30 10
20 409 30 10
21 469 30 10
22 415 30 10
23 471 30 10
24 420 30 10
25 469 30 10
26 416 30 10
27 464 30 10
28 409 30 10
29 465 30 10
30 412 30 10
31 464 30 10
32 409 30 10
33 466 30 10
34 417 30 10
35 466 30 10
36 417 30 10
37 464 30 10
38 414 30 10
39 466 30 10
40 415 30 10
41 585 30 94
42 234 30 94
43 589 30 94
44 231 30 94
45 585 30 94
46 223 30 94
47 586 30 94
48 223 30 94
49 572 30 94
50 233 30 94
51 585 30 94
52 233 30 94
53 589 30 94
54 234 30 94
55 598 30 94
56 237 30 94
57 605 30 94
58 237 30 94
59 586 30 94
60 233 30 94
61 588 30 94
62 227 30 94
63 585 30 94
64 230 30 94
65 586 30 94
66 230 30 94
67 591 30 94
68 237 30 94
69 586 30 94
70 234 30 94
71 592 30 94
72 237 30 94
73 595 30 94
74 236 30 94
75 600 30 94
76 227 30 94
77 592 30 94
78 237 30 94
79 592 30 94
80 240 30 94
81 468 30 10
82 408 30 10
83 471 30 10
84 405 30 10
85 475 30 10
86 403 30 10
87 470 30 10
88 409 30 10
89 478 30 10
90 405 30 10
91 474 30 10
92 403 30 10
93 472 30 10
94 402 30 10
95 478 30 10
96 408 30 10
97 477 30 10
98 406 30 10
99 473 30 10
100 406 30 10
101 474 30 10
102 406 30 10
103 477 30 10
104 411 30 10
105 480 30 10
106 413 30 10
107 479 30 10
108 408 30 10
109 476 30 10
110 406 30 10
111 476 30 10
112 404 30 10
113 472 30 10
114 407 30 10
115 474 30 10
116 411 30 10
117 473 30 10
118 415 30 10
119 479 30 10
120 409 30 10
121 578 30 94
122 370 30 94
123 570 30 94
124 378 30 94
125 575 30 94
126 367 30 94
127 579 30 94
128 371 30 94
129 576 30 94
130 362 30 94
131 579 30 94
132 372 30 94
133 588 30 94
134 375 30 94
135 586 30 94
136 372 30 94
137 589 30 94
138 378 30 94
139 587 30 94
140 375 30 94
141 578 30 94
142 368 30 94
143 575 30 94
144 375 30 94
145 574 30 94
146 376 30 94
147 575 30 94
148 367 30 94
149 580 30 94
150 382 30 94
151 583 30 94
152 368 30 94
153 591 30 94
154 386 30 94
155 595 30 94
156 379 30 94
157 593 30 94
158 384 30 94
159 607 30 94
160 399 30 94
161 760 30 122
162 625 30 122
163 746 30 122
164 612 30 122
165 762 30 122
166 625 30 122
167 783 30 122
168 637 30 122
169 778 30 122
170 640 30 122
171 778 30 122
172 638 30 122
173 791 30 122
174 638 30 122
175 782 30 122
176 635 30 122
177 792 30 122
178 640 30 122
179 783 30 122
180 637 30 122
181 774 30 122
182 622 30 122
183 777 30 122
184 618 30 122
185 777 30 122
186 622 30 122
187 765 30 122
188 623 30 122
189 769 30 122
190 625 30 122
191 775 30 122
192 622 30 122
193 777 30 122
194 628 30 122
195 769 30 122
196 620 30 122
197 778 30 122
198 623 30 122
199 788 30 122
200 634 30 122
201 457 40 38
202 416 40 38
203 460 40 38
204 438 40 38
205 465 40 38
206 441 40 38
207 467 40 38
208 442 40 38
209 473 40 38
210 452 40 38
211 469 40 38
212 446 40 38
213 478 40 38
214 450 40 38
215 476 40 38
216 454 40 38
217 479 40 38
218 452 40 38
219 480 40 38
220 450 40 38
221 481 40 38
222 443 40 38
223 476 40 38
224 447 40 38
225 472 40 38
226 450 40 38
227 479 40 38
228 449 40 38
229 478 40 38
230 455 40 38
231 478 40 38
232 457 40 38
233 481 40 38
234 447 40 38
235 504 40 38
236 452 40 38
237 472 40 38
238 447 40 38
239 472 40 38
240 451 40 38
241 622 40 66
242 377 40 66
243 619 40 66
244 378 40 66
245 622 40 66
246 369 40 66
247 616 40 66
248 374 40 66
249 619 40 66
250 374 40 66
251 616 40 66
252 374 40 66
253 621 40 66
254 375 40 66
255 618 40 66
256 397 40 66
257 633 40 66
258 406 40 66
259 652 40 66
260 412 40 66
261 652 40 66
262 419 40 66
263 658 40 66
264 423 40 66
265 659 40 66
266 409 40 66
267 650 40 66
268 405 40 66
269 653 40 66
270 405 40 66
271 652 40 66
272 403 40 66
273 656 40 66
274 408 40 66
275 644 40 66
276 406 40 66
277 649 40 66
278 412 40 66
279 650 40 66
280 406 40 66
281 853 40 122
282 330 40 122
283 859 40 122
284 323 40 122
285 842 40 122
286 308 40 122
287 842 40 122
288 324 40 122
289 831 40 122
290 334 40 122
291 838 40 122
292 341 40 122
293 836 40 122
294 328 40 122
295 840 40 122
296 324 40 122
297 836 40 122
298 321 40 122
299 831 40 122
300 328 40 122
301 833 40 122
302 328 40 122
303 840 40 122
304 330 40 122
305 831 40 122
306 321 40 122
307 833 40 122
308 328 40 122
309 833 40 122
310 321 40 122
311 840 40 122
312 319 40 122
313 838 40 122
314 317 40 122
315 831 40 122
316 319 40 122
317 827 40 122
318 323 40 122
319 836 40 122
320 328 40 122
321 442 40 38
322 407 40 38
323 437 40 38
324 410 40 38
325 444 40 38
326 412 40 38
327 440 40 38
328 414 40 38
329 439 40 38
330 413 40 38
331 436 40 38
332 416 40 38
333 446 40 38
334 412 40 38
335 438 40 38
336 414 40 38
337 443 40 38
338 408 40 38
339 446 40 38
340 407 40 38
341 445 40 38
342 413 40 38
343 453 40 38
344 414 40 38
345 449 40 38
346 417 40 38
347 447 40 38
348 411 40 38
349 443 40 38
350 417 40 38
351 447 40 38
352 410 40 38
353 449 40 38
354 409 40 38
355 442 40 38
356 413 40 38
357 451 40 38
358 412 40 38
359 447 40 38
360 420 40 38
361 526 40 66
362 467 40 66
363 532 40 66
364 470 40 66
365 528 40 66
366 474 40 66
367 529 40 66
368 472 40 66
369 533 40 66
370 480 40 66
371 542 40 66
372 487 40 66
373 545 40 66
374 504 40 66
375 549 40 66
376 507 40 66
377 546 40 66
378 517 40 66
379 541 40 66
380 518 40 66
381 554 40 66
382 514 40 66
383 564 40 66
384 514 40 66
385 571 40 66
386 522 40 66
387 575 40 66
388 525 40 66
389 582 40 66
390 533 40 66
391 588 40 66
392 536 40 66
393 591 40 66
394 553 40 66
395 592 40 66
396 557 40 66
397 592 40 66
398 563 40 66
399 583 40 66
400 568 40 66
> dadosc <- summarySE(dados, measurevar="Fres", groupvars=c("Vc","Lu"))
> dadosc
Vc Lu N Fres sd se ci
1 30 10 80 440.6875 30.91540 3.456447 6.879885
2 30 94 80 445.0250 150.97028 16.878990 33.596789
3 30 122 40 701.7000 75.06688 11.869115 24.007552
4 40 38 80 444.6125 23.31973 2.607225 5.189552
5 40 66 80 526.7125 90.77824 10.149316 20.201707
6 40 122 40 581.1250 259.74092 41.068645 83.069175
> ggplot(dadosc, aes(x=Lu, y=Fres, colour=Vc)) +
+ geom_errorbar(aes(ymin=Fres-se, ymax=Fres+se), width=5) +
+ geom_point()
> pd <- position_dodge(0.1)
Up to here I got this graph, very close to my desired graph, except for the fact I´d like a legend with only two colors, one for Vc=30 and other for Vc=40.
![enter image description here][1]
Then I try the following script:
ggplot(dadosc, aes(x=Lu, y=Fres, ymax = max(Fres), colour=Vc, group=Vc)) +
+ geom_errorbar(aes(ymin=Fres-se, ymax=Fres+se), colour="black", width=.1, position=pd) +
+ geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
+ xlab("Machining lenght (mm)") +
+ ylab("Machining forces (N)") +
+ scale_colour_hue(name="Cutting Velocity",
+ breaks=c("30", "40"),
+ labels=c("Vc = 30 m/min", " Vc = 40 m/min "),
+ l=40) +
+ ggtitle("The Effect of Cutting Velocity on Machining Forces") +
+ expand_limits(y=0) +
+ scale_y_continuous(breaks=0:750*50) +
+ theme_bw() +
+ theme(legend.justification=c(1,0),
+ legend.position=c(1,0))
Error: Continuous value supplied to discrete scale
And I receive this message:
"Error: Continuous value supplied to discrete scale"!
Vc should be a factor if you want two values in the legend. You were getting that error because you were trying to scale Vc as discrete (breaks = c(30, 40)) when it was of type integer
ggplot(dadosc, aes(x=Lu, y=Fres, colour=factor(Vc))) +
...

Technical error of measurement in between two columns

I have the following data frame:
data_2
sex age seca1 chad1 DL alog1 dig1 scifirst1 crimetech1
1 F 19 1800 1797 180 70 69 421 424
2 F 19 1682 1670 167 69 69 421 423
3 F 21 1765 1765 178 80 81 421 423
4 F 21 1829 1833 181 74 72 421 419
5 F 21 1706 1705 170 103 101 439 440
6 F 18 1607 1606 160 76 76 440 439
7 F 19 1578 1576 156 50 48 422 422
8 F 19 1577 1575 156 61 61 439 441
9 F 21 1666 1665 166 52 51 439 441
10 F 17 1710 1716 172 65 65 420 420
11 F 28 1616 1619 161 66 65 426 428
12 F 22 1648 1644 165 58 57 426 429
13 F 19 1569 1570 155 55 54 419 420
14 F 19 1779 1777 177 55 54 422 422
15 M 18 1773 1772 179 70 69 420 419
16 M 18 1816 1809 181 81 80 442 440
17 M 19 1766 1765 178 77 76 425 425
18 M 19 1745 1741 174 76 76 421 423
19 M 18 1716 1714 170 71 70 445 446
20 M 21 1785 1783 179 64 63 446 445
21 M 19 1850 1854 185 71 72 422 421
22 M 31 1875 1880 188 95 95 419 420
23 M 26 1877 1877 186 106 106 420 420
24 M 19 1836 1837 185 100 100 426 423
25 M 18 1825 1823 182 85 85 444 439
26 M 19 1755 1754 174 79 78 420 419
27 M 26 1658 1658 165 69 69 421 421
28 M 20 1816 1818 183 84 83 439 440
29 M 18 1755 1755 175 67 67 429 422
I wish to compute the technical error measurement (TEM) between " alog1 " and " dig1 ", which has the following formula:
TEM= √(D/2n)
Where D is the sum of the differences between alog1 and dig1 squared and n is 29
I'm not sure how to compute the sum of the differences squared between the two columns in the first place. Please help.
Probably with
n <- 29
TEM <- sqrt((data_2$alog1-data_2$dig1)^2/2*n)
data_3 <- cbind(data_2, TEM) #To bind it to the table and create the output table 3
Check the formula of TEM maybe I didn't understand it correctly.

Resources