Successive prime numbers in R [duplicate] - r

This question already has answers here:
What does the diff() function in R do? [closed]
(2 answers)
Closed 5 years ago.
I'm using RStudio and am pretty new to R. I have a dataset that shows the prime numbers from 1- 301. How do you use the diff function to compute the differences between successive primes?
Here is my dataset:
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
[31] 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
[61] 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
[91] 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
[121] 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863
[151] 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
[181] 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291
[211] 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511
[241] 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733
[271] 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987
[301] 1993 1997 1999 2003
Would appreciate some help, thanks!

You simply call
diff(primes)
For a simple dataset:
> primes <- c(2,3,5,7)
> diff(primes)
[1] 1 2 2

Related

Choosing rows from a matrix in a specific order

I have a matrix of order 2192*23, I want to choose the rows number "3rd", "4th", "10th", "11th", "17th", "18th", "24th", "25th" and so on
[3,4,10,11,17,18,24,25,31,32,38,39,45,46,...]
continue till the last row in the above order. how I will do this in R?
a1 <- c(3, 4)
d <- 7
n <- 10
S1 <- a1[1] + d * (seq(n) - 1)
S2 <- a1[2] + d * (seq(n) - 1)
sort(c(S1, S2))
#> [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67
library(magrittr)
sapply(a1, function(x) (x + d * (seq(n) - 1))) %>%
matrix(ncol = 1) %>%
sort
#> [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67
Created on 2022-01-18 by the reprex package (v2.0.1)
We can use outer + seq
> c(outer(0:1, seq(3, 2192, by = 7), `+`))
[1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52
[16] 53 59 60 66 67 73 74 80 81 87 88 94 95 101 102
[31] 108 109 115 116 122 123 129 130 136 137 143 144 150 151 157
[46] 158 164 165 171 172 178 179 185 186 192 193 199 200 206 207
[61] 213 214 220 221 227 228 234 235 241 242 248 249 255 256 262
[76] 263 269 270 276 277 283 284 290 291 297 298 304 305 311 312
[91] 318 319 325 326 332 333 339 340 346 347 353 354 360 361 367
[106] 368 374 375 381 382 388 389 395 396 402 403 409 410 416 417
[121] 423 424 430 431 437 438 444 445 451 452 458 459 465 466 472
[136] 473 479 480 486 487 493 494 500 501 507 508 514 515 521 522
[151] 528 529 535 536 542 543 549 550 556 557 563 564 570 571 577
[166] 578 584 585 591 592 598 599 605 606 612 613 619 620 626 627
[181] 633 634 640 641 647 648 654 655 661 662 668 669 675 676 682
[196] 683 689 690 696 697 703 704 710 711 717 718 724 725 731 732
[211] 738 739 745 746 752 753 759 760 766 767 773 774 780 781 787
[226] 788 794 795 801 802 808 809 815 816 822 823 829 830 836 837
[241] 843 844 850 851 857 858 864 865 871 872 878 879 885 886 892
[256] 893 899 900 906 907 913 914 920 921 927 928 934 935 941 942
[271] 948 949 955 956 962 963 969 970 976 977 983 984 990 991 997
[286] 998 1004 1005 1011 1012 1018 1019 1025 1026 1032 1033 1039 1040 1046 1047
[301] 1053 1054 1060 1061 1067 1068 1074 1075 1081 1082 1088 1089 1095 1096 1102
[316] 1103 1109 1110 1116 1117 1123 1124 1130 1131 1137 1138 1144 1145 1151 1152
[331] 1158 1159 1165 1166 1172 1173 1179 1180 1186 1187 1193 1194 1200 1201 1207
[346] 1208 1214 1215 1221 1222 1228 1229 1235 1236 1242 1243 1249 1250 1256 1257
[361] 1263 1264 1270 1271 1277 1278 1284 1285 1291 1292 1298 1299 1305 1306 1312
[376] 1313 1319 1320 1326 1327 1333 1334 1340 1341 1347 1348 1354 1355 1361 1362
[391] 1368 1369 1375 1376 1382 1383 1389 1390 1396 1397 1403 1404 1410 1411 1417
[406] 1418 1424 1425 1431 1432 1438 1439 1445 1446 1452 1453 1459 1460 1466 1467
[421] 1473 1474 1480 1481 1487 1488 1494 1495 1501 1502 1508 1509 1515 1516 1522
[436] 1523 1529 1530 1536 1537 1543 1544 1550 1551 1557 1558 1564 1565 1571 1572
[451] 1578 1579 1585 1586 1592 1593 1599 1600 1606 1607 1613 1614 1620 1621 1627
[466] 1628 1634 1635 1641 1642 1648 1649 1655 1656 1662 1663 1669 1670 1676 1677
[481] 1683 1684 1690 1691 1697 1698 1704 1705 1711 1712 1718 1719 1725 1726 1732
[496] 1733 1739 1740 1746 1747 1753 1754 1760 1761 1767 1768 1774 1775 1781 1782
[511] 1788 1789 1795 1796 1802 1803 1809 1810 1816 1817 1823 1824 1830 1831 1837
[526] 1838 1844 1845 1851 1852 1858 1859 1865 1866 1872 1873 1879 1880 1886 1887
[541] 1893 1894 1900 1901 1907 1908 1914 1915 1921 1922 1928 1929 1935 1936 1942
[556] 1943 1949 1950 1956 1957 1963 1964 1970 1971 1977 1978 1984 1985 1991 1992
[571] 1998 1999 2005 2006 2012 2013 2019 2020 2026 2027 2033 2034 2040 2041 2047
[586] 2048 2054 2055 2061 2062 2068 2069 2075 2076 2082 2083 2089 2090 2096 2097
[601] 2103 2104 2110 2111 2117 2118 2124 2125 2131 2132 2138 2139 2145 2146 2152
[616] 2153 2159 2160 2166 2167 2173 2174 2180 2181 2187 2188
Using vector recycling -
ind <- seq_len(nrow(mat))
#ind <- seq_len(2192)
ind[c(F, F, T, T, F, F, F)]
#[1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52
#[16] 53 59 60 66 67 73 74 80 81 87 88 94 95 101 102
#...
#...
Another way is to use sequence
n = 2192/7
s <- sequence(nvec = rep(2, n), from = (7*1:n)-4)
# [1] 3 4 10 11 17 18 24 25 31 32 38 39 45 46 52 53 59 60 66 67 73 74
# ...

why is this butterworth filter presenting different results in R and Matlab?

I'm trying to use a 20Hz low pass filter on data in R, but when I use the filtfilt function, the plot is different from the matlab.
I'm using the following code in R:
fc<-20
fs<-100
Wn<-pi*fc/(2*fs)
testar<- butter(5, Wn, type="low")
L2<- signal::filtfilt(testar,Tabela$posicao)
plot(Tabela$tempo, L2, type = "l", col="red")
The matlab code is:
fc=20;
fs=100;
Wn=pi*fc/(2*fs);
[b,a] = butter(5,Wn,'low');
posfilt= filtfilt(b,a,Tabela.posicao);
The plot in matlab is:
The R one:
why the R one is presenting those variation in the begin and in the end of the graph?
Data can be produced as follows:
Tabela <- data.table::fread("
tempo posicao
0 870.22
1 870.27
2 870.33
3 870.39
4 870.46
5 870.52
6 870.57
7 870.61
8 870.63
9 870.65
10 870.66
11 870.68
12 870.7
13 870.73
14 870.76
15 870.79
16 870.81
17 870.82
18 870.83
19 870.83
20 870.83
21 870.84
22 870.85
23 870.85
24 870.85
25 870.83
26 870.79
27 870.74
28 870.69
29 870.63
30 870.59
31 870.57
32 870.56
33 870.55
34 870.53
35 870.51
36 870.46
37 870.42
38 870.37
39 870.33
40 870.31
41 870.3
42 870.3
43 870.31
44 870.31
45 870.31
46 870.33
47 870.36
48 870.42
49 870.52
50 870.64
51 870.77
52 870.87
53 870.92
54 870.91
55 870.82
56 870.68
57 870.51
58 870.37
59 870.27
60 870.25
61 870.29
62 870.38
63 870.5
64 870.61
65 870.69
66 870.74
67 870.76
68 870.76
69 870.75
70 870.74
71 870.74
72 870.76
73 870.78
74 870.81
75 870.86
76 870.93
77 871.02
78 871.12
79 871.23
80 871.33
81 871.42
82 871.47
83 871.5
84 871.52
85 871.52
86 871.54
87 871.57
88 871.62
89 871.67
90 871.71
91 871.73
92 871.72
93 871.68
94 871.64
95 871.59
96 871.58
97 871.59
98 871.62
99 871.66
100 871.7
101 871.7
102 871.69
103 871.65
104 871.6
105 871.56
106 871.54
107 871.52
108 871.52
109 871.5
110 871.48
111 871.43
112 871.38
113 871.31
114 871.24
115 871.17
116 871.12
117 871.07
118 871.02
119 870.99
120 870.97
121 870.97
122 870.98
123 871.00
124 871.02
125 871.04
126 871.04
127 871.02
128 870.97
129 870.91
130 870.84
131 870.78
132 870.74
133 870.72
134 870.72
135 870.72
136 870.72
137 870.71
138 870.69
139 870.68
140 870.69
141 870.72
142 870.77
143 870.84
144 870.92
145 871.01
146 871.1
147 871.19
148 871.28
149 871.36
150 871.43
151 871.49
152 871.55
153 871.6
154 871.67
155 871.74
156 871.84
157 871.95
158 872.07
159 872.2
160 872.31
161 872.42
162 872.51
163 872.59
164 872.66
165 872.75
166 872.86
167 873.02
168 873.22
169 873.48
170 873.8
171 874.16
172 874.55
173 874.99
174 875.49
175 876.06
176 876.72
177 877.48
178 878.36
179 879.33
180 880.41
181 881.59
182 882.87
183 884.24
184 885.71
185 887.29
186 888.96
187 890.73
188 892.61
189 894.57
190 896.63
191 898.77
192 900.99
193 903.28
194 905.63
195 908.02
196 910.44
197 912.88
198 915.33
199 917.79
200 920.25
201 922.71
202 925.15
203 927.57
204 929.96
205 932.3
206 934.59
207 936.82
208 938.99
209 941.09
210 943.14
211 945.12
212 947.05
213 948.89
214 950.62
215 952.2
216 953.62
217 954.86
218 955.94
219 956.86
220 957.65
221 958.33
222 958.9
223 959.4
224 959.83
225 960.2
226 960.53
227 960.82
228 961.09
229 961.35
230 961.58
231 961.81
232 962.02
233 962.23
234 962.45
235 962.7
236 962.98
237 963.32
238 963.7
239 964.13
240 964.6
241 965.09
242 965.59
243 966.09
244 966.59
245 967.1
246 967.62
247 968.15
248 968.69
249 969.25
250 969.81
251 970.36
252 970.89
253 971.4
254 971.89
255 972.33
256 972.73
257 973.08
258 973.38
259 973.63
260 973.85
261 974.05
262 974.25
263 974.44
264 974.63
265 974.8
266 974.96
267 975.1
268 975.24
269 975.37
270 975.5
271 975.64
272 975.8
273 975.96
274 976.13
275 976.32
276 976.52
277 976.74
278 976.97
279 977.21
280 977.44
281 977.66
282 977.84
283 977.97
284 978.05
285 978.06
286 978.01
287 977.9
288 977.74
289 977.53
290 977.28
291 976.99
292 976.67
293 976.34
294 976.01
295 975.68
296 975.35
297 975.02
298 974.68
299 974.31
300 973.91
301 973.48
302 973.04
303 972.58
304 972.14
305 971.71
306 971.32
307 970.97
308 970.67
309 970.41
310 970.2
311 970.02
312 969.89
313 969.78
314 969.72
315 969.68
316 969.67
317 969.67
318 969.67
319 969.67
320 969.67
321 969.68
322 969.69
323 969.73
324 969.79
325 969.88
326 969.98
327 970.08
328 970.17
329 970.24
330 970.28
331 970.29
332 970.27
333 970.22
334 970.15
335 970.07
336 969.98
337 969.89
338 969.81
339 969.74
340 969.68
341 969.63
342 969.6
343 969.57
344 969.56
345 969.55
346 969.57
347 969.6
348 969.65
349 969.73
350 969.81
351 969.89
352 969.96
353 970.01
354 970.05
355 970.06
356 970.07
357 970.08
358 970.09
359 970.09
360 970.09
361 970.08
362 970.06
363 970.04
364 970.00
365 969.96
366 969.94
367 969.93
368 969.95
369 970.00
370 970.08
371 970.17
372 970.27
373 970.35
374 970.42
375 970.48
376 970.53
377 970.58
378 970.64
379 970.73
380 970.85
381 970.98
382 971.14
383 971.3
384 971.45
385 971.58
386 971.69
387 971.76
388 971.79
389 971.8
390 971.78
391 971.75
392 971.71
393 971.66
394 971.61
395 971.55
396 971.48
397 971.39
398 971.3
399 971.2
400 971.1
401 971.00
402 970.9
403 970.82
404 970.76
405 970.73
406 970.72
407 970.73
408 970.77
409 970.83
410 970.9
411 970.98
412 971.06
413 971.16
414 971.27
415 971.4
416 971.53
417 971.67
418 971.81
419 971.94
420 972.06
421 972.17
422 972.25
423 972.33
424 972.38
425 972.42
426 972.45
427 972.45
428 972.44
429 972.42
430 972.38
431 972.34
432 972.29
433 972.24
434 972.2
435 972.16
436 972.12
437 972.1
438 972.08
439 972.07
440 972.07
441 972.07
442 972.07
443 972.08
444 972.09
445 972.12
446 972.18
447 972.26
448 972.37
449 972.49
450 972.61
451 972.7
452 972.78
453 972.82
454 972.83
455 972.82
456 972.79
457 972.76
458 972.71
459 972.65
460 972.57
461 972.49
462 972.39
463 972.29
464 972.19
465 972.11
466 972.07
467 972.05
468 972.07
469 972.1
470 972.14
471 972.17
472 972.19
473 972.2
474 972.21
475 972.22
476 972.25
477 972.29
478 972.36
479 972.44
480 972.52
481 972.61
482 972.68
483 972.74
484 972.78
485 972.81
486 972.83
487 972.85
488 972.86
489 972.88
490 972.9
491 972.92
492 972.95
493 972.97
494 972.99
495 973.00
496 972.99
497 972.97
498 972.93
499 972.88
500 972.83
501 972.78
502 972.73
503 972.69
504 972.66
505 972.64
506 972.64
507 972.66
508 972.7
509 972.76
510 972.83
511 972.92
512 973.02
513 973.13
514 973.25
515 973.39
516 973.56
517 973.74
518 973.94
519 974.14
520 974.34
521 974.52
522 974.68
523 974.82
524 974.94
525 975.06
526 975.18
527 975.3
528 975.43
529 975.58
530 975.73
531 975.88
532 976.02
533 976.15
534 976.27
535 976.4
536 976.53
537 976.67
538 976.82
539 976.99
540 977.17
541 977.35
542 977.53
543 977.71
544 977.88
545 978.03
546 978.18
547 978.31
548 978.44
549 978.55
550 978.63
551 978.69
552 978.72
553 978.73
554 978.73
555 978.72
556 978.71
557 978.69
558 978.67
559 978.62
560 978.54
561 978.41
562 978.22
563 977.96
564 977.62
565 977.19
566 976.67
567 976.05
568 975.32
569 974.47
570 973.48
571 972.34
572 971.03
573 969.52
574 967.79
575 965.83
576 963.64
577 961.2
578 958.52
579 955.62
580 952.5
581 949.16
582 945.6
583 941.83
584 937.85
585 933.68
586 929.33
587 924.8
588 920.12
589 915.3
590 910.35
591 905.29
592 900.13
593 894.88
594 889.56
595 884.18
596 878.76
597 873.31
598 867.84
599 862.37
600 856.93
601 851.52
602 846.16
603 840.86
604 835.64
605 830.48
606 825.41
607 820.4
608 815.46
609 810.57
610 805.74
611 800.96
612 796.25
613 791.59
614 786.99
615 782.46
616 777.99
617 773.57
618 769.2
619 764.89
620 760.64
621 756.45
622 752.32
623 748.25
624 744.24
625 740.31
626 736.46
627 732.69
628 729.03
629 725.5
630 722.1
631 718.83
632 715.7
633 712.68
634 709.77
635 706.96
636 704.25
637 701.63
638 699.13
639 696.75
640 694.49
641 692.36
642 690.34
643 688.42
644 686.6
645 684.85
646 683.17
647 681.56
648 680.01
649 678.52
650 677.1
651 675.75
652 674.49
653 673.3
654 672.19
655 671.15
656 670.16
657 669.22
658 668.33
659 667.5
660 666.74
661 666.05
662 665.42
663 664.85
664 664.32
665 663.82
666 663.35
667 662.93
668 662.57
669 662.27
670 662.05
671 661.89
672 661.77
673 661.69
674 661.62
675 661.56
676 661.5
677 661.44
678 661.38
679 661.34
680 661.29
681 661.25
682 661.2
683 661.13
684 661.05
685 660.95
686 660.83
687 660.7
688 660.57
689 660.43
690 660.28
691 660.13
692 659.96
693 659.78
694 659.6
695 659.43
696 659.29
697 659.2
698 659.16
699 659.19
700 659.28
701 659.43
702 659.65
703 659.96
704 660.37
705 660.9
706 661.54
707 662.31
708 663.19
709 664.2
710 665.33
711 666.58
712 667.94
713 669.43
714 671.02
715 672.73
716 674.55
717 676.46
718 678.46
719 680.55
720 682.73
721 685.00
722 687.36
723 689.81
724 692.34
725 694.92
726 697.54
727 700.15
728 702.73
729 705.28
730 707.79
731 710.27
732 712.76
733 715.26
734 717.8
735 720.38
736 722.98
737 725.6
738 728.21
739 730.81
740 733.39
741 735.96
742 738.5
743 741.02
744 743.52
745 746.00
746 748.45
747 750.87
748 753.25
749 755.58
750 757.87
751 760.12
752 762.34
753 764.53
754 766.71
755 768.86
756 770.99
757 773.09
758 775.16
759 777.2
760 779.23
761 781.24
762 783.25
763 785.26
764 787.28
765 789.3
766 791.31
767 793.33
768 795.34
769 797.35
770 799.35
771 801.34
772 803.33
773 805.31
774 807.29
775 809.26
776 811.21
777 813.16
778 815.09
779 817.03
780 818.96
781 820.91
782 822.88
783 824.85
784 826.82
785 828.78
786 830.73
787 832.67
788 834.59
789 836.5
790 838.41
791 840.33
792 842.27
793 844.23
794 846.2
795 848.18
796 850.15
797 852.1
798 854.02
799 855.93
800 857.84
801 859.76
802 861.71
803 863.69
804 865.69
805 867.72
806 869.75
807 871.79
808 873.83
809 875.88
810 877.94
811 880.02
812 882.12
813 884.25
814 886.41
815 888.59
816 890.78
817 892.97
818 895.18
819 897.39
820 899.61
821 901.85
822 904.11
823 906.38
824 908.67
825 910.97
826 913.29
827 915.61
828 917.94
829 920.28
830 922.63
831 925.00
832 927.38
833 929.79
834 932.22
835 934.68
836 937.17
837 939.67
838 942.17
839 944.67
840 947.15
841 949.62
842 952.08
843 954.51
844 956.94
845 959.36
846 961.75
847 964.12
848 966.45
849 968.73
850 970.94
851 973.07
852 975.12
853 977.08
854 978.94
855 980.7
856 982.34
857 983.86
858 985.26
859 986.52
860 987.65
861 988.64
862 989.49
863 990.2
864 990.76
865 991.16
866 991.42
867 991.52
868 991.48
869 991.3
870 991.01
871 990.63
872 990.18
873 989.67
874 989.13
875 988.56
876 987.98
877 987.39
878 986.79
879 986.2
880 985.61
881 985.04
882 984.52
883 984.05
884 983.65
885 983.32
886 983.07
887 982.88
888 982.74
889 982.64
890 982.55
891 982.47
892 982.38
893 982.28
894 982.15
895 981.98
896 981.78
897 981.54
898 981.26
899 980.94
900 980.61
901 980.28
902 979.94
903 979.61
904 979.29
905 978.98
906 978.68
907 978.39
908 978.11
909 977.85
910 977.6
911 977.37
912 977.16
913 976.94
914 976.72
915 976.5
916 976.27
917 976.06
918 975.85
919 975.67
920 975.5
921 975.36
922 975.22
923 975.08
924 974.93
925 974.76
926 974.57
927 974.35
928 974.1
929 973.85
930 973.6
931 973.36
932 973.13
933 972.93
934 972.74
935 972.55
936 972.37
937 972.19
938 972.00
939 971.8
940 971.6
941 971.39
942 971.18
943 970.97
944 970.76
945 970.56
946 970.37
947 970.19
948 970.02
949 969.86
950 969.72
951 969.6
952 969.5
953 969.42
954 969.36
955 969.33
956 969.29
957 969.27
958 969.23
959 969.19
960 969.14
961 969.09
962 969.04
963 968.99
964 968.94
965 968.88
966 968.82
967 968.74
968 968.64
969 968.54
970 968.42
971 968.3
972 968.19
973 968.08
974 967.98
975 967.86
976 967.74
977 967.59
978 967.42
979 967.24
980 967.04
981 966.85
982 966.67
983 966.5
984 966.35
985 966.2
986 966.06
987 965.92
988 965.77
989 965.61
990 965.44
991 965.25
992 965.05
993 964.82
994 964.58
995 964.32
996 964.05
997 963.78
998 963.52
999 963.28
1000 963.06
1001 962.85
1002 962.65
1003 962.44
1004 962.18
1005 961.87
1006 961.49
1007 961.03
1008 960.49
1009 959.91
1010 959.32
1011 958.75
1012 958.23
1013 957.77
1014 957.33
1015 956.9
1016 956.43
1017 955.87
1018 955.19
1019 954.37
1020 953.43
1021 952.39
1022 951.28
1023 950.13
1024 948.96
1025 947.74
1026 946.48
1027 945.15
1028 943.74
1029 942.26
1030 940.72
1031 939.11
1032 937.45
1033 935.74
1034 933.95
1035 932.07
1036 930.11
1037 928.06
1038 925.97
1039 923.92
1040 921.98
1041 920.24
1042 918.75
1043 917.51
1044 916.51
1045 915.7
1046 915.04
1047 914.51
1048 914.1
1049 913.76
1050 913.44
1051 913.05
1052 912.52
1053 911.79
1054 910.86
1055 909.74
1056 908.49
1057 907.19
1058 905.91
1059 904.73
1060 903.71
1061 902.89
1062 902.28
1063 901.88
1064 901.66
1065 901.59
1066 901.65
1067 901.81
1068 902.03
1069 902.3
1070 902.56
1071 902.79
1072 902.96
1073 903.06
1074 903.09
1075 903.06
1076 902.97
1077 902.85
1078 902.7
1079 902.53
1080 902.36
1081 902.21
1082 902.07
1083 901.95
1084 901.83
1085 901.67
1086 901.46
1087 901.17
1088 900.77
1089 900.26
1090 899.61
1091 898.81
1092 897.85
1093 896.73
1094 895.47
1095 894.12
1096 892.74
1097 891.4
1098 890.16
1099 889.04
1100 888.02
1101 887.1
1102 886.26
1103 885.5
1104 884.81
1105 884.15
1106 883.45
1107 882.61
1108 881.56
1109 880.29
1110 878.88
1111 877.44
1112 876.11
1113 875.01
1114 874.2
1115 873.65
1116 873.28
1117 872.99
1118 872.69
1119 872.36
1120 872.02
1121 871.74
1122 871.56
1123 871.5
1124 871.53
1125 871.6
1126 871.62
1127 871.58
1128 871.45
1129 871.26
1130 871.06
1131 870.9
1132 870.81
1133 870.82
1134 870.92
1135 871.06
1136 871.21
1137 871.32
1138 871.36
1139 871.33
1140 871.24
1141 871.14
1142 871.08
1143 871.08
1144 871.15
1145 871.28
1146 871.43
1147 871.56
1148 871.62
1149 871.6
1150 871.51
1151 871.37
1152 871.2
1153 871.04
1154 870.89
1155 870.77
1156 870.66
1157 870.55
1158 870.44
1159 870.32
1160 870.22
1161 870.13
1162 870.08
1163 870.06
1164 870.07
1165 870.09
1166 870.12
1167 870.14
1168 870.13
1169 870.11
1170 870.08
1171 870.05
1172 870.03
1173 870.03
1174 870.04
1175 870.04
1176 870.03
1177 869.99
1178 869.93
1179 869.87
1180 869.83
1181 869.81
1182 869.83
1183 869.88
1184 869.94
1185 870.00
1186 870.03
1187 870.03
1188 870.02
1189 870.00
1190 870.00
1191 870.00
1192 870.03
1193 870.06
1194 870.1
1195 870.14
1196 870.17
1197 870.2
1198 870.24
1199 870.28
1200 870.33
1201 870.37
1202 870.39
1203 870.39
1204 870.36
1205 870.31
1206 870.24
1207 870.18
1208 870.13
1209 870.09
1210 870.05
1211 870.01
1212 869.95
1213 869.88
1214 869.81
1215 869.75
1216 869.72
1217 869.73
1218 869.77
1219 869.85
1220 869.93
1221 870.01
1222 870.06
1223 870.1
1224 870.11
1225 870.11
1226 870.11
1227 870.11
1228 870.11
1229 870.12
1230 870.14
1231 870.16")
I have hunch that the difference is in how each version handles end-effect transients.
Your signal has a large DC-offset (~875). If you think of the signal as being zero 0 before and after the recording. The jump at the start of the signal gets processed by the filter and is seen as an artifact or end-effect. These end-effects are what you see in the R version of the filtered signal.
From the R documentation from filtfilt this version is old and likely doesn't minimize the end transients (R 'filtfilt' docs). On the other hand the MATLAB version of filtfilt does; Quoting from the MATLAB documentation:
"filtfilt minimizes start-up and ending transients by matching initial conditions. Do not use 'filtfilt' with differentiator and Hilbert FIR filters, because the operation of these filters depends heavily on their phase response." FILTFILT Documentation
As mentioned by Azim, the default implementation of signal::filtfilt() does not include any steps to remove end-effect transients. However, a very simple function that pads the series with a reversed values before/after and then subsets the result to the original range of interest can solve this problem.
EndEffect <- function(filt,x) {
signal::filtfilt(filt,c(rev(x),x,rev(x)))[(length(x) + 1):(2 * length(x))]
}
L2<- EndEffect(testar,Tabela$posicao)
plot(Tabela$tempo, L2, type = "l", col="red")

How can I swap default colors in heatmap function in R?

I have created following heatmap of days in the week and hours in a day;
This is table of values, from which was the map created;
0 1 2 3 4 5 6 7 8 9 10 11 12 13
nedeľa 2028 1236 1019 838 607 461 478 483 615 864 884 787 1192 789
piatok 1873 932 743 560 473 602 839 1203 1268 1286 938 822 1207 857
pondelok 1900 825 712 527 415 542 772 1123 1323 1235 971 737 1129 824
sobota 2050 1267 985 836 652 508 541 650 858 1039 946 789 1204 767
streda 1814 790 619 469 396 561 862 1140 1329 1237 947 763 1225 804
štvrtok 1856 816 696 508 400 534 799 1135 1298 1301 932 731 1093 752
utorok 1691 777 603 464 414 520 845 1118 1175 1174 948 786 1108 762
14 15 16 17 18 19 20 21 22 23
nedeľa 959 1037 1083 1160 1389 1342 1706 1696 2079 1584
piatok 937 1140 1165 1318 1623 1652 1736 1881 2308 1921
pondelok 958 1059 1136 1252 1518 1503 1622 1815 2009 1490
sobota 963 1086 1055 1084 1348 1390 1570 1702 2078 1750
streda 863 1075 1076 1289 1580 1507 1718 1748 2093 1511
štvrtok 831 1044 1131 1258 1510 1537 1668 1776 2134 1579
utorok 908 1071 1090 1274 1553 1496 1696 1816 2044 1458
I wonder if there is some easy and elegant way how to swap color range, so that high values are represented by red color and other way around.
I've used this function;
heatmap (myMatrix, Colv=NA, Rowv=NA)
The default colors for the heatmap function are actually set by the image() function and are
col=heat.colors(12)
If you want to reverse them, just use pass
heatmap(..., col=rev(heat.colors(12)))
where ... are the rest of the parameters you need to pass.

Can I plot multiple rows on the same plot from a 2-way table

I have the following table, which gives the number of earthquakes in each year (row) by month (column).
> tmp=table(quakes$year,quakes$mon)
> tmp
0 1 2 3 4 5 6 7 8 9 10 11
1973 388 453 451 508 375 533 496 392 349 424 400 406
1974 386 384 385 388 456 414 491 501 385 432 354 420
1975 435 374 397 439 449 629 461 434 386 404 440 470
1976 677 478 474 430 612 514 561 533 600 485 463 481
1977 453 355 508 519 460 477 416 541 449 523 585 489
1978 499 449 730 533 550 578 524 480 535 458 526 566
1979 485 444 771 662 705 661 590 597 514 635 549 549
1980 530 530 668 654 969 779 668 472 452 614 549 463
1981 501 506 545 547 538 524 662 587 690 561 518 650
1982 655 527 632 602 630 658 603 639 640 761 628 772
1983 909 683 775 847 1028 743 823 902 727 770 793 842
1984 798 732 872 943 795 721 782 820 994 947 1056 1033
1985 1016 839 1140 1078 1146 989 1066 1136 1095 1115 1162 1333
1986 1050 867 1217 944 1368 1046 1256 1035 912 1086 1066 871
1987 834 860 1003 884 891 871 959 943 952 1022 1035 1036
1988 990 957 1127 1123 1121 975 1095 1160 929 1079 1092 1063
1989 1133 1106 1144 1297 1235 1060 1175 1312 1200 1458 1137 1305
1990 1247 1176 1404 1489 1431 1321 1713 1496 1160 1277 1307 1569
1991 1476 1226 1369 1388 1387 1380 1327 1378 1253 1530 1301 1469
1992 1362 1292 1622 1715 1915 1649 1941 1722 1518 1501 1653 1634
1993 1435 1428 1821 1691 1970 1767 2502 1957 1903 1852 1628 1522
1994 2095 1409 1466 1520 1760 1702 1473 1494 1625 1889 1673 1265
1995 1656 1590 1444 1798 1931 1691 1445 1574 1640 2005 1917 2316
1996 2297 2310 1513 1290 1335 1675 1545 1450 1615 1604 1690 1614
1997 1441 1570 1890 1919 1618 1269 1582 1463 1463 1645 1892 2120
1998 1905 1592 1773 2021 2068 1786 1971 1776 1724 1749 1761 1562
1999 1752 1740 2093 1713 2145 1891 1679 1628 1487 1799 1584 1321
2000 1451 1340 1587 1702 1710 1941 2221 2125 1724 1863 2735 1857
2001 1945 2007 1856 2091 1724 2091 2039 1915 1817 2124 1917 2008
2002 2101 1996 2291 2202 1981 2126 2001 2091 2733 2411 3316 2205
2003 2053 2139 2604 2475 2526 2950 2655 2841 3030 2794 2709 2643
2004 2680 2861 2866 2692 3157 2767 2090 2274 2313 2168 2449 2883
2005 3253 2096 2842 3028 2562 2492 2340 2215 2347 2887 2176 2245
2006 2086 2007 2509 2739 2738 2445 2548 2405 2157 2399 3128 2407
2007 2822 1954 2361 3206 2351 2257 2566 2779 2682 2324 2072 2311
2008 2333 2666 2732 2595 3303 3024 2743 2795 2096 2726 2337 2427
2009 1512 1266 1223 1171 1124 1158 1162 1355 1112 1623 1085 1034
2010 1371 1630 2032 2120 1402 1419 2747 1885 1548 1550 1651 2186
Then following two commands give me two different plots, the first for the 1973 time series and the second for the 2010 time series:
> dim(tmp)
[1] 38 12
> plot(tmp[1,], type="l")
> plot(tmp[38,], type="l")
I want to combine and show both of these time series on the same plot. Is there a way to plot rows from the table above on the same plot and at the same time identify each time series by the year (row label)?
matplot is good for this sort of thing:
Reverse your rows and columns of your table:
tmp <- table(quakes$mon,quakes$year)
# 1973 1974 1975 1976 1977 1978
#0 388 386 435 677 453 499
#1 453 384 374 478 355 449
#2 451 385 397 474 508 730
#3 508 388 439 430 519 533
#etc
Then use matplot:
vars <- c(1,6)
matplot(tmp[,vars], type="l", lty=1)
legend("topright", colnames(tmp)[vars], lty=1, col=seq_along(vars))
As a general rule I try not to plot using tables, even though it makes sense for a person to read the data that way.
library(ggplot2)
ggplot(data.frame(tmp)) +
geom_line(aes(x = Var2, y = Freq, group = Var1, col = Var1))
The ggplot2 library is great for this sort of group plotting exercise, though it can take a little bit of input to get used to.
It's probably a bad idea to call Var1 and Var2 (which are created when I coerce the table to a data.frame). You can avoid this by aggregating the quakes data frame first, then calling the plot on that.

R: kmeans on matrix (100x21)

I have a question about Kmeans in R. I have a dataframe like variable data_file. My question is, how can I perform kmeans on my data? If anyone has any suggestion, you are more than welcome. Thank you!
> data_file
WT_Sham WT_Sham.1 WT_Sham.2 WT_Sham.3 WT_Sham.4 WT_Sham.5 WT_CSD WT_CSD.1 WT_CSD.2 WT_CSD.3 RQ_Sham RQ_Sham.1 RQ_Sham.2 RQ_Sham.3 RQ_Sham.4 RQ_Sham.5 RQ_CSD RQ_CSD.1 RQ_CSD.2 RQ_CSD.3
ENSMUSG00000002012 581 1221 681 1789 376 787 1009 480 992 1004 582 896 1319 1200 663 1089 1003 821 807 696
ENSMUSG00000028182 2 11 3 8 2 8 1 3 12 3 1 5 35 13 0 1 8 13 5 1
ENSMUSG00000002017 382 698 555 1290 892 999 546 245 689 539 367 548 927 905 853 623 823 722 494 505
ENSMUSG00000028184 381 666 443 763 491 655 621 376 379 353 382 306 878 690 1787 257 776 636 240 564
ENSMUSG00000002015 402 956 533 1728 1224 1129 668 383 930 355 481 704 1611 1458 0 345 1199 1017 653 917
ENSMUSG00000028180 778 2158 1506 3606 2489 3128 1573 1030 1962 956 1093 1410 3702 3122 1 1433 2535 2125 1242 1825
Did you try the built in function kmeans?
kmeans(USArrests, centers=3)
USArrests is just a data set that comes with R.
If you google R kmeans you will get more information.

Resources