Get a sublist of a list - r

I have a vector which is around 3000 elements long. I have extracted a specific point in the vector with which(...). Now I want to have -120 before this point and +120 after this point.
My list looks like that:
> testList$Date
[1] "01.01.2002" "02.01.2002" "03.01.2002" "04.01.2002" "07.01.2002"
[6] "08.01.2002" "09.01.2002" "10.01.2002" "11.01.2002" "14.01.2002"
[11] "15.01.2002" "16.01.2002" "17.01.2002" "18.01.2002" "21.01.2002"
[16] "22.01.2002" "23.01.2002" "24.01.2002" "25.01.2002" "28.01.2002"
[21] "29.01.2002" "30.01.2002" "31.01.2002" "01.02.2002" "04.02.2002"
[26] "05.02.2002" "06.02.2002" "07.02.2002" "08.02.2002" "11.02.2002"
[31] "12.02.2002" "13.02.2002" "14.02.2002" "15.02.2002" "18.02.2002"ect....
I could do a for-loop to iterate over the list and save this as a sublist. However, I do not think that is very efficient. How can I implement that in R?
I appreciate your answer!
UPDATE
When using lapply I get:
> 120BeforeSublist <- lapply(event, function(x) c(x-120, x))
> (120BeforeSublist)
[[1]]
[1] 1875 1995
However I want to have the sublist saved -120 before and +120 after.

Does that describe your problem in principle, and a generic solution:
x <- 1:20
pos <- which( x == 10 )
end <- 2
len <- 5
x_bef <- x[ ( pos - len - end ) : ( pos - end ) ]
x_aft <- x[ (pos + end ) : ( pos + len + end ) ]
x_bef
[1] 3 4 5 6 7 8
x_aft
[1] 12 13 14 15 16 17

How about something like this:
i <- which(...)
boundaries <- lapply(i, function(x) (x-120):(x+120))
An example:
> i <- c(350, 465, 2700) # Points of interest
> boundaries <- lapply(i, function(x) (x-120):(x+120))
> boundaries
[[1]]
[1] 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
[19] 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
[37] 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
[55] 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
[73] 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
[91] 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
[109] 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
[127] 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
[145] 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
[163] 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
[181] 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
[199] 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
[217] 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
[235] 464 465 466 467 468 469 470
[[2]]
[1] 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
[19] 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
[37] 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
[55] 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
[73] 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
[91] 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
[109] 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
[127] 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
[145] 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
[163] 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
[181] 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
[199] 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
[217] 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
[235] 579 580 581 582 583 584 585
[[3]]
[1] 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
[16] 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
[31] 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
[46] 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
[61] 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
[76] 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
[91] 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
[106] 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
[121] 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
[136] 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
[151] 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
[166] 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
[181] 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
[196] 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
[211] 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
[226] 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
[241] 2820

Related

Storing every number that bigger than 600 to store another vector but is not printing numbers

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)

Efficiently Finding Sequences Between Vectors of Start and End Numbers in R [duplicate]

