I have following data. i want to first find out the most occurring digit on every place value. Obviously one place can have 10 possibilities from 0 to 9. Than i want an option where by i can choose 5 top occurrences or 6 or 7 or 8 top occurrences for e.g. if i choose 5 then the program should take the top 5 occurrences or if i choose 8 then program should leave out the least 2 occurring digits and take all others.
Data example:
076060
693022
585821
980575
438068
766214
051726
060417
822591
015507
635576
180231
212238
417651
631269
720767
348344
532148
748085
474026
380897
512421
749492
423616
950330
930079
097759
638901
319356
683308
818127
880675
256095
639187
339904
945437
799571
466063
428853
397799
782034
462486
739342
879023
419264
793319
603131
315791
351701
151747
365656
982700
348093
793392
946875
912108
070001
780515
222468
345439
234846
227112
757243
341747
480781
906624
868265
388572
947873
898895
452518
738580
217342
849951
437382
247068
743776
562584
636948
049434
139296
688436
443629
I want option of choosing 5, 6,7 or 8 top occurrences and 2 or 3 or 4number combination
Expected results, 2 number combination basis top 8 occurrences and so on.
01
02
03
04
05
06
08
09
21
22
23
24
25
26
28
29
31
32
33
34
35
36
38
39
41
42
43
44
45
46
48
49
61
62
63
64
65
66
68
69
71
72
73
74
75
76
78
79
81
82
83
84
85
86
88
89
91
92
93
94
95
96
98
99
Expected results, 3 number combination basis top 8 occurrences and so on.
010
012
013
015
016
017
018
019
020
022
023
025
026
027
028
029
030
032
033
035
036
037
038
039
040
042
043
045
046
047
048
049
050
052
053
055
056
057
058
059
060
062
063
065
066
067
068
069
080
082
083
085
086
087
088
089
090
092
093
095
096
097
098
099
210
212
213
215
216
217
218
219
220
222
223
225
226
227
228
229
230
232
233
235
236
237
238
239
240
242
243
245
246
247
248
249
250
252
253
255
256
257
258
259
260
262
263
265
266
267
268
269
280
282
283
285
286
287
288
289
290
292
293
295
296
297
298
299
310
312
313
315
316
317
318
319
320
322
323
325
326
327
328
329
330
332
333
335
336
337
338
339
340
342
343
345
346
347
348
349
350
352
353
355
356
357
358
359
360
362
363
365
366
367
368
369
380
382
383
385
386
387
388
389
390
392
393
395
396
397
398
399
410
412
413
415
416
417
418
419
420
422
423
425
426
427
428
429
430
432
433
435
436
437
438
439
440
442
443
445
446
447
448
449
450
452
453
455
456
457
458
459
460
462
463
465
466
467
468
469
480
482
483
485
486
487
488
489
490
492
493
495
496
497
498
499
610
612
613
615
616
617
618
619
620
622
623
625
626
627
628
629
630
632
633
635
636
637
638
639
640
642
643
645
646
647
648
649
650
652
653
655
656
657
658
659
660
662
663
665
666
667
668
669
680
682
683
685
686
687
688
689
690
692
693
695
696
697
698
699
710
712
713
715
716
717
718
719
720
722
723
725
726
727
728
729
730
732
733
735
736
737
738
739
740
742
743
745
746
747
748
749
750
752
753
755
756
757
758
759
760
762
763
765
766
767
768
769
780
782
783
785
786
787
788
789
790
792
793
795
796
797
798
799
810
812
813
815
816
817
818
819
820
822
823
825
826
827
828
829
830
832
833
835
836
837
838
839
840
842
843
845
846
847
848
849
850
852
853
855
856
857
858
859
860
862
863
865
866
867
868
869
880
882
883
885
886
887
888
889
890
892
893
895
896
897
898
899
910
912
913
915
916
917
918
919
920
922
923
925
926
927
928
929
930
932
933
935
936
937
938
939
940
942
943
945
946
947
948
949
950
952
953
955
956
957
958
959
960
962
963
965
966
967
968
969
980
982
983
985
986
987
988
989
990
992
993
995
996
997
998
999
code i have tried
getwd()
setwd("C:/Users/aziq/Desktop")
library(xlsx)
x <- read.xlsx("numbers.xlsx","Sheet1")
generate_combinations <- function(x, pos, n) {
#select first pos characters from each string
#split each character and create a matrix
mat <- do.call(rbind, strsplit(substr(x, 1, pos), ''))
#Find top n occurrence in each column of matrix
tmp <- apply(mat, 2, function(x) tail(names(sort(table(x))), n))
#Create all combinations of top occurrences.
do.call(expand.grid, asplit(tmp, 2))
}
generate_combinations(x, 2, 8)
nrow(generate_combinations(x, 2, 8))
Error it is showing
Error in asplit(tmp, 2) : dim(x) must have a positive length
Dput results:
> dput(x)
structure(list(X076060 = c("693022", "585821", "980575", "438068",
"766214", "051726", "060417", "822591", "015507", "635576", "180231",
"212238", "417651", "631269", "720767", "348344", "532148", "748085",
"474026", "380897", "512421", "749492", "423616", "950330", "930079",
"097759", "638901", "319356", "683308", "818127", "880675", "256095",
"639187", "339904", "945437", "799571", "466063", "428853", "397799",
"782034", "462486", "739342", "879023", "419264", "793319", "603131",
"315791", "351701", "151747", "365656", "982700", "348093", "793392",
"946875", "912108", "070001", "780515", "222468", "345439", "234846",
"227112", "757243", "341747", "480781", "906624", "868265", "388572",
"947873", "898895", "452518", "738580", "217342", "849951", "437382",
"247068", "743776", "562584", "636948", "049434", "139296", "688436",
"443629")), class = "data.frame", row.names = c(NA, -82L))
We can write a function :
generate_combinations <- function(x, pos, n) {
if(pos == 1) {
return(data.frame(Var1 = names(sort(table(substr(x, 1, pos)),
= decreasing = TRUE)[1:n])))
}
#select first pos characters from each string
#split each character and create a matrix
mat <- do.call(rbind, strsplit(substr(x, 1, pos), ''))
#Find top n occurrence in each column of matrix
tmp <- apply(mat, 2, function(x) tail(names(sort(table(x))), n))
#Create all combinations of top occurrences.
do.call(expand.grid, asplit(tmp, 2))
}
generate_combinations(x, 2, 8)
# Var1 Var2
#1 0 2
#2 2 2
#3 8 2
#4 6 2
#5 9 2
#6 3 2
#7 4 2
#8 7 2
#9 0 5
#10 2 5
#...
#...
nrow(generate_combinations(x, 2, 8))
#[1] 64
nrow(generate_combinations(x, 3, 8))
#[1] 512
data
x <- c("076060", "693022", "585821", "980575", "438068", "766214",
"051726", "060417", "822591", "015507", "635576", "180231", "212238",
"417651", "631269", "720767", "348344", "532148", "748085", "474026",
"380897", "512421", "749492", "423616", "950330", "930079", "097759",
"638901", "319356", "683308", "818127", "880675", "256095", "639187",
"339904", "945437", "799571", "466063", "428853", "397799", "782034",
"462486", "739342", "879023", "419264", "793319", "603131", "315791",
"351701", "151747", "365656", "982700", "348093", "793392", "946875",
"912108", "070001", "780515", "222468", "345439", "234846", "227112",
"757243", "341747", "480781", "906624", "868265", "388572", "947873",
"898895", "452518", "738580", "217342", "849951", "437382", "247068",
"743776", "562584", "636948", "049434", "139296", "688436", "443629")
Related
I tried if vec2 > 600 save it to va
set.seed(75)
Vec2 <- sample(0:999, size = 100)
print(Vec2)
va <- (Vec2 > 600)
print(va)
set.seed(75)
Vec2 <- sample(0:999, size = 100)
print(Vec2)
#> [1] 703 167 792 788 872 758 202 925 472 463 823 596 928 274 290 190 599 943
#> [19] 321 95 745 587 971 635 432 300 818 415 878 914 139 926 806 677 49 832
#> [37] 610 531 175 678 501 462 148 645 958 213 731 367 912 523 324 80 133 473
#> [55] 235 976 595 239 1 785 591 533 908 750 455 946 435 384 223 510 97 853
#> [73] 24 742 757 713 685 149 699 690 334 841 104 604 549 436 271 141 695 934
#> [91] 129 932 561 439 296 897 174 727 445 396
va <- Vec2[(Vec2 > 600)]
print(va)
#> [1] 703 792 788 872 758 925 823 928 943 745 971 635 818 878 914 926 806 677 832
#> [20] 610 678 645 958 731 912 976 785 908 750 946 853 742 757 713 685 699 690 841
#> [39] 604 695 934 932 897 727
Created on 2023-01-27 by the reprex package (v2.0.1)
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")
This question already has answers here:
How to reshape data from long to wide format
(14 answers)
Closed 5 years ago.
I have a simple table in the following format:
Date val
2005-01-01 15
2005-01-02 18
2005-01-03 20
...
And am trying to reshape it to the following "wide" column format:
Year Month day1 day2 day3 day4 ...day31
2005 01 day1val day2val day3val day4val ...day31val
2005 02 day1val day2val day3val day4val ...day31val
I've successfully split the date column into three separate d,m,y columns using
dates_separated <- data.frame(year = as.numeric(format(input_df$DATE, format = "%Y")),
month = as.numeric(format(input_df$DATE, format = "%m")),
day = as.numeric(format(input_df$DATE, format = "%d")))
output_df <- cbind(input_df, dates_sep)
I'm trying to use the reshape function to get this done, but am finding my output could be more complicated than it can handle. Is there another function I should be using here?
Edit: I don't believe this was a duplicate of what was suggested. markdly's answer below did exactly what I needed. Thanks!
For the sake of completeness, here is a solution using the dcast() function.
OP's input_df consists only of two columns Date and val. So, let's create a full year of sample data by
set.seed(1234L)
input_df <- data.frame(Date = as.Date("2005-01-01") + 0:364,
val = sample(100:999, 365L, TRUE))
The dcast() function is available from the reshape2 and the data.table packages. Here, data.table is used because of its handy year(), month(), and mday() functions:
library(data.table)
dcast(input_df, year(Date) + month(Date) ~ mday(Date))
year(Date) month(Date) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1 2005 1 202 660 648 661 874 676 108 309 699 562 724 590 354 931 363 853 357 340 268 309 384 372 243 135 296 829 573 923 848 141 510
2 2005 2 338 374 556 262 783 281 332 992 826 598 681 380 659 396 551 709 536 319 788 166 378 745 554 237 553 544 776 257 NA NA NA
3 2005 3 863 878 137 385 112 315 735 377 557 146 608 209 903 113 804 180 567 445 163 388 701 933 524 228 589 276 908 450 379 244 906
4 2005 4 249 910 220 218 194 560 370 124 378 767 131 608 352 283 220 393 239 216 491 134 741 190 955 209 297 921 951 351 211 817 NA
5 2005 5 769 924 995 948 537 355 326 552 547 386 966 670 214 480 922 521 917 637 668 882 552 985 391 533 421 664 767 609 982 619 495
6 2005 6 305 173 865 311 989 641 998 438 599 486 618 489 302 176 673 487 165 822 392 781 625 737 484 409 783 481 604 204 372 530 NA
7 2005 7 410 640 168 960 119 857 669 379 768 675 993 215 894 829 839 851 759 984 675 694 575 385 791 573 759 376 463 283 987 609 352
8 2005 8 266 782 610 938 674 730 531 865 480 128 332 401 220 549 821 403 558 544 817 610 196 826 610 291 774 376 540 990 481 319 295
9 2005 9 720 982 529 796 616 969 817 578 636 337 351 158 606 336 102 630 568 860 126 639 341 208 190 773 114 144 772 421 783 438 NA
10 2005 10 819 123 555 839 590 340 410 432 486 926 805 764 352 511 358 726 838 689 472 956 318 647 782 724 203 672 378 417 982 584 499
11 2005 11 954 507 271 992 593 791 922 713 466 466 231 277 272 467 413 851 278 875 457 237 405 430 484 267 692 928 760 894 958 275 NA
12 2005 12 525 447 436 125 936 469 960 344 565 980 432 379 130 700 928 140 281 769 217 737 998 949 633 758 538 791 102 602 514 396 851
To prettify the result, Year and Month can be computed in advance:
dcast(setDT(input_df)[, Year := year(Date)][, Month := month(Date)],
Year + Month ~ sprintf("day%02i", mday(Date)), value.var = "val")
Year Month day01 day02 day03 day04 day05 day06 day07 day08 day09 day10 day11 day12 day13 day14 day15 day16 day17 day18 day19 day20 ...
1: 2005 1 202 660 648 661 874 676 108 309 699 562 724 590 354 931 363 853 357 340 268 309
2: 2005 2 338 374 556 262 783 281 332 992 826 598 681 380 659 396 551 709 536 319 788 166
3: 2005 3 863 878 137 385 112 315 735 377 557 146 608 209 903 113 804 180 567 445 163 388
4: 2005 4 249 910 220 218 194 560 370 124 378 767 131 608 352 283 220 393 239 216 491 134
5: 2005 5 769 924 995 948 537 355 326 552 547 386 966 670 214 480 922 521 917 637 668 882
6: 2005 6 305 173 865 311 989 641 998 438 599 486 618 489 302 176 673 487 165 822 392 781
7: 2005 7 410 640 168 960 119 857 669 379 768 675 993 215 894 829 839 851 759 984 675 694
8: 2005 8 266 782 610 938 674 730 531 865 480 128 332 401 220 549 821 403 558 544 817 610
9: 2005 9 720 982 529 796 616 969 817 578 636 337 351 158 606 336 102 630 568 860 126 639
10: 2005 10 819 123 555 839 590 340 410 432 486 926 805 764 352 511 358 726 838 689 472 956
11: 2005 11 954 507 271 992 593 791 922 713 466 466 231 277 272 467 413 851 278 875 457 237
12: 2005 12 525 447 436 125 936 469 960 344 565 980 432 379 130 700 928 140 281 769 217 737
Note that here sprintf("Day%02i", mday(Date)) is used to keep the columns ordered. Using paste0("day", day) as in markdly's answer, the columns would be in the wrong order:
day1 day10 day11 day12 day13 day14 day15 day16 day17 day18 day19 day2 day20 ...
If you can add actual data to your question it really helps others to post answers. For example, here's some data for 5 days in each month in 2015:
set.seed(123)
df <- expand.grid(year = 2015, month = 1:12, day = 1:5)
df$val <- sample.int(1000, nrow(df))
head(df)
#> year month day val
#> 1 2015 1 1 288
#> 2 2015 2 1 788
#> 3 2015 3 1 409
#> 4 2015 4 1 881
#> 5 2015 5 1 937
#> 6 2015 6 1 46
This can be converted to the desired format using tidyr::spread:
library(dplyr)
library(tidyr)
df %>%
mutate(day = paste0("day", day)) %>%
spread(day, val)
#> year month day1 day2 day3 day4 day5
#> 1 2015 1 288 670 640 732 254
#> 2 2015 2 788 566 691 209 816
#> 3 2015 3 409 102 530 307 44
#> 4 2015 4 881 993 579 223 420
#> 5 2015 5 937 243 282 138 758
#> 6 2015 6 46 42 143 398 116
#> 7 2015 7 525 323 935 397 531
#> 8 2015 8 887 996 875 353 196
#> 9 2015 9 548 872 669 146 121
#> 10 2015 10 453 679 770 133 711
#> 11 2015 11 948 627 24 961 844
#> 12 2015 12 449 972 462 445 957
I've got the final data DS such as :
|user_id
40 33
70 50
93 67
106 77
136 91
144 97
160 105
176 113
195 128
207 132
211 134
229 142
280 159
338 187
407 232
425 248
442 259
446 261
469 277
470 278
588 353
590 355
594 358
598 362
609 369
615 375
626 381
633 386
652 399
657 402
735 452
751 464
758 470
760 471
769 478
774 480
806 493
821 501
825 505
856 526
876 536
886 540
890 542
894 543
903 549
919 556
921 558
932 562
The fist column is a what left of line numbers I suppose, after many data manipulations,
and I'd like to drop them, nice, efficient way, and replace it with normal order numbers , 1,2,3,4,5 etc.
I did try to use :
aggr.cid <-aggregate(cbind(DS$user_id), by=list(CustID = DS$user_id),
function(x) x[1])
But instead of getting 1 line I'm getting two, with content of "user_id"
I can remove the second one and all will looks as I need but it is a doggy way....
Those are the row names. You can reset them with
rownames(DS) <- NULL
I am a beginner R user and am trying to find a way to detect x consecutive values within a column of data which have a value >=y. Example: find all instances where 4 or more consecutive data points have a value >=2
The run length encoding rle() command looks promising for identifying these consecutive values:
rle(dataset>=2)
but I cannot find a way to further set the condition for the lengths to be >=4 and the values to be "TRUE".
Any suggestions?
res <- rle(dataset>=2)
which( res$lengths>=4 & res$values==TRUE] )
That will identify them in the compacted representation of the rle result and you will then need to expand that result and pick out starting points for the sequences.
You can simply transform the vector, and use rle on that:
res = rle(runif(1000, 0, 4) >= 2)
res
Run Length Encoding
lengths: int [1:491] 2 2 2 2 3 1 3 2 7 1 ...
values : logi [1:491] TRUE FALSE TRUE FALSE TRUE FALSE ...
To get the indices where in the vector the runs are located, you can use this trick:
res$values = res$lengths > 4
inv_res = inverse.rle(res)
seq_along(inv_res)[inv_res]
[1] 3 4 5 6 7 8 9 10 11 12 13 37 38 39 40 41 42 74
[19] 75 76 77 78 79 80 81 82 83 84 85 108 109 110 111 112 142 143
[37] 144 145 146 147 148 221 222 223 224 225 226 235 236 237 238 239 240 241
[55] 278 279 280 281 282 305 306 307 308 309 310 311 312 313 314 315 316 317
[73] 318 319 342 343 344 345 346 347 414 415 416 417 418 419 430 431 432 433
[91] 434 435 449 450 451 452 453 472 473 474 475 476 477 478 523 524 525 526
[109] 527 545 546 547 548 549 561 562 563 564 565 566 567 568 569 607 608 609
[127] 610 611 612 613 625 626 627 628 629 630 646 647 648 649 650 651 652 699
[145] 700 701 702 703 765 766 767 768 769 770 771 772 773 789 790 791 792 793
[163] 794 795 800 801 802 803 804 810 811 812 813 814 850 851 852 853 854 855
[181] 869 870 871 872 873 879 880 881 882 883 904 905 906 907 908 909 919 920
[199] 921 922 923 949 950 951 952 953 954 955 956 957 958 959 960 961