This question already has answers here:
Expand ranges defined by "from" and "to" columns
(10 answers)
Closed 2 years ago.
I have two vectors in R, say, start_values and end_values, which contain numbered elements of increasing value. For example:
start_values <- c(88, 241, 394, 545)
end_values <- c(147, 300, 453, 604)
I'm trying to find an efficient (hopefully without writing a loop) that will allow me to obtain a single vector of numbers with sequences of numbers that range from the first element in start_values to the first element in end_values, then from the second element in start_values to the second element in end_values, etc. So in the end, I'd like a vector called sequence_range that looks like this:
sequence_range <- c(seq(88, 147), seq(241, 300), seq(394, 453), seq(545, 604))
which should have output that looks like:
> sequence_range
[1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
[30] 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
[59] 146 147 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
[88] 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
[117] 297 298 299 300 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
[146] 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
[175] 448 449 450 451 452 453 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
[204] 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
[233] 597 598 599 600 601 602 603 604
I'd appreciate any ideas for efficient techniques to accomplish this so that it's generalizeable to any two vectors of start and end values.
Thanks.
You may use : in an apply on cbinded vectors.
as.vector(apply(cbind(start_values, end_values), 1, function(x) x[1]:x[2]))
# [1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101
# [15] 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# [29] 116 117 118 119 120 121 122 123 124 125 126 127 128 129
# [43] 130 131 132 133 134 135 136 137 138 139 140 141 142 143
# [57] 144 145 146 147 241 242 243 244 245 246 247 248 249 250
# [71] 251 252 253 254 255 256 257 258 259 260 261 262 263 264
# [85] 265 266 267 268 269 270 271 272 273 274 275 276 277 278
# [99] 279 280 281 282 283 284 285 286 287 288 289 290 291 292
# [113] 293 294 295 296 297 298 299 300 394 395 396 397 398 399
# [127] 400 401 402 403 404 405 406 407 408 409 410 411 412 413
# [141] 414 415 416 417 418 419 420 421 422 423 424 425 426 427
# [155] 428 429 430 431 432 433 434 435 436 437 438 439 440 441
# [169] 442 443 444 445 446 447 448 449 450 451 452 453 545 546
# [183] 547 548 549 550 551 552 553 554 555 556 557 558 559 560
# [197] 561 562 563 564 565 566 567 568 569 570 571 572 573 574
# [211] 575 576 577 578 579 580 581 582 583 584 585 586 587 588
# [225] 589 590 591 592 593 594 595 596 597 598 599 600 601 602
# [239] 603 604
mapply works nicely:
> as.vector(mapply(seq,start_values,end_values))
[1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101
[15] 102 103 104 105 106 107 108 109 110 111 112 113 114 115
[29] 116 117 118 119 120 121 122 123 124 125 126 127 128 129
[43] 130 131 132 133 134 135 136 137 138 139 140 141 142 143
[57] 144 145 146 147 241 242 243 244 245 246 247 248 249 250
[71] 251 252 253 254 255 256 257 258 259 260 261 262 263 264
[85] 265 266 267 268 269 270 271 272 273 274 275 276 277 278
[99] 279 280 281 282 283 284 285 286 287 288 289 290 291 292
[113] 293 294 295 296 297 298 299 300 394 395 396 397 398 399
[127] 400 401 402 403 404 405 406 407 408 409 410 411 412 413
[141] 414 415 416 417 418 419 420 421 422 423 424 425 426 427
[155] 428 429 430 431 432 433 434 435 436 437 438 439 440 441
[169] 442 443 444 445 446 447 448 449 450 451 452 453 545 546
[183] 547 548 549 550 551 552 553 554 555 556 557 558 559 560
[197] 561 562 563 564 565 566 567 568 569 570 571 572 573 574
[211] 575 576 577 578 579 580 581 582 583 584 585 586 587 588
[225] 589 590 591 592 593 594 595 596 597 598 599 600 601 602
[239] 603 604

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")

Model fitting: From "nlmer" to "nlme"

How can I fit the following model BUT using "nlme" instead "nlmer"?
The data (at the end of the post, you can find the data to reproduce the code written here).
dd.gr <- groupedData(y ~ x | id, dd)
Define some functions
beta.model <- function(cl, b0, b1, b2) {
f <- b0*(cl^b1)*(1-cl)^b2
return(f)}
nform <- ~ b0*(cl^b1)*(1-cl)^b2
nfun <- deriv(nform, namevec=c("b0", "b1", "b2"),
function.arg=c("cl","b0", "b1", "b2"))
Generate start parameters
start.dd <- nls(y ~ beta.model(x, b0, b1, b2),
start=list(b0=1, b1=1, b2=1),
data=dd.gr)
start.dd <- coef(start.dd)
Fit the nonlinear model
fit <- lme4::nlmer(y ~ nfun(x, b0, b1, b2) ~
(b0|id),
data = dd.gr,
start = start.dd, REML=T)
summary(fit)
Nonlinear mixed model fit by maximum likelihood ['nlmerMod']
Formula: y ~ nfun(x, b0, b1, b2) ~ (b0 | id)
Data: dd.gr
AIC BIC logLik deviance df.resid
-1673.5 -1652.0 841.7 -1683.5 534
Scaled residuals:
Min 1Q Median 3Q Max
-3.4812 -0.6319 0.0865 0.5712 3.2816
Random effects:
Groups Name Variance Std.Dev.
id b0 0.03537 0.18808
Residual 0.00221 0.04701
Number of obs: 539, groups: id, 20
Fixed effects:
Estimate Std. Error t value
b0 0.99075 0.04902 20.21
b1 0.45828 0.01449 31.62
b2 0.65220 0.01734 37.60
Correlation of Fixed Effects:
b0 b1
b1 0.480
b2 0.475 0.809
I would be grateful if anyone could help me adapt my code for what I propose.
Here the data "dd":
y x id
1 0.19012041 0.033511 20
2 0.28284850 0.068081 20
3 0.30852905 0.101623 20
4 0.33527818 0.137577 20
5 0.38641197 0.170015 20
6 0.41929523 0.207414 20
7 0.41697570 0.240817 20
8 0.41720256 0.274229 20
9 0.42971583 0.311311 20
10 0.41991537 0.345469 20
11 0.43032094 0.377397 20
12 0.43643438 0.414996 20
13 0.42266673 0.446316 20
14 0.43037591 0.480386 20
15 0.41315721 0.516730 20
16 0.40941867 0.550281 20
17 0.38272123 0.586440 20
18 0.38690141 0.619709 20
19 0.37053631 0.655532 20
20 0.35475040 0.690939 20
21 0.33294172 0.722318 20
22 0.26763630 0.754480 20
23 0.21367107 0.793380 20
24 0.19283832 0.826003 20
25 0.18314927 0.862719 20
26 0.16607962 0.895078 20
27 0.10271515 0.929464 20
28 0.05054509 0.964828 20
29 0.12439211 0.055681 29
30 0.24075680 0.113468 29
31 0.28940082 0.165547 29
32 0.36451986 0.222673 29
33 0.39986469 0.279548 29
34 0.41949874 0.338177 29
35 0.42081401 0.387903 29
36 0.41949874 0.446316 29
37 0.41166800 0.500000 29
38 0.39383040 0.556794 29
39 0.35305355 0.613815 29
40 0.31752589 0.670729 29
41 0.28620056 0.722318 29
42 0.24354607 0.779668 29
43 0.21800329 0.837162 29
44 0.18740906 0.888411 29
45 0.16769700 0.946148 29
46 0.35212840 0.040750 1970
47 0.48320028 0.085903 1970
48 0.53451401 0.126127 1970
49 0.55424578 0.165547 1970
50 0.56271842 0.207414 1970
51 0.57498323 0.252096 1970
52 0.57982842 0.291917 1970
53 0.57911318 0.331517 1970
54 0.54955214 0.370496 1970
55 0.54106483 0.414996 1970
56 0.51983827 0.459525 1970
57 0.48683208 0.505156 1970
58 0.41197552 0.543154 1970
59 0.39301102 0.581372 1970
60 0.35144113 0.624610 1970
61 0.32615887 0.670729 1970
62 0.30356154 0.709662 1970
63 0.25400500 0.749612 1970
64 0.23843431 0.788056 1970
65 0.17314649 0.832348 1970
66 0.11181707 0.876633 1970
67 0.09217675 0.914606 1970
68 0.05513091 0.955580 1970
69 0.27973694 0.033511 1971
70 0.31497877 0.068081 1971
71 0.31774541 0.101623 1971
72 0.33633484 0.137577 1971
73 0.38142103 0.170015 1971
74 0.39212430 0.207414 1971
75 0.41776918 0.240817 1971
76 0.46544395 0.274229 1971
77 0.48094132 0.311311 1971
78 0.47609669 0.345469 1971
79 0.48437211 0.377397 1971
80 0.49303656 0.414996 1971
81 0.51532308 0.446316 1971
82 0.52591006 0.480386 1971
83 0.53168086 0.516730 1971
84 0.53575850 0.550281 1971
85 0.53642039 0.586440 1971
86 0.53831331 0.619709 1971
87 0.49989785 0.655532 1971
88 0.47896984 0.690939 1971
89 0.44155355 0.722318 1971
90 0.39668264 0.754480 1971
91 0.36687930 0.793380 1971
92 0.28175916 0.826003 1971
93 0.25477636 0.862719 1971
94 0.20772056 0.895078 1971
95 0.18146242 0.929464 1971
96 0.11509623 0.964828 1971
97 0.29424805 0.028771 2037
98 0.31100689 0.055681 2037
99 0.37968128 0.080921 2037
100 0.44570510 0.113468 2037
101 0.47689253 0.132828 2037
102 0.51388355 0.165547 2037
103 0.52951039 0.190470 2037
104 0.53486242 0.214077 2037
105 0.55310811 0.249102 2037
106 0.54069923 0.274229 2037
107 0.56085704 0.298996 2037
108 0.57346329 0.331517 2037
109 0.57252492 0.349985 2037
110 0.55666983 0.377397 2037
111 0.55588612 0.408420 2037
112 0.53516121 0.434682 2037
113 0.53464502 0.459525 2037
114 0.51990000 0.497546 2037
115 0.51118999 0.505156 2037
116 0.50976662 0.543154 2037
117 0.51704108 0.567886 2037
118 0.51518273 0.592719 2037
119 0.51144578 0.624610 2037
120 0.48702363 0.651879 2037
121 0.46874780 0.670729 2037
122 0.46610520 0.702626 2037
123 0.45957450 0.729257 2037
124 0.44940666 0.754480 2037
125 0.41248172 0.788056 2037
126 0.39988825 0.811593 2037
127 0.38568122 0.837162 2037
128 0.34338463 0.862719 2037
129 0.28846227 0.888411 2037
130 0.18329780 0.921480 2037
131 0.14774007 0.946148 2037
132 0.08575088 0.972091 2037
133 0.21763661 0.028771 2038
134 0.23256787 0.062335 2038
135 0.31054960 0.094141 2038
136 0.33781744 0.126127 2038
137 0.33712660 0.159287 2038
138 0.35776009 0.190470 2038
139 0.36132643 0.222673 2038
140 0.37726677 0.249102 2038
141 0.38080348 0.279548 2038
142 0.38177161 0.311311 2038
143 0.36956664 0.338177 2038
144 0.36977686 0.377397 2038
145 0.37166348 0.408420 2038
146 0.37411226 0.434682 2038
147 0.36272173 0.470750 2038
148 0.35090634 0.500000 2038
149 0.34812706 0.530893 2038
150 0.33913760 0.561354 2038
151 0.33184098 0.592719 2038
152 0.31086225 0.624610 2038
153 0.30179281 0.655532 2038
154 0.29244225 0.685227 2038
155 0.29171056 0.715608 2038
156 0.28896703 0.749612 2038
157 0.27356355 0.779668 2038
158 0.24517447 0.811593 2038
159 0.24808910 0.843440 2038
160 0.24206431 0.876633 2038
161 0.17025486 0.907147 2038
162 0.08859430 0.939046 2038
163 0.01079669 0.967649 2038
164 0.17164772 0.055681 2167
165 0.20956873 0.118653 2167
166 0.25329354 0.176730 2167
167 0.31110630 0.234884 2167
168 0.34389648 0.291917 2167
169 0.36050986 0.349985 2167
170 0.35936234 0.414996 2167
171 0.36918899 0.470750 2167
172 0.35897902 0.530893 2167
173 0.36581733 0.586440 2167
174 0.36862919 0.646015 2167
175 0.33702190 0.709662 2167
176 0.31287146 0.761244 2167
177 0.24696364 0.826003 2167
178 0.23022979 0.883711 2167
179 0.21250447 0.939046 2167
180 0.17598238 0.033511 2170
181 0.18737701 0.062335 2170
182 0.25275592 0.094141 2170
183 0.28559558 0.126127 2170
184 0.32734108 0.159287 2170
185 0.34818007 0.197259 2170
186 0.36637420 0.222673 2170
187 0.37547146 0.259716 2170
188 0.37475402 0.291917 2170
189 0.38964924 0.322916 2170
190 0.41576375 0.355376 2170
191 0.42667442 0.387903 2170
192 0.44626303 0.421442 2170
193 0.45753246 0.452095 2170
194 0.47906100 0.484540 2170
195 0.48635176 0.516730 2170
196 0.48690479 0.550281 2170
197 0.47611344 0.581372 2170
198 0.46409143 0.613815 2170
199 0.44356431 0.646015 2170
200 0.43274461 0.678666 2170
201 0.42292197 0.709662 2170
202 0.37036428 0.741452 2170
203 0.34960413 0.773048 2170
204 0.32294790 0.805637 2170
205 0.26755673 0.837162 2170
206 0.24912608 0.868745 2170
207 0.19643837 0.901224 2170
208 0.16754568 0.934460 2170
209 0.16021908 0.967649 2170
210 0.02224721 0.028771 2779
211 0.11104242 0.046814 2779
212 0.16841056 0.074093 2779
213 0.18409920 0.094141 2779
214 0.19098377 0.118653 2779
215 0.19968678 0.143437 2779
216 0.24379425 0.165547 2779
217 0.26619965 0.184325 2779
218 0.29353416 0.214077 2779
219 0.31359542 0.234884 2779
220 0.30777733 0.252096 2779
221 0.33962059 0.279548 2779
222 0.34543186 0.306186 2779
223 0.35301113 0.331517 2779
224 0.36405476 0.349985 2779
225 0.37258480 0.370496 2779
226 0.38418643 0.395798 2779
227 0.39501225 0.414996 2779
228 0.40087943 0.446316 2779
229 0.40893103 0.465443 2779
230 0.38764362 0.497546 2779
231 0.37029641 0.505156 2779
232 0.36884072 0.537179 2779
233 0.35422820 0.556794 2779
234 0.34543186 0.581372 2779
235 0.32837782 0.606240 2779
236 0.33608560 0.632230 2779
237 0.33095619 0.651879 2779
238 0.31397261 0.670729 2779
239 0.31170265 0.697418 2779
240 0.28869241 0.722318 2779
241 0.27357500 0.749612 2779
242 0.25919586 0.767440 2779
243 0.24564087 0.788056 2779
244 0.21054514 0.818738 2779
245 0.18037964 0.837162 2779
246 0.15573046 0.857893 2779
247 0.14260262 0.883711 2779
248 0.09563263 0.907147 2779
249 0.08206988 0.929464 2779
250 0.03908776 0.955580 2779
251 0.02125794 0.972091 2779
252 0.33096335 0.037328 2780
253 0.36969011 0.074093 2780
254 0.44504021 0.113468 2780
255 0.45485124 0.151987 2780
256 0.46464513 0.190470 2780
257 0.53736638 0.229281 2780
258 0.57555094 0.267528 2780
259 0.59723195 0.306186 2780
260 0.61552894 0.345469 2780
261 0.62033914 0.383389 2780
262 0.61015052 0.421442 2780
263 0.59728123 0.459525 2780
264 0.59515874 0.497546 2780
265 0.55152852 0.537179 2780
266 0.52641099 0.574251 2780
267 0.48956528 0.613815 2780
268 0.48497512 0.651879 2780
269 0.45698140 0.690939 2780
270 0.43844469 0.729257 2780
271 0.41683569 0.767440 2780
272 0.40151589 0.805637 2780
273 0.39202175 0.843440 2780
274 0.38459441 0.883711 2780
275 0.28157268 0.921480 2780
276 0.21164063 0.960888 2780
277 0.08489932 0.028771 1771
278 0.12305442 0.055681 1771
279 0.14546015 0.080921 1771
280 0.20507495 0.106794 1771
281 0.21327282 0.132828 1771
282 0.22885254 0.165547 1771
283 0.25315275 0.190470 1771
284 0.28460782 0.214077 1771
285 0.28404561 0.249102 1771
286 0.29963138 0.274229 1771
287 0.30334288 0.298996 1771
288 0.32026109 0.331517 1771
289 0.37167406 0.349985 1771
290 0.39620088 0.377397 1771
291 0.41163531 0.408420 1771
292 0.41672284 0.434682 1771
293 0.43135634 0.459525 1771
294 0.43591399 0.497546 1771
295 0.44049046 0.505156 1771
296 0.43976407 0.543154 1771
297 0.42582959 0.567886 1771
298 0.41946869 0.592719 1771
299 0.40894347 0.624610 1771
300 0.40408173 0.651879 1771
301 0.39886942 0.670729 1771
302 0.36607992 0.702626 1771
303 0.35079531 0.729257 1771
304 0.33792425 0.754480 1771
305 0.31231247 0.788056 1771
306 0.29062044 0.811593 1771
307 0.27997472 0.837162 1771
308 0.23381520 0.868745 1771
309 0.20789011 0.888411 1771
310 0.20415166 0.921480 1771
311 0.10956034 0.946148 1771
312 0.07090901 0.972091 1771
313 0.25314656 0.046814 1773
314 0.30329515 0.085903 1773
315 0.32123976 0.132828 1773
316 0.34022917 0.176730 1773
317 0.36301058 0.214077 1773
318 0.37100331 0.259716 1773
319 0.38405288 0.306186 1773
320 0.40362816 0.349985 1773
321 0.41329562 0.395798 1773
322 0.44000388 0.434682 1773
323 0.44340953 0.480386 1773
324 0.44170999 0.521847 1773
325 0.44880483 0.567886 1773
326 0.44754614 0.606240 1773
327 0.43829113 0.651879 1773
328 0.43014986 0.697418 1773
329 0.40816814 0.741452 1773
330 0.41000674 0.779668 1773
331 0.40465177 0.826003 1773
332 0.34777252 0.868745 1773
333 0.32310745 0.914606 1773
334 0.18171237 0.955580 1773
335 0.26951496 0.040750 2001
336 0.37914212 0.080921 2001
337 0.40401647 0.118653 2001
338 0.42272825 0.159287 2001
339 0.43048811 0.197259 2001
340 0.46236016 0.240817 2001
341 0.50902284 0.279548 2001
342 0.52259916 0.318519 2001
343 0.53243477 0.361549 2001
344 0.54973030 0.400000 2001
345 0.53761080 0.439565 2001
346 0.51653397 0.480386 2001
347 0.49107186 0.521847 2001
348 0.44538828 0.561354 2001
349 0.43004423 0.600000 2001
350 0.42084156 0.641002 2001
351 0.42922927 0.678666 2001
352 0.41879440 0.722318 2001
353 0.32868583 0.761244 2001
354 0.29564826 0.800000 2001
355 0.23628702 0.843440 2001
356 0.20310825 0.876633 2001
357 0.13773623 0.921480 2001
358 0.12074184 0.960888 2001
359 0.39968960 0.046814 2003
360 0.57863824 0.101623 2003
361 0.65666614 0.151987 2003
362 0.67908133 0.197259 2003
363 0.67341823 0.249102 2003
364 0.65522550 0.298996 2003
365 0.64079619 0.349985 2003
366 0.64435662 0.395798 2003
367 0.63861925 0.452095 2003
368 0.63588759 0.500000 2003
369 0.61311017 0.550281 2003
370 0.62738351 0.606240 2003
371 0.61852138 0.651879 2003
372 0.58223709 0.697418 2003
373 0.54161098 0.749612 2003
374 0.48646018 0.805637 2003
375 0.38459670 0.851205 2003
376 0.38043597 0.901224 2003
377 0.27075957 0.946148 2003
378 0.38036247 0.037328 2122
379 0.40276000 0.074093 2122
380 0.39612914 0.113468 2122
381 0.42791423 0.151987 2122
382 0.42825796 0.190470 2122
383 0.46095690 0.229281 2122
384 0.48521779 0.267528 2122
385 0.49731865 0.306186 2122
386 0.54605868 0.345469 2122
387 0.54933640 0.383389 2122
388 0.55525115 0.421442 2122
389 0.55625728 0.459525 2122
390 0.55072754 0.497546 2122
391 0.55657463 0.537179 2122
392 0.53676362 0.574251 2122
393 0.50652415 0.613815 2122
394 0.48860251 0.651879 2122
395 0.47683604 0.690939 2122
396 0.44212102 0.729257 2122
397 0.39612914 0.767440 2122
398 0.38528248 0.805637 2122
399 0.34693920 0.843440 2122
400 0.29693585 0.883711 2122
401 0.26873795 0.921480 2122
402 0.22797492 0.960888 2122
403 0.24762581 0.033511 2125
404 0.29675779 0.062335 2125
405 0.32531104 0.094141 2125
406 0.33472047 0.126127 2125
407 0.34663074 0.159287 2125
408 0.35390041 0.197259 2125
409 0.36387951 0.222673 2125
410 0.37320407 0.259716 2125
411 0.38901117 0.291917 2125
412 0.39308746 0.322916 2125
413 0.40142535 0.355376 2125
414 0.41491642 0.387903 2125
415 0.42316665 0.421442 2125
416 0.41814693 0.452095 2125
417 0.40435419 0.484540 2125
418 0.37175904 0.516730 2125
419 0.38099944 0.550281 2125
420 0.38516053 0.581372 2125
421 0.37985668 0.613815 2125
422 0.37717678 0.646015 2125
423 0.30406241 0.678666 2125
424 0.34248509 0.709662 2125
425 0.32696141 0.741452 2125
426 0.29019762 0.773048 2125
427 0.28639182 0.805637 2125
428 0.16112100 0.837162 2125
429 0.15240521 0.868745 2125
430 0.14023783 0.901224 2125
431 0.11144973 0.934460 2125
432 0.04684425 0.967649 2125
433 0.12738818 0.033511 3355
434 0.12032137 0.068081 3355
435 0.19246996 0.101623 3355
436 0.30021305 0.137577 3355
437 0.39536852 0.170015 3355
438 0.43191219 0.207414 3355
439 0.45271647 0.240817 3355
440 0.47445420 0.274229 3355
441 0.48947320 0.311311 3355
442 0.51200729 0.345469 3355
443 0.51159131 0.377397 3355
444 0.50573160 0.414996 3355
445 0.50671293 0.446316 3355
446 0.49885571 0.480386 3355
447 0.47911798 0.516730 3355
448 0.48398125 0.550281 3355
449 0.45930461 0.586440 3355
450 0.44893852 0.619709 3355
451 0.42866861 0.655532 3355
452 0.40953422 0.690939 3355
453 0.37415759 0.722318 3355
454 0.34808624 0.754480 3355
455 0.34998393 0.793380 3355
456 0.31834044 0.826003 3355
457 0.24694972 0.862719 3355
458 0.22192562 0.895078 3355
459 0.17830631 0.929464 3355
460 0.17509332 0.964828 3355
461 0.23328477 0.046814 3356
462 0.29713891 0.094141 3356
463 0.33913003 0.137577 3356
464 0.39302555 0.184325 3356
465 0.42662490 0.229281 3356
466 0.45006759 0.274229 3356
467 0.45955706 0.318519 3356
468 0.46964286 0.361549 3356
469 0.47564354 0.408420 3356
470 0.47304390 0.452095 3356
471 0.44970213 0.500000 3356
472 0.42720259 0.543154 3356
473 0.41737157 0.592719 3356
474 0.39780731 0.632230 3356
475 0.35407259 0.685227 3356
476 0.30057734 0.729257 3356
477 0.20794585 0.773048 3356
478 0.20253853 0.818738 3356
479 0.16693878 0.862719 3356
480 0.08553668 0.907147 3356
481 0.03007141 0.955580 3356
482 0.12522795 0.037328 2873
483 0.12723816 0.074093 2873
484 0.14477616 0.106794 2873
485 0.15411502 0.143437 2873
486 0.21348148 0.176730 2873
487 0.26271901 0.214077 2873
488 0.27856451 0.252096 2873
489 0.31723668 0.285536 2873
490 0.34120764 0.318519 2873
491 0.36044750 0.355376 2873
492 0.36332243 0.395798 2873
493 0.36755816 0.428268 2873
494 0.37460158 0.465443 2873
495 0.38178095 0.500000 2873
496 0.38118229 0.537179 2873
497 0.36989800 0.574251 2873
498 0.35641212 0.606240 2873
499 0.33814474 0.641002 2873
500 0.30921638 0.678666 2873
501 0.28200482 0.715608 2873
502 0.26425989 0.754480 2873
503 0.25337707 0.788056 2873
504 0.24647287 0.818738 2873
505 0.22176036 0.857893 2873
506 0.16676785 0.895078 2873
507 0.11398351 0.929464 2873
508 0.05037407 0.964828 2873
509 0.27228222 0.028771 2874
510 0.34193313 0.062335 2874
511 0.39443653 0.094141 2874
512 0.42802678 0.126127 2874
513 0.43522858 0.159287 2874
514 0.43105717 0.190470 2874
515 0.44967499 0.222673 2874
516 0.46696994 0.249102 2874
517 0.47057562 0.279548 2874
518 0.47811186 0.311311 2874
519 0.47603496 0.338177 2874
520 0.47778667 0.377397 2874
521 0.48030114 0.408420 2874
522 0.48376732 0.434682 2874
523 0.47758331 0.470750 2874
524 0.47316947 0.500000 2874
525 0.46672024 0.530893 2874
526 0.47349784 0.561354 2874
527 0.45448816 0.592719 2874
528 0.44419784 0.624610 2874
529 0.43478196 0.655532 2874
530 0.36181003 0.685227 2874
531 0.37885981 0.715608 2874
532 0.38476236 0.749612 2874
533 0.36972431 0.779668 2874
534 0.34385953 0.811593 2874
535 0.24493663 0.843440 2874
536 0.24109927 0.876633 2874
537 0.21870520 0.907147 2874
538 0.19573503 0.939046 2874
539 0.19263348 0.967649 2874
fit2 <- nlme::nlme(y ~ nfun(x, b0, b1, b2),
data = dd.gr,
fixed = b0 + b1 + b2 ~ 1,
random = b0 ~ 1 | id,
start = start.dd,
method = "REML")

Successive prime numbers in R [duplicate]

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

Resources