If-Else Statement in R with 3 conditions - r

I am trying to use an if-else statement to create a column in my data set. I want this if-else statement to create a column called "Surgical" in the df "option1" that displays the value of the column "Duration" subtracted by 20 ONLY IF the value in Duration is above 625, AND the factor "Single" is indicated in the column "Variability".
I have tried the following code:
option1$Surgical <- ifelse(option1$Variability == "Single", option1$Duration - 20, option1$Duration)
Any insight into how to specify the "only if the value is greater than 625" portion is appreciated!!
Df "option 1" for reference.
dput(option1)
structure(list(Stimulus = structure(c(36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 67L, 73L, 74L, 75L, 76L, 77L, 78L,
79L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
69L, 70L, 71L, 72L, 73L, 74L, 75L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 60L, 61L,
62L, 63L, 64L, 65L, 66L, 67L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 69L,
70L, 71L, 72L, 73L, 74L, 75L, 7L, 9L, 12L, 18L, 28L, 26L, 51L,
37L, 3L, 2L, 19L, 14L, 27L, 23L, 65L, 77L, 7L, 9L, 12L, 18L,
28L, 26L, 51L, 37L, 3L, 2L, 19L, 14L, 27L, 23L, 65L, 77L, 5L,
11L, 20L, 16L, 30L, 25L, 35L, 33L, 7L, 9L, 12L, 18L, 28L, 26L,
51L, 37L, 5L, 11L, 20L, 16L, 30L, 25L, 35L, 33L, 7L, 9L, 12L,
18L, 28L, 26L, 51L, 37L), .Label = c("t1_block2_hoed3.mp3", "t1_block2_whod3.mp3",
"t1_block2_whod4.mp3", "t1_block2_whod5.mp3", "t1_block3_heed2.mp3",
"t1_block3_heed5.mp3", "t1_block3_hoed1.mp3", "t1_block3_hoed2.mp3",
"t1_block3_hoed4.mp3", "t1_block3_whod3.mp3", "t1_block4_heed5.mp3",
"t2_block1_hoed3.mp3", "t2_block1_whod1.mp3", "t2_block1_whod2.mp3",
"t2_block1_whod4.mp3", "t2_block2_heed3.mp3", "t2_block2_hoed5.mp3",
"t2_block3_hoed1.mp3", "t2_block3_whod1.mp3", "t2_block4_heed2.mp3",
"t2_block4_heed5.mp3", "t3_block1_heed1.mp3", "t3_block1_whod2.mp3",
"t3_block1_whod5.mp3", "t3_block2_heed5.mp3", "t3_block2_hoed2.mp3",
"t3_block2_whod5.mp3", "t3_block3_hoed1.mp3", "t3_block3_hoed4.mp3",
"t3_block4_heed3.mp3", "t4_block1_heed1.mp3", "t4_block1_heed2.mp3",
"t4_block1_heed3.mp3", "t4_block1_heed4.mp3", "t4_block1_heed5.mp3",
"t4_block1_hoed1.mp3", "t4_block1_hoed2.mp3", "t4_block1_hoed3.mp3",
"t4_block1_hoed4.mp3", "t4_block1_hoed5.mp3", "t4_block1_whod1.mp3",
"t4_block1_whod2.mp3", "t4_block1_whod3.mp3", "t4_block1_whod4.mp3",
"t4_block1_whod5.mp3", "t4_block2_heed1.mp3", "t4_block2_heed2.mp3",
"t4_block2_heed4.mp3", "t4_block2_heed5.mp3", "t4_block2_hoed1.mp3",
"t4_block2_hoed3.mp3", "t4_block2_hoed4.mp3", "t4_block2_hoed5.mp3",
"t4_block2_whod2.mp3", "t4_block2_whod4.mp3", "t4_block2_whod5.mp3",
"t4_block3_heed1.mp3", "t4_block3_heed4.mp3", "t4_block3_heed5.mp3",
"t4_block3_hoed1.mp3", "t4_block3_hoed2.mp3", "t4_block3_hoed4.mp3",
"t4_block3_hoed5.mp3", "t4_block3_whod1.mp3", "t4_block3_whod2.mp3",
"t4_block3_whod3.mp3", "t4_block3_whod5.mp3", "t4_block4_heed1.mp3",
"t4_block4_heed2.mp3", "t4_block4_heed3.mp3", "t4_block4_heed4.mp3",
"t4_block4_heed5.mp3", "t4_block4_hoed1.mp3", "t4_block4_hoed2.mp3",
"t4_block4_hoed3.mp3", "t4_block4_whod1.mp3", "t4_block4_whod2.mp3",
"t4_block4_whod3.mp3", "t4_block4_whod5.mp3"), class = "factor"),
Duration = c(497L, 517L, 580L, 563L, 569L, 486L, 506L, 536L,
545L, 554L, 516L, 600L, 607L, 577L, 537L, 583L, 544L, 566L,
567L, 616L, 652L, 564L, 517L, 612L, 564L, 632L, 662L, 565L,
594L, 622L, 552L, 542L, 539L, 554L, 600L, 607L, 577L, 497L,
517L, 580L, 563L, 569L, 594L, 563L, 623L, 602L, 516L, 600L,
607L, 577L, 531L, 642L, 624L, 566L, 567L, 616L, 652L, 654L,
576L, 556L, 608L, 632L, 662L, 565L, 497L, 517L, 580L, 563L,
569L, 486L, 506L, 536L, 545L, 554L, 516L, 600L, 607L, 577L,
537L, 583L, 544L, 566L, 567L, 616L, 652L, 564L, 517L, 612L,
564L, 632L, 662L, 565L, 594L, 622L, 552L, 542L, 539L, 554L,
600L, 607L, 577L, 497L, 517L, 580L, 563L, 569L, 594L, 563L,
623L, 602L, 516L, 600L, 607L, 577L, 531L, 642L, 624L, 566L,
567L, 616L, 652L, 654L, 576L, 556L, 608L, 632L, 662L, 565L,
452L, 547L, 510L, 663L, 470L, 503L, 600L, 517L, 491L, 505L,
641L, 581L, 520L, 485L, 517L, 622L, 452L, 547L, 510L, 663L,
470L, 503L, 600L, 517L, 491L, 505L, 641L, 581L, 520L, 485L,
517L, 622L, 510L, 458L, 558L, 638L, 483L, 538L, 577L, 600L,
452L, 547L, 510L, 663L, 470L, 503L, 600L, 517L, 510L, 458L,
558L, 638L, 483L, 538L, 577L, 600L, 452L, 547L, 510L, 663L,
470L, 503L, 600L, 517L), F0 = c(196L, 204L, 204L, 197L, 203L,
216L, 208L, 223L, 213L, 219L, 196L, 202L, 205L, 202L, 208L,
205L, 206L, 197L, 202L, 195L, 200L, 201L, 210L, 202L, 208L,
195L, 196L, 195L, 205L, 208L, 203L, 203L, 212L, 213L, 210L,
206L, 204L, 196L, 204L, 204L, 197L, 203L, 201L, 198L, 199L,
203L, 196L, 202L, 205L, 202L, 193L, 195L, 208L, 197L, 202L,
195L, 200L, 201L, 195L, 205L, 202L, 195L, 196L, 195L, 196L,
204L, 204L, 197L, 203L, 216L, 208L, 223L, 213L, 219L, 196L,
202L, 205L, 202L, 208L, 205L, 206L, 197L, 202L, 195L, 200L,
201L, 210L, 202L, 208L, 195L, 196L, 195L, 205L, 208L, 203L,
203L, 212L, 213L, 210L, 206L, 204L, 196L, 204L, 204L, 197L,
203L, 201L, 198L, 199L, 203L, 196L, 202L, 205L, 202L, 193L,
195L, 208L, 197L, 202L, 195L, 200L, 201L, 195L, 205L, 202L,
195L, 196L, 195L, 215L, 219L, 219L, 220L, 199L, 202L, 202L,
204L, 224L, 231L, 238L, 240L, 217L, 212L, 210L, 208L, 215L,
219L, 219L, 220L, 199L, 202L, 202L, 204L, 224L, 231L, 238L,
240L, 217L, 212L, 210L, 208L, 230L, 223L, 219L, 221L, 199L,
200L, 204L, 210L, 215L, 219L, 219L, 220L, 199L, 202L, 202L,
204L, 230L, 223L, 219L, 221L, 199L, 200L, 204L, 210L, 215L,
219L, 219L, 220L, 199L, 202L, 202L, 204L), F1 = c(576L, 553L,
579L, 586L, 601L, 398L, 390L, 398L, 389L, 404L, 587L, 560L,
562L, 553L, 393L, 397L, 382L, 553L, 592L, 556L, 571L, 387L,
392L, 398L, 400L, 580L, 580L, 554L, 403L, 391L, 388L, 393L,
382L, 375L, 384L, 392L, 388L, 576L, 553L, 579L, 586L, 601L,
387L, 393L, 402L, 406L, 587L, 560L, 562L, 553L, 388L, 391L,
412L, 553L, 592L, 556L, 571L, 410L, 404L, 401L, 420L, 580L,
580L, 554L, 576L, 553L, 579L, 586L, 601L, 398L, 390L, 398L,
389L, 404L, 587L, 560L, 562L, 553L, 393L, 397L, 382L, 553L,
592L, 556L, 571L, 387L, 392L, 398L, 400L, 580L, 580L, 554L,
403L, 391L, 388L, 393L, 382L, 375L, 384L, 392L, 388L, 576L,
553L, 579L, 586L, 601L, 387L, 393L, 402L, 406L, 587L, 560L,
562L, 553L, 388L, 391L, 412L, 553L, 592L, 556L, 571L, 410L,
404L, 401L, 420L, 580L, 580L, 554L, 620L, 630L, 602L, 605L,
571L, 573L, 560L, 553L, 434L, 417L, 306L, 319L, 414L, 419L,
392L, 391L, 620L, 630L, 602L, 605L, 571L, 573L, 560L, 553L,
434L, 417L, 306L, 319L, 414L, 419L, 392L, 391L, 448L, 441L,
293L, 291L, 420L, 420L, 388L, 384L, 620L, 630L, 602L, 605L,
571L, 573L, 560L, 553L, 448L, 441L, 293L, 291L, 420L, 420L,
388L, 384L, 620L, 630L, 602L, 605L, 571L, 573L, 560L, 553L
), F2 = c(1339L, 1381L, 1381L, 1347L, 1394L, 1484L, 1521L,
1539L, 1430L, 1454L, 1353L, 1378L, 1325L, 1357L, 1424L, 1563L,
1578L, 1350L, 1397L, 1273L, 1319L, 1548L, 1452L, 1499L, 1515L,
1358L, 1347L, 1248L, 1575L, 1438L, 1414L, 1548L, 3001L, 2916L,
2948L, 2973L, 2947L, 1339L, 1381L, 1381L, 1347L, 1394L, 2943L,
2913L, 2987L, 2940L, 1353L, 1378L, 1325L, 1357L, 3010L, 3008L,
2972L, 1350L, 1397L, 1273L, 1319L, 2963L, 2991L, 3007L, 2989L,
1358L, 1347L, 1248L, 1339L, 1381L, 1381L, 1347L, 1394L, 1484L,
1521L, 1539L, 1430L, 1454L, 1353L, 1378L, 1325L, 1357L, 1424L,
1563L, 1578L, 1350L, 1397L, 1273L, 1319L, 1548L, 1452L, 1499L,
1515L, 1358L, 1347L, 1248L, 1575L, 1438L, 1414L, 1548L, 3001L,
2916L, 2948L, 2973L, 2947L, 1339L, 1381L, 1381L, 1347L, 1394L,
2943L, 2913L, 2987L, 2940L, 1353L, 1378L, 1325L, 1357L, 3010L,
3008L, 2972L, 1350L, 1397L, 1273L, 1319L, 2963L, 2991L, 3007L,
2989L, 1358L, 1347L, 1248L, 1247L, 1244L, 1293L, 1264L, 1348L,
1354L, 1378L, 1381L, 1314L, 1233L, 1190L, 1208L, 1643L, 1659L,
1452L, 1438L, 1247L, 1244L, 1293L, 1264L, 1348L, 1354L, 1378L,
1381L, 1314L, 1233L, 1190L, 1208L, 1643L, 1659L, 1452L, 1438L,
2837L, 2816L, 2780L, 2776L, 2684L, 2718L, 2947L, 2948L, 1247L,
1244L, 1293L, 1264L, 1348L, 1354L, 1378L, 1381L, 2837L, 2816L,
2780L, 2776L, 2684L, 2718L, 2947L, 2948L, 1247L, 1244L, 1293L,
1264L, 1348L, 1354L, 1378L, 1381L), F3 = c(2831L, 2779L,
2915L, 2875L, 2712L, 2730L, 2793L, 2779L, 2772L, 2692L, 2718L,
2856L, 2674L, 2659L, 2717L, 2584L, 2829L, 2726L, 2685L, 2866L,
2793L, 2614L, 2636L, 2907L, 2822L, 2932L, 2882L, 2882L, 2650L,
2929L, 2809L, 2737L, 3623L, 3607L, 3584L, 3576L, 3680L, 2831L,
2779L, 2915L, 2875L, 2712L, 3641L, 3590L, 3556L, 3584L, 2718L,
2856L, 2674L, 2659L, 3640L, 3656L, 3686L, 2726L, 2685L, 2866L,
2793L, 3516L, 3552L, 3513L, 3579L, 2932L, 2882L, 2882L, 2831L,
2779L, 2915L, 2875L, 2712L, 2730L, 2793L, 2779L, 2772L, 2692L,
2718L, 2856L, 2674L, 2659L, 2717L, 2584L, 2829L, 2726L, 2685L,
2866L, 2793L, 2614L, 2636L, 2907L, 2822L, 2932L, 2882L, 2882L,
2650L, 2929L, 2809L, 2737L, 3623L, 3607L, 3584L, 3576L, 3680L,
2831L, 2779L, 2915L, 2875L, 2712L, 3641L, 3590L, 3556L, 3584L,
2718L, 2856L, 2674L, 2659L, 3640L, 3656L, 3686L, 2726L, 2685L,
2866L, 2793L, 3516L, 3552L, 3513L, 3579L, 2932L, 2882L, 2882L,
2711L, 3129L, 2786L, 2833L, 2754L, 2771L, 2856L, 2779L, 2909L,
2750L, 2866L, 2863L, 2804L, 2704L, 2636L, 2929L, 2711L, 3129L,
2786L, 2833L, 2754L, 2771L, 2856L, 2779L, 2909L, 2750L, 2866L,
2863L, 2804L, 2704L, 2636L, 2929L, 3226L, 3121L, 3867L, 3319L,
3426L, 3269L, 3680L, 3357L, 2711L, 3129L, 2786L, 2833L, 2754L,
2771L, 2856L, 2779L, 3226L, 3121L, 3867L, 3319L, 3426L, 3269L,
3680L, 3357L, 2711L, 3129L, 2786L, 2833L, 2754L, 2771L, 2856L,
2779L), Word = structure(c(2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L,
4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L,
4L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 2L,
2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("heed", "hoed", "hoed ", "whod"
), class = "factor"), Vowel = structure(c(2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 2L, 2L,
2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L,
4L, 4L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 4L, 4L,
4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("i", "o", "o ",
"u"), class = "factor"), F1.Mean = c(564L, 564L, 564L, 564L,
564L, 394L, 394L, 394L, 394L, 394L, 564L, 564L, 564L, 564L,
394L, 394L, 394L, 564L, 564L, 564L, 564L, 394L, 394L, 394L,
394L, 564L, 564L, 564L, 394L, 394L, 394L, 394L, 398L, 398L,
398L, 398L, 398L, 564L, 564L, 564L, 564L, 564L, 398L, 398L,
398L, 398L, 564L, 564L, 564L, 564L, 398L, 398L, 398L, 564L,
564L, 564L, 564L, 398L, 398L, 398L, 398L, 564L, 564L, 564L,
564L, 564L, 564L, 564L, 564L, 394L, 394L, 394L, 394L, 394L,
564L, 564L, 564L, 564L, 394L, 394L, 394L, 564L, 564L, 564L,
564L, 394L, 394L, 394L, 394L, 564L, 564L, 564L, 394L, 394L,
394L, 394L, 398L, 398L, 398L, 398L, 398L, 564L, 564L, 564L,
564L, 564L, 398L, 398L, 398L, 398L, 564L, 564L, 564L, 564L,
398L, 398L, 398L, 564L, 564L, 564L, 564L, 398L, 398L, 398L,
398L, 564L, 564L, 564L, 627L, 627L, 614L, 614L, 614L, 614L,
566L, 566L, 432L, 432L, 327L, 327L, 415L, 415L, 393L, 393L,
627L, 627L, 614L, 614L, 614L, 614L, 566L, 566L, 432L, 432L,
327L, 327L, 415L, 415L, 393L, 393L, 397L, 397L, 292L, 292L,
417L, 417L, 398L, 398L, 627L, 627L, 614L, 614L, 614L, 614L,
566L, 566L, 397L, 397L, 292L, 292L, 417L, 417L, 398L, 398L,
627L, 627L, 614L, 614L, 614L, 614L, 566L, 566L), F2.Mean = c(1328L,
1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1496L,
1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1328L, 1328L,
1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1328L, 1328L, 1328L,
1496L, 1496L, 1496L, 1496L, 2969L, 2969L, 2969L, 2969L, 2969L,
1328L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 2969L,
1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 1328L, 1328L,
1328L, 1328L, 2969L, 2969L, 2969L, 2969L, 1328L, 1328L, 1328L,
1328L, 1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L,
1496L, 1328L, 1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1328L,
1328L, 1328L, 1328L, 1496L, 1496L, 1496L, 1496L, 1328L, 1328L,
1328L, 1496L, 1496L, 1496L, 1496L, 2969L, 2969L, 2969L, 2969L,
2969L, 1328L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L,
2969L, 1328L, 1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 1328L,
1328L, 1328L, 1328L, 2969L, 2969L, 2969L, 2969L, 1328L, 1328L,
1328L, 1250L, 1250L, 1247L, 1247L, 1247L, 1247L, 1357L, 1357L,
1292L, 1292L, 1157L, 1157L, 1746L, 1746L, 1455L, 1455L, 1250L,
1250L, 1247L, 1247L, 1247L, 1247L, 1357L, 1357L, 1292L, 1292L,
1157L, 1157L, 1746L, 1746L, 1455L, 1455L, 2828L, 2828L, 2763L,
2763L, 2721L, 2721L, 2969L, 2969L, 1250L, 1250L, 1247L, 1247L,
1247L, 1247L, 1357L, 1357L, 2828L, 2828L, 2763L, 2763L, 2721L,
2721L, 2969L, 2969L, 1250L, 1250L, 1247L, 1247L, 1247L, 1247L,
1357L, 1357L), Distance = c(16L, 54L, 55L, 29L, 76L, 13L,
25L, 43L, 66L, 43L, 34L, 50L, 4L, 31L, 72L, 67L, 83L, 25L,
74L, 56L, 11L, 52L, 44L, 5L, 20L, 34L, 25L, 81L, 80L, 58L,
82L, 52L, 36L, 58L, 25L, 7L, 24L, 16L, 54L, 55L, 29L, 76L,
28L, 56L, 18L, 30L, 34L, 50L, 4L, 31L, 42L, 40L, 14L, 25L,
74L, 56L, 11L, 13L, 23L, 38L, 30L, 34L, 25L, 81L, 16L, 54L,
55L, 29L, 76L, 13L, 25L, 43L, 66L, 43L, 34L, 50L, 4L, 31L,
72L, 67L, 83L, 25L, 74L, 56L, 11L, 52L, 44L, 5L, 20L, 34L,
25L, 81L, 80L, 58L, 82L, 52L, 36L, 58L, 25L, 7L, 24L, 16L,
54L, 55L, 29L, 76L, 28L, 56L, 18L, 30L, 34L, 50L, 4L, 31L,
42L, 40L, 14L, 25L, 74L, 56L, 11L, 13L, 23L, 38L, 30L, 34L,
25L, 81L, 8L, 7L, 48L, 19L, 110L, 115L, 22L, 27L, 22L, 61L,
39L, 52L, 103L, 87L, 3L, 17L, 8L, 7L, 48L, 19L, 110L, 115L,
22L, 27L, 22L, 61L, 39L, 52L, 103L, 87L, 3L, 17L, 52L, 46L,
17L, 13L, 37L, 4L, 24L, 25L, 8L, 7L, 48L, 19L, 110L, 115L,
22L, 27L, 52L, 46L, 17L, 13L, 37L, 4L, 24L, 25L, 8L, 7L,
48L, 19L, 110L, 115L, 22L, 27L), Included = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L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2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("Consistent", "Inconsistent"), class = "factor"),
Fake = c(477, 497, 560, 543, 549, 466, 486, 516, 525, 534,
496, 580, 587, 557, 517, 563, 524, 546, 547, 596, 632, 544,
497, 592, 544, 612, 642, 545, 574, 602, 532, 522, 519, 534,
580, 587, 557, 477, 497, 560, 543, 549, 574, 543, 603, 582,
496, 580, 587, 557, 511, 622, 604, 546, 547, 596, 632, 634,
556, 536, 588, 612, 642, 545, 477, 497, 560, 543, 549, 466,
486, 516, 525, 534, 496, 580, 587, 557, 517, 563, 524, 546,
547, 596, 632, 544, 497, 592, 544, 612, 642, 545, 574, 602,
532, 522, 519, 534, 580, 587, 557, 477, 497, 560, 543, 549,
574, 543, 603, 582, 496, 580, 587, 557, 511, 622, 604, 546,
547, 596, 632, 634, 556, 536, 588, 612, 642, 545, 452, 547,
510, 663, 470, 503, 600, 517, 491, 505, 641, 581, 520, 485,
517, 622, 452, 547, 510, 663, 470, 503, 600, 517, 491, 505,
641, 581, 520, 485, 517, 622, 510, 458, 558, 638, 483, 538,
577, 600, 452, 547, 510, 663, 470, 503, 600, 517, 510, 458,
558, 638, 483, 538, 577, 600, 452, 547, 510, 663, 470, 503,
600, 517), Check = c(20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0)), row.names = c(NA, -192L), class = "data.frame")

We can check multiple conditions with & or |. Here, we would need & because both the conditions should be satisfied
option1$Surgical <- ifelse(option1$Variability == "Single" &
option1$Duration > 625, option1$Duration - 20, option1$Duration)

You can do this directly without using ifelse :
option1$Surgical <- with(option1, Duration - (20 *
(Variability == 'Single' & Duration > 625)))
Here, we take advantage of the fact that
20 * TRUE #gives
#[1] 20
and
20 * FALSE #gives
#[1] 0
So whenever the condition (Variability == 'Single' & Duration > 625) is TRUE it subtracts 20 from Duration or else 0.

Related

summarise_each() with across() for dplyr package

I have this script, I want to know how I can replace summarise_each() with the across() function?
common_bw_elements = df %>%
group_by(range_of_commons = cut(common_IDs,
breaks= c(-Inf,0, 5, 10, 20, 30, 60, 100, 200, 300, 600, 1200, 1800, Inf))) %>%
summarise_each(funs(sum), sum_of_instances = frequent)
I am asking this, as I get the following message:
Warning message: summarise_each() is deprecated as of dplyr 0.7.0. Please use across() instead.
My code is very similar to the following post: summarize groups into intervals using dplyr
Any leads on this would be greatly appreciated.
For reference, you can use the following dput()
dput(df)
structure(list(common_IDs = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 17L, 18L, 25L, 26L, 27L, 37L, 51L, 55L, 56L, 63L, 68L, 69L, 70L, 71L, 74L, 76L, 81L, 84L, 86L, 87L, 89L, 90L, 91L, 92L, 101L,
103L, 108L, 109L, 110L, 113L, 114L, 115L, 116L, 129L, 130L, 131L, 133L, 135L, 136L, 137L, 138L, 139L, 141L, 152L, 153L, 154L, 177L, 178L, 190L, 191L, 196L, 199L, 202L, 203L, 208L, 209L, 210L, 211L, 213L, 214L, 215L, 216L, 218L, 219L, 222L, 223L, 229L, 230L, 231L,
232L, 239L, 251L, 252L, 254L, 257L, 264L, 265L, 271L, 272L, 273L, 275L, 276L, 277L, 280L, 293L, 294L, 297L, 298L, 299L, 300L, 301L, 304L, 317L, 320L, 337L, 346L, 347L, 364L, 371L, 373L, 386L, 387L, 389L, 412L, 417L, 419L, 420L, 432L, 440L, 441L, 442L, 443L, 451L,
452L, 453L, 455L, 456L, 457L, 458L, 462L, 463L, 464L, 469L, 470L, 474L, 476L, 477L, 478L, 487L, 488L, 492L, 1484L, 1534L, 1546L, 1561L, 1629L, 1642L, 1670L, 1672L, 1681L, 1698L, 1723L, 1725L,
1736L, 1738L, 1745L, 1753L, 1759L, 1764L, 1766L, 1767L, 1770L, 1772L, 1775L, 1776L, 1781L, 1784L, 1787L, 1791L, 1802L, 1807L, 1813L, 1815L, 1817L, 1821L, 1823L, 1825L, 1846L, 1850L, 1852L,
1853L, 1854L, 1857L, 1858L, 1859L, 1868L, 1899L, 1904L, 1911L, 1913L, 1977L, 1997L, 1999L, 2023L, 2079L),
frequent = c(81L, 75L, 10L, 17L, 4L, 4L, 33L, 13L, 31L, 3L, 19L, 22L, 6L, 1L, 11L, 2L,
1L, 1L, 3L, 14L, 1L, 2L, 1L, 14L, 1L, 9L, 6L, 9L, 2L, 5L, 13L, 4L, 4L, 1L, 4L, 1L, 3L, 1L, 6L, 2L, 1L, 3L, 2L, 5L, 2L, 1L, 17L, 5L, 4L, 4L, 1L, 4L, 7L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 6L,
16L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 13L, 6L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 5L, 1L, 3L, 1L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -193L))
You can use summarise since you are only summing one variable by group.
library(tidyverse)
common_bw_elements = df %>%
group_by(range_of_commons = cut(common_IDs,
breaks= c(-Inf,0, 5, 10, 20, 30, 60, 100, 200, 300, 600, 1200, 1800, Inf))) %>%
summarise(sum_of_instances = sum(frequent))
Output
range_of_commons sum_of_instances
<fct> <int>
1 (-Inf,0] 81
2 (0,5] 110
3 (5,10] 46
4 (10,20] 34
5 (20,30] 47
6 (30,60] 15
7 (60,100] 85
8 (100,200] 87
9 (200,300] 92
10 (300,600] 75
11 (1.2e+03,1.8e+03] 29
12 (1.8e+03, Inf] 28
If you had multiple columns to sum, then we would use across (or if you only had a few columns, then instead of everything(), you can provide a vector of column names (e.g., c(common_IDs, frequent)):
df %>%
group_by(range_of_commons = cut(common_IDs,
breaks= c(-Inf,0, 5, 10, 20, 30, 60, 100, 200, 300, 600, 1200, 1800, Inf))) %>%
summarise(across(everything(), ~ sum(.x))) %>%
rename(sum_of_instances = frequent)
Output
range_of_commons common_IDs sum_of_instances
<fct> <int> <int>
1 (-Inf,0] 0 81
2 (0,5] 15 110
3 (5,10] 13 46
4 (10,20] 35 34
5 (20,30] 78 47
6 (30,60] 199 15
7 (60,100] 1191 85
8 (100,200] 3928 87
9 (200,300] 9392 92
10 (300,600] 17290 75
11 (1.2e+03,1.8e+03] 47829 29
12 (1.8e+03, Inf] 48922 28

Categorical covariate with two way anova in R

This is a random sample of my data set:
structure(list(DTI_ID = structure(c(31L, 241L, 84L, 298L, 185L,
269L, 198L, 24L, 286L, 177L, 228L, 158L, 57L, 293L, 218L, 8L,
180L, 39L, 211L, 134L, 291L, 309L, 99L, 70L, 154L, 138L, 250L,
41L, 276L, 262L, 96L, 139L, 232L, 12L, 294L, 38L, 244L, 289L,
280L, 196L, 58L, 44L, 188L, 152L, 143L, 302L, 201L, 27L, 24L,
67L, 247L, 223L, 74L, 32L, 110L, 98L, 303L, 256L, 71L, 30L, 236L,
266L, 307L, 224L, 100L, 73L, 288L, 230L, 182L, 159L, 190L, 123L,
241L, 169L, 103L, 40L, 248L, 293L, 60L, 260L, 168L, 267L, 144L,
89L, 139L, 231L, 204L, 130L, 278L, 227L, 205L, 268L, 88L, 221L,
208L, 306L, 242L, 145L, 21L, 165L, 217L, 159L, 206L, 70L, 121L,
181L, 95L, 279L, 265L, 4L, 122L, 177L, 234L, 34L, 261L, 86L,
2L, 296L, 39L, 283L, 251L, 126L, 188L, 176L, 220L, 77L, 225L,
73L, 48L, 107L, 280L, 118L, 38L, 310L, 297L, 258L, 89L, 205L,
4L, 54L, 16L, 95L, 119L, 40L, 9L, 66L, 64L, 55L, 131L, 290L,
166L, 170L, 182L, 139L, 125L, 201L, 302L, 137L, 8L, 81L, 61L,
119L, 278L, 135L, 117L, 65L, 21L, 200L, 150L, 146L, 54L, 262L,
152L, 224L, 162L, 111L, 251L, 130L, 41L, 271L, 33L, 86L, 32L,
199L, 49L, 180L, 101L, 271L, 80L, 84L, 293L, 5L, 170L, 74L, 279L,
281L, 255L, 210L, 52L, 248L, 53L, 121L, 190L, 141L, 213L, 138L,
112L, 234L, 235L, 40L, 233L, 115L, 154L, 11L, 76L, 29L, 19L,
249L, 1L, 207L), .Label = c("5356", "5357", "5358", "5359", "5360",
"5363", "5373", "5381", "5383", "5386", "5395", "5397", "5400",
"5401", "5444", "5445", "5446", "5448", "5450", "5451", "5454",
"5472", "5473", "5475", "5476", "5477", "5478", "5480", "5481",
"5483", "5487", "5494", "5495", "5504", "5505", "5506", "5507",
"5508", "5509", "5513", "5514", "5515", "5516", "5517", "5518",
"5519", "5521", "5523", "5524", "5526", "5527", "5528", "5544",
"5545", "5546", "5547", "5551", "5552", "5553", "5554", "5555",
"5558", "5559", "5560", "5562", "5564", "5566", "5573", "5574",
"5575", "5576", "5577", "5578", "5579", "5584", "5585", "5587",
"5588", "5589", "5591", "5594", "5595", "5604", "5611", "5612",
"5613", "5615", "5616", "5619", "5620", "5621", "5622", "5626",
"5627", "5628", "5631", "5632", "5634", "5635", "5643", "5652",
"5653", "5654", "5655", "5656", "5657", "5659", "5660", "5661",
"5664", "5665", "5666", "5669", "5671", "5672", "5673", "5678",
"5680", "5688", "5689", "5690", "5691", "5692", "5698", "5699",
"5700", "5702", "5703", "5704", "5706", "5708", "5709", "5710",
"5730", "5731", "5732", "5733", "5734", "5735", "5739", "5740",
"5741", "5742", "5743", "5744", "5745", "5746", "5747", "5748",
"5749", "5750", "5753", "5754", "5755", "5766", "5767", "5776",
"5777", "5778", "5779", "5780", "5781", "5787", "5788", "5789",
"5790", "5791", "5792", "5793", "5797", "5798", "5799", "5800",
"5801", "5810", "5811", "5812", "5813", "5814", "5819", "5820",
"5821", "5822", "5823", "5824", "5825", "5827", "5828", "5829",
"5830", "5857", "5859", "5874", "5875", "5876", "5877", "5878",
"5879", "5883", "5884", "5886", "5887", "5888", "5889", "5890",
"5892", "5893", "5896", "5899", "5900", "5909", "5910", "5918",
"5919", "5920", "5921", "5922", "5923", "5927", "5929", "5931",
"5932", "5933", "5934", "5936", "5937", "5941", "5943", "5944",
"5949", "5950", "5951", "5952", "5956", "5957", "5958", "5959",
"5971", "5972", "5973", "5976", "5979", "5980", "5981", "6001",
"6002", "6003", "6004", "6005", "6009", "6027", "6028", "6033",
"6042", "6054", "6063", "6067", "6073", "6076", "6077", "6078",
"6079", "6080", "6081", "6082", "6083", "6098", "6102", "6103",
"6104", "6105", "6106", "6107", "6111", "6119", "6133", "6146",
"6147", "6157", "6158", "6160", "6161", "6162", "6163", "6164",
"6165", "6166", "6167", "6168", "6169", "6170", "6171", "6172",
"6173", "6174", "6175", "6190", "6193", "6195", "6196", "6197",
"6208", "6228", "6229", "6232", "6255", "6268", "6269", "6270",
"6275"), class = "factor"), Gender = structure(c(2L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L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 1L, 2L, 1L,
4L, 4L, 1L, 2L, 1L, 2L, 4L, 2L, 4L, 1L, 4L, 2L, 4L, 3L, 2L
), .Label = c("A", "B","C", "D"), class = "factor"),
value = c(0.326713741, 0.349206239, 0.365954667,
0.313958377, 0.480487555, 0.431199849, 0.446729183, 0.337009728,
0.331222087, 0.386937141, 0.372758657, 0.305083066, 0.504718482,
0.414191663, 0.40949735, 0.271525055, 0.30009532, 0.50117749,
0.387669057, 0.330797315, 0.390679717, 0.452181876, 0.423188657,
0.396808296, 0.388510793, 0.298505336, 0.412985921, 0.327000797,
0.304242313, 0.277513236, 0.394773901, 0.4322685, 0.440891623,
0.439061254, 0.453015536, 0.385896087, 0.452299237, 0.296923041,
0.443324417, 0.420699686, 0.282610774, 0.303566545, 0.535346806,
0.393591255, 0.32561186, 0.309230596, 0.417596817, 0.281766504,
0.445347071, 0.353419632, 0.354420125, 0.429613769, 0.385733992,
0.155136898, 0.485385537, 0.439544022, 0.436584443, 0.458706915,
0.600399196, 0.440390527, 0.362952292, 0.37253055, 0.37306264,
0.371298164, 0.469741255, 0.573943496, 0.283266962, 0.391182601,
0.663566113, 0.517713368, 0.327498972, 0.353969425, 0.443648636,
0.449972481, 0.434426159, 0.305042148, 0.422493547, 0.194572225,
0.331083208, 0.418288261, 0.447215647, 0.429001331, 0.339149892,
0.336879104, 0.471237898, 0.408330619, 0.393405557, 0.486086488,
0.427713692, 0.379242182, 0.40456596, 0.326695889, 0.393235713,
0.452374548, 0.332855165, 0.323469192, 0.396484613, 0.372199923,
0.257353246, 0.405249774, 0.326494843, 0.420468688, 0.335335255,
0.267627925, 0.379383296, 0.338241786, 0.416064918, 0.381003618,
0.284006208, 0.442705005, 0.494199812, 0.464447916, 0.370418996,
0.293953657, 0.34482345, 0.47208631, 0.378798842, 0.407261223,
0.34767586, 0.424341202, 0.434532404, 0.342623293, 0.628901243,
0.381492049, 0.540111601, 0.392371207, 0.459349483, 0.373172134,
0.270272404, 0.413454324, 0.375994682, 0.470298111, 0.340463549,
0.31613645, 0.470312864, 0.410651028, 0.276164204, 0.341546267,
0.402167588, 0.465735435, 0.434102625, 0.328114063, 0.394582212,
0.331681252, 0.387562275, 0.3989245, 0.44939962, 0.29586333,
0.398924828, 0.559520543, 0.392099082, 0.589552164, 0.397368163,
0.375135392, 0.348508835, 0.447002649, 0.407775551, 0.404435992,
0.666776299, 0.265039146, 0.25311482, 0.354386091, 0.44051528,
0.416727781, 0.460624784, 0.455415428, 0.445090771, 0.502343714,
0.393426061, 0.463244319, 0.345586747, 0.291874498, 0.564393103,
0.400276631, 0.41512531, 0.308440536, 0.373545259, 0.272377819,
0.434890926, 0.358394623, 0.414819628, 0.761894882, 0.409700364,
0.403811544, 0.469092041, 0.397044837, 0.312479883, 0.294876397,
0.314414322, 0.428720832, 0.329074681, 0.311423391, 0.444689006,
0.254723012, 0.248710752, 0.270434052, 0.416304022, 0.38875562,
0.396840513, 0.296386898, 0.454476953, 0.474986047, 0.427072734,
0.270839244, 0.426266223, 0.586857438, 0.348018169, 0.386638522,
0.349321723, 0.418692261, 0.295630395, 0.463439822, 0.286190838,
0.336389571, 0.422766507, 0.231764346, 0.358636618, 0.562871873,
0.381515294, 0.28637746)), row.names = c(961L, 1171L, 84L,
608L, 805L, 889L, 818L, 334L, 596L, 177L, 848L, 468L, 987L, 293L,
218L, 318L, 1110L, 969L, 211L, 1064L, 1221L, 1239L, 99L, 70L,
1084L, 448L, 250L, 351L, 586L, 572L, 1026L, 139L, 232L, 632L,
914L, 658L, 1174L, 909L, 280L, 1126L, 368L, 354L, 808L, 1082L,
453L, 612L, 201L, 337L, 644L, 67L, 1177L, 1153L, 694L, 962L,
1040L, 98L, 923L, 1186L, 691L, 650L, 236L, 886L, 307L, 224L,
100L, 1003L, 598L, 230L, 1112L, 779L, 500L, 433L, 861L, 1099L,
103L, 350L, 248L, 1223L, 370L, 260L, 788L, 267L, 454L, 399L,
1069L, 231L, 204L, 1060L, 1208L, 847L, 205L, 578L, 88L, 221L,
518L, 616L, 242L, 1075L, 951L, 165L, 527L, 1089L, 206L, 380L,
431L, 801L, 1025L, 279L, 575L, 624L, 1052L, 1107L, 854L, 344L,
1191L, 86L, 2L, 916L, 659L, 903L, 251L, 436L, 1118L, 796L, 1150L,
77L, 1155L, 693L, 358L, 107L, 1210L, 1048L, 968L, 620L, 1227L,
258L, 1019L, 515L, 4L, 674L, 16L, 405L, 739L, 660L, 629L, 66L,
64L, 365L, 1061L, 1220L, 166L, 1100L, 182L, 759L, 745L, 1131L,
302L, 757L, 938L, 391L, 371L, 119L, 278L, 135L, 117L, 995L, 21L,
1130L, 150L, 1076L, 364L, 1192L, 772L, 844L, 782L, 421L, 561L,
440L, 41L, 1201L, 963L, 706L, 652L, 1129L, 669L, 180L, 1031L,
581L, 390L, 704L, 603L, 625L, 170L, 384L, 899L, 591L, 255L, 830L,
672L, 558L, 983L, 121L, 190L, 451L, 213L, 1068L, 1042L, 234L,
545L, 40L, 543L, 1045L, 464L, 941L, 76L, 959L, 329L, 1179L, 621L,
517L), class = "data.frame")
Which looks like:
# A tibble: 10 x 5
DTI_ID Gender Age ROI value
<fct> <fct> <fct> <fct> <dbl>
1 5927 Male Old A 0.395
2 5634 Male Old C 0.433
3 5547 Female Old B 0.257
4 5979 Male Old C 0.404
5 5660 Male Old A 0.398
6 5876 Female Old D 0.426
7 5518 Male Old A 0.404
8 6001 Female Old D 0.392
9 6042 Male Old A 0.388
10 5821 Male Old A 0.344
ROI is a region of interest within each subject, so all subjects have all 4 ROIs.
I would like to calculate a 2-way ANCOVA 4(ROIs [a/b/c/d] - within) x 2 (Age [young/old] - between) + Gender [covariate] to determine the interaction effects of age and ROI on value, controlling for Gender.
To do that, I calculated:
#2-way ANOVA
res.aov2 <- df %>%
anova_test(value ~ Gender + Age*ROI, within = ROI, wid= DTI_ID)
get_anova_table(res.aov2)
which works fine and outputs:
ANOVA Table (type II tests)
Effect DFn DFd F p p<.05 ges
1 Gender 1 1227 5.196 2.30e-02 * 0.004000
2 Age 1 1227 0.732 3.92e-01 0.000596
3 ROI 3 1227 228.933 6.13e-118 * 0.359000
4 Age:ROI 3 1227 22.258 4.90e-14 * 0.052000
I then want to run a multiple comparisons to generate p values that I can graph over boxplots for visualization of the analyses.
I am using emmeans_test:
# Pairwise comparisons
pwc2 <- df %>%
group_by(ROI) %>%
emmeans_test(value ~ Age, covariate = Gender,
p.adjust.method = "bonferroni")
but receive the error:
Error in contrast.emmGrid(res.emmeans, by = grouping.vars, method = method, : Nonconforming number of contrast coefficients
I cannot figure out why, as the pairwise comparison works fine when I remove the covariate. Does it have to do with a categorical variable being used as a covariate? I am stuck and want to make sure I am reporting the appropriate p-values in my chart.
Adding Gender to group_by as well, allowed the code to run properly.

Identify function is not accurate in R

Here is the problem:
When I use cook's distance to check influential points in SLR, I used two methods.
First one:
plot(mortality.model, which = 4)
This one gives me the correct answer.
Second one:
plot(cooks.distance(mortality.model), type = 'p')
identify(cooks.distance(mortality.model))
This one gives me the wrong answer, but very close to the correct answer.
Read the data set:
df.mortality <- read.csv("mortality.csv", header = TRUE)
Build the model:
mortality.model <- lm(log(infant) ~ log(income))
By the way, the dataset has NA values. If you would like to see the dataset, I could email it to you.
The dput result:
structure(list(X = structure(c(4L, 5L, 7L, 15L, 23L, 29L, 30L, 101L,
41L,43L, 46L, 61L, 62L, 66L, 73L, 79L, 86L, 87L, 10L, 97L, 2L, 25L, 38L,
39L, 40L, 52L, 65L, 75L, 100L, 3L, 9L, 18L, 19L, 21L, 24L, 32L, 33L, 42L,
45L, 50L, 55L, 58L, 63L, 68L, 71L, 77L, 83L, 89L, 93L, 94L, 99L, 103L,
105L, 8L, 14L, 20L, 26L, 27L, 31L, 36L, 44L, 47L, 80L, 51L, 59L, 69L, 70L,
72L, 88L, 91L, 95L, 81L, 1L, 6L,11L, 12L, 13L, 16L, 17L, 22L, 28L, 34L,
35L, 37L, 48L, 49L, 53L, 54L, 56L, 57L, 60L, 64L, 67L, 74L, 76L, 78L, 84L,
85L, 90L, 92L, 96L, 98L, 82L, 102L, 104L), .Label = c("Afganistan",
"Algeria", "Argentina", "Australia", "Austria", "Bangladesh","Belgium",
"Bolivia", "Brazil", "Britain", "Burma","Burundi","Cambodia","Cameroon",
"Canada", "Central.African.Republic", "Chad","Chile", "Colombia","Congo",
"Costa.Rica", "Dahomey", "Denmark", "Dominican.Republic", "Ecuador",
"Egypt", "El.Salvador", "Ethiopia", "Finland", "France", "Ghana",
"Greece", "Guatemala", "Guinea", "Haiti", "Honduras", "India",
"Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Ivory.Coast",
"Jamaica", "Japan", "Jordan", "Kenya", "Laos", "Lebanon", "Liberia",
"Libya", "Madagascar", "Malawi", "Malaysia", "Mali", "Mauritania",
"Mexico", "Moroco", "Nepal", "Netherlands", "New.Zealand", "Nicaragua",
"Niger", "Nigeria", "Norway", "Pakistan", "Panama", "Papua.New.Guinea",
"Paraguay", "Peru", "Philippines", "Portugal", "Rwanda", "Saudi.Arabia",
"Sierra.Leone", "Singapore", "Somalia", "South.Africa", "South.Korea",
"South.Vietnam", "Southern.Yemen", "Spain", "Sri.Lanka", "Sudan",
"Sweden", "Switzerland", "Syria", "Taiwan", "Tanzania", "Thailand",
"Togo", "Trinidad.and.Tobago", "Tunisia", "Turkey", "Uganda",
"United.States", "Upper.Volta", "Uruguay", "Venezuela", "West.Germany",
"Yemen", "Yugoslavia", "Zaire", "Zambia"), class = "factor"),
income = c(3426L, 3350L, 3346L, 4751L, 5029L, 3312L, 3403L,
5040L, 2009L, 2298L, 3292L, 4103L, 3723L, 4102L, 956L, 1000L,
5596L, 2963L, 2503L, 5523L, 400L, 250L, 110L, 1280L, 560L,
3010L, 220L, 1530L, 1240L, 1191L, 425L, 590L, 426L, 725L,
406L, 1760L, 302L, 2526L, 727L, 631L, 295L, 684L, 507L, 754L,
335L, 1268L, 1256L, 261L, 732L, 434L, 799L, 406L, 310L, 200L,
100L, 281L, 210L, 319L, 217L, 284L, 387L, 334L, 344L, 197L,
279L, 477L, 347L, 230L, 334L, 210L, 435L, 130L, 75L, 100L,
73L, 68L, 123L, 122L, 70L, 81L, 79L, 79L, 100L, 93L, 169L,
71L, 120L, 130L, 50L, 174L, 90L, 70L, 102L, 61L, 148L, 85L,
162L, 125L, 120L, 160L, 134L, 82L, 96L, 77L, 118L), infant = c(26.7,
23.7, 17, 16.8, 13.5, 10.1, 12.9, 20.4, 17.8, 25.7, 11.7,
11.6, 16.2, 11.3, 44.8, 71.5, 9.6, 12.8, 17.5, 17.6, 86.3,
78.5, 125, NA, 28.1, 300, 58, 650, 51.7, 59.6, 170, 78, 62.8,
54.4, 48.8, 27.8, 79.1, 22.1, 26.2, 13.6, 32, 60.9, 46, 34.1,
65.1, 20.4, 15.1, 19.1, 26.2, 76.3, 40.4, 43.3, 259, 60.4,
137, 180, 114, 58.2, 63.7, 39.3, 138, 21.3, 58, 159.2, 149,
10.2, 38.6, 67.9, 21.7, 27, 153, 100, 400, 124.3, 200, 150,
100, 190, 160, 109.6, 84.2, 216, NA, 60.6, 55, NA, 102, 148.3,
120, 187, NA, 200, 124.3, 132.9, 170, 158, 45.1, 129.4, 162.5,
127, 160, 180, 80, 50, 104), region = structure(c(3L, 4L,
4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 1L, 4L,
4L, 4L, 2L, 1L, 2L, 3L, 3L, 3L, 1L, 1L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 4L,
3L, 2L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 3L,
3L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L,
1L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L,
3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L), .Label = c("Africa",
"Americas", "Asia", "Europe"), class = "factor"), oil = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("no",
"yes"), class = "factor")), class = "data.frame", row.names = c(NA,
-105L))
Thanks!
Here are results:The correct answer The wrong answer
Could anyone explain why it happened?

How do I maintain an order after creating a list from a data.frame

I have a data.frame in R, myTable consisting of a date , myDate; integer, myCount and factor ,myCategory. It is sorted by date
I need to create a list from the data.frame (in order to plot a Highcharts graph in rCharts)
mySeries <- lapply(split(myTable, myTable$myCategory), function(x) {
res <- lapply(split(x, rownames(x)), as.list)
names(res) <- NULL
return(res)
})
However, the resulting list is not ordered by myDate within myCategory. How can this be achieved?
myTable shown below
TIA
myTable <- structure(list(myDate = structure(c(15642, 15649, 15656, 15663,
15670, 15670, 15677, 15677, 15684, 15684, 15691, 15691, 15698,
15698, 15705, 15705, 15712, 15712, 15719, 15719, 15726, 15726,
15733, 15733, 15740, 15740, 15747, 15747, 15754, 15754, 15761,
15761, 15768, 15768, 15775, 15775, 15782, 15782, 15789, 15789,
15796, 15796, 15803, 15803, 15810, 15810, 15817, 15817, 15824,
15824, 15831, 15831, 15838, 15838, 15845, 15845, 15852, 15852,
15859, 15859, 15866, 15866), class = "Date"), myCount = c(142L,
338L, 350L, 324L, 490L, 4L, 567L, 178L, 414L, 113L, 489L, 195L,
241L, 168L, 355L, 164L, 1215L, 253L, 2171L, 222L, 1916L, 437L,
1820L, 420L, 1897L, 356L, 1632L, 381L, 1571L, 464L, 1835L, 620L,
1348L, 500L, 1398L, 448L, 2668L, 461L, 2510L, 456L, 2654L, 733L,
1977L, 617L, 1645L, 523L, 1857L, 550L, 1247L, 477L, 1286L, 560L,
1168L, 719L, 1264L, 649L, 1162L, 682L, 1149L, 801L, 1450L, 1018L
), myCategory = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("myDate",
"myCount", "myCategory"), row.names = c(62L, 59L, 56L, 55L, 51L,
52L, 47L, 48L, 49L, 50L, 53L, 54L, 60L, 61L, 57L, 58L, 45L, 46L,
39L, 40L, 1L, 2L, 23L, 24L, 35L, 36L, 41L, 42L, 31L, 32L, 27L,
28L, 11L, 12L, 33L, 34L, 37L, 38L, 43L, 44L, 17L, 18L, 15L, 16L,
7L, 8L, 3L, 4L, 29L, 30L, 19L, 20L, 13L, 14L, 9L, 10L, 21L, 22L,
I don't understand why do you want to split your data in order to plot it. I would try to plot it without splitting. Also, as mentioned your structure is truncated due to a bad copy and paste. I had to remove rownames part from your structure in order to use it. IFor example, here I am using ggplot2 to plot the data without splitting. For example:
library(ggplot2)
ggplot(myTable)+
geom_line(aes(x=myDate,y=myCount,group=myCategory,color=myCategory))
or splitting using faceting like this :
ggplot(myTable) +
geom_line(aes(x=myDate,y=myCount)) +
facet_wrap(~myCategory)
EDIT
The same thing can be obtained using rCharts package:
library(rCharts)
rPlot(myCount ~ myDate | myCategory, data = myTable,
color = 'myCategory', type = 'line')
Here my data is :
structure(list(myDate = structure(c(15642, 15649, 15656, 15663,
15670, 15670, 15677, 15677, 15684, 15684, 15691, 15691, 15698,
15698, 15705, 15705, 15712, 15712, 15719, 15719, 15726, 15726,
15733, 15733, 15740, 15740, 15747, 15747, 15754, 15754, 15761,
15761, 15768, 15768, 15775, 15775, 15782, 15782, 15789, 15789,
15796, 15796, 15803, 15803, 15810, 15810, 15817, 15817, 15824,
15824, 15831, 15831, 15838, 15838, 15845, 15845, 15852, 15852,
15859, 15859, 15866, 15866), class = "Date"), myCount = c(142L,
338L, 350L, 324L, 490L, 4L, 567L, 178L, 414L, 113L, 489L, 195L,
241L, 168L, 355L, 164L, 1215L, 253L, 2171L, 222L, 1916L, 437L,
1820L, 420L, 1897L, 356L, 1632L, 381L, 1571L, 464L, 1835L, 620L,
1348L, 500L, 1398L, 448L, 2668L, 461L, 2510L, 456L, 2654L, 733L,
1977L, 617L, 1645L, 523L, 1857L, 550L, 1247L, 477L, 1286L, 560L,
1168L, 719L, 1264L, 649L, 1162L, 682L, 1149L, 801L, 1450L, 1018L
), myCategory = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("myDate",
"myCount", "myCategory"), row.names = c(NA, -62L), class = "data.frame")
I have heard off-forum of the solution which is to add an appropriate order within the lapply that creates the list
mySeries <- lapply(split(myTable, myTable$myCategory), function(x) {
res <- lapply(split(x, rownames(x)), as.list)
names(res) <- NULL
res <- res[order(sapply(res, function(x) x$myDate))] ## Provides Solution
return(res)
})

Add duplicate xyplot in lattice

This is my first post so I'm not sure if I've done this correctly. I'd like to add a second xyplot to plot1 so that it represents the same data as the bubble plot (Percent~Distance and grouped by Forest_type), but without the size of the bubble based on dat$Pixels. IE: a dot within the bubble. All dots should be black and very small. I tried as.layer with LatticeExtra, but couldn't get it to work with my limited panel and function experience. I obtained this original code from someone much more experienced and have only been able to modify it slightly.
Thanks for any suggestions.
library(lattice)
mykey <- list(x = .7, y = .7, corner = c(0,1), text = list(lab = c("D", "C", "M")),
points = list(col = c(2,3,4), pch = 1) )
fontsize <- trellis.par.get("par.main.text")
fontsize$font <- 1
trellis.par.set("par.main.text", fontsize)
trellis.par.get()
plot1<-xyplot(Percent ~ Distance, key = mykey, cex = dat$Pixels / 15000000,
col = dat$Forest_type+1,
xlab="Distance", ylab="Percent of pixels",
data = dat, panel = function(...){
panel.xyplot(...)
})
print(plot1, position=c(0, .5, 1, 1))
Data to reproduce/use are below:
dat <- structure(list(Pixels = c(51442200L, 16201800L, 9679500L, 8954100L,
4332600L, 4024800L, 2843100L, 2707200L, 2635200L, 1754100L, 1865700L,
1467000L, 1575900L, 1253700L, 1061100L, 1205100L, 1045800L, 909000L,
822600L, 732600L, 837900L, 676800L, 600300L, 538200L, 504900L,
414000L, 316800L, 318600L, 260100L, 261900L, 214200L, 189900L,
182700L, 178200L, 150300L, 135000L, 117000L, 103500L, 90000L,
95400L, 68400L, 79200L, 69300L, 54900L, 72000L, 60300L, 55800L,
41400L, 41400L, 37800L, 24300L, 25200L, 32400L, 36000L, 28800L,
19800L, 18900L, 16200L, 16200L, 60586200L, 25074900L, 18993600L,
21714300L, 13090500L, 13913100L, 10777500L, 11007900L, 11732400L,
8385300L, 9188100L, 7379100L, 8556300L, 7248600L, 6276600L, 7616700L,
6766200L, 6323400L, 5963400L, 5404500L, 6618600L, 5781600L, 5630400L,
5091300L, 5515200L, 4810500L, 4392900L, 4674600L, 4113900L, 4517100L,
3974400L, 3650400L, 3759300L, 3761100L, 3456000L, 3180600L, 2963700L,
2999700L, 2619000L, 2723400L, 2321100L, 2286000L, 2167200L, 1925100L,
1906200L, 1649700L, 1658700L, 1561500L, 1567800L, 1494900L, 1378800L,
1384200L, 1219500L, 1257300L, 1220400L, 1098000L, 1133100L, 959400L,
1044900L, 3713400L, 1295100L, 827100L, 892800L, 549000L, 561600L,
440100L, 432900L, 461700L, 319500L, 344700L, 248400L, 277200L,
221400L, 185400L, 227700L, 222300L, 206100L, 189900L, 177300L,
209700L, 189900L, 170100L, 156600L, 170100L, 163800L, 153900L,
169200L, 148500L, 185400L, 162000L, 165600L, 176400L, 149400L,
128700L, 111600L, 99000L, 83700L, 60300L, 48600L, 32400L, 27000L,
12600L, 6300L, 5400L, 8100L, 9000L, 11700L, 15300L, 16200L, 11700L,
10800L, 6300L, 5400L, 6300L, 9900L, 14400L, 15300L, 15300L),
Forest_type = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), Distance = c(30L, 60L, 90L, 120L, 150L, 180L, 210L,
240L, 270L, 300L, 330L, 360L, 390L, 420L, 450L, 480L, 510L,
540L, 570L, 600L, 630L, 660L, 690L, 720L, 750L, 780L, 810L,
840L, 870L, 900L, 930L, 960L, 990L, 1020L, 1050L, 1080L,
1110L, 1140L, 1170L, 1200L, 1230L, 1260L, 1290L, 1320L, 1350L,
1380L, 1410L, 1440L, 1470L, 1500L, 1530L, 1560L, 1590L, 1620L,
1650L, 1680L, 1710L, 1740L, 1770L, 30L, 60L, 90L, 120L, 150L,
180L, 210L, 240L, 270L, 300L, 330L, 360L, 390L, 420L, 450L,
480L, 510L, 540L, 570L, 600L, 630L, 660L, 690L, 720L, 750L,
780L, 810L, 840L, 870L, 900L, 930L, 960L, 990L, 1020L, 1050L,
1080L, 1110L, 1140L, 1170L, 1200L, 1230L, 1260L, 1290L, 1320L,
1350L, 1380L, 1410L, 1440L, 1470L, 1500L, 1530L, 1560L, 1590L,
1620L, 1650L, 1680L, 1710L, 1740L, 1770L, 30L, 60L, 90L,
120L, 150L, 180L, 210L, 240L, 270L, 300L, 330L, 360L, 390L,
420L, 450L, 480L, 510L, 540L, 570L, 600L, 630L, 660L, 690L,
720L, 750L, 780L, 810L, 840L, 870L, 900L, 930L, 960L, 990L,
1020L, 1050L, 1080L, 1110L, 1140L, 1170L, 1200L, 1230L, 1260L,
1290L, 1320L, 1350L, 1380L, 1410L, 1440L, 1470L, 1500L, 1530L,
1560L, 1590L, 1620L, 1650L, 1680L, 1710L, 1740L, 1770L),
Percent = c(44.44565403, 38.05758742, 32.81164195, 28.37059427,
24.10736642, 21.75626368, 20.22018818, 19.13486005, 17.77022516,
16.77136219, 16.36794315, 16.1306284, 15.13920111, 14.37119571,
14.10455796, 13.31675783, 13.01669094, 12.22020569, 11.79202684,
11.60205245, 10.92979573, 10.18004603, 9.378515186, 9.301602115,
8.156440826, 7.683313847, 6.51369356, 6.171548117, 5.751243781,
5.275562001, 4.923458833, 4.740507751, 4.436188811, 4.358353511,
4.024096386, 3.93907563, 3.679592414, 3.24767015, 3.249918752,
3.327055869, 2.824228911, 3.31075997, 3.081232493, 2.763932941,
3.629764065, 3.509690938, 3.237597911, 2.564102564, 2.548476454,
2.440441604, 1.717557252, 1.774397972, 2.575107296, 2.772002772,
2.29390681, 1.755786113, 1.62037037, 1.634877384, 1.505016722,
52.34599773, 58.90025792, 64.38464824, 68.80061595, 72.83789874,
75.20797859, 76.64981118, 77.80534351, 79.11634399, 80.17382325,
80.60797473, 81.13805047, 82.1978212, 83.09089033, 83.43103242,
84.16708105, 84.21642209, 85.00907441, 85.48574377, 85.59007982,
86.33482038, 86.96358468, 87.9640045, 87.99191165, 89.09566735,
89.27676633, 90.32198372, 90.55090656, 90.96517413, 90.98984772,
91.35291684, 91.12558976, 91.28059441, 91.98767334, 92.53012048,
92.80462185, 93.20690631, 94.12595312, 94.57263568, 94.97802888,
95.83797845, 95.56057186, 96.35854342, 96.91889443, 96.09800363,
96.01885804, 96.24020888, 96.71125975, 96.50969529, 96.51365485,
97.45547074, 97.46514575, 96.9241774, 96.81219681, 97.20430108,
97.36632083, 97.14506173, 96.82107175, 97.0735786, 3.208348237,
3.042154666, 2.803709805, 2.82878978, 3.054734839, 3.035757723,
3.13000064, 3.059796438, 3.113430843, 3.05481456, 3.024082116,
2.731321128, 2.662977693, 2.537913959, 2.464409618, 2.516161114,
2.766886972, 2.770719903, 2.72222939, 2.807867731, 2.735383893,
2.856369297, 2.657480315, 2.706486234, 2.747891829, 3.039919826,
3.164322724, 3.277545328, 3.28358209, 3.734590283, 3.723624328,
4.133902494, 4.283216783, 3.653973145, 3.445783133, 3.256302521,
3.113501274, 2.62637673, 2.177445564, 1.694915254, 1.337792642,
1.128668172, 0.56022409, 0.317172633, 0.272232305, 0.471451021,
0.522193211, 0.724637681, 0.941828255, 1.045903544, 0.82697201,
0.760456274, 0.500715308, 0.415800416, 0.501792115, 0.877893057,
1.234567901, 1.544050863, 1.421404682), div2erroftot = c(4.966434556,
1.564186201, 0.934497422, 0.864464421, 0.418286433, 0.388570197,
0.27448418, 0.261363854, 0.254412687, 0.169347789, 0.180122097,
0.141630014, 0.152143653, 0.121037184, 0.102442814, 0.116345146,
0.100965691, 0.087758475, 0.079417075, 0.070728117, 0.080894198,
0.065340963, 0.057955349, 0.051959968, 0.048745054, 0.039969206,
0.030585132, 0.030758911, 0.025111088, 0.025284867, 0.02067972,
0.018333701, 0.017638585, 0.017204137, 0.01451056, 0.013033437,
0.011295645, 0.009992302, 0.008688958, 0.009210295, 0.006603608,
0.007646283, 0.006690498, 0.005300264, 0.006951166, 0.005821602,
0.005387154, 0.003996921, 0.003996921, 0.003649362, 0.002346019,
0.002432908, 0.003128025, 0.003475583, 0.002780467, 0.001911571,
0.001824681, 0.001564012, 0.001564012, 5.849232678, 2.42083056,
1.833717675, 2.096384872, 1.263808926, 1.343226002, 1.040502708,
1.062746441, 1.132692552, 0.809550207, 0.887055712, 0.712407658,
0.826059227, 0.699808669, 0.605967924, 0.735346507, 0.653235855,
0.610486182, 0.57573035, 0.521771922, 0.638985964, 0.558178655,
0.543581206, 0.491534348, 0.53245934, 0.4644248, 0.424108035,
0.451304473, 0.397172266, 0.436098797, 0.383704381, 0.352424132,
0.362937771, 0.363111551, 0.333655983, 0.307067772, 0.286127384,
0.289602967, 0.252848675, 0.262927866, 0.224088224, 0.220699531,
0.209230106, 0.185856809, 0.184032128, 0.159268598, 0.160137494,
0.15075342, 0.151361647, 0.144323591, 0.133114835, 0.133636172,
0.11773538, 0.121384742, 0.117822269, 0.106005286, 0.10939398,
0.092624291, 0.100878801, 0.358506403, 0.125034104, 0.079851523,
0.086194462, 0.053002643, 0.054219097, 0.042489004, 0.041793887,
0.044574354, 0.030845801, 0.033278709, 0.023981524, 0.02676199,
0.021374836, 0.017899253, 0.021983063, 0.021461726, 0.019897714,
0.018333701, 0.017117247, 0.020245272, 0.018333701, 0.01642213,
0.015118787, 0.01642213, 0.015813903, 0.014858118, 0.016335241,
0.014336781, 0.017899253, 0.015640124, 0.015987683, 0.017030357,
0.01442367, 0.01242521, 0.010774308, 0.009557854, 0.008080731,
0.005821602, 0.004692037, 0.003128025, 0.002606687, 0.001216454,
0.000608227, 0.000521337, 0.000782006, 0.000868896, 0.001129565,
0.001477123, 0.001564012, 0.001129565, 0.001042675, 0.000608227,
0.000521337, 0.000608227, 0.000955785, 0.001390233, 0.001477123,
0.001477123)), .Names = c("Pixels", "Forest_type", "Distance",
"Percent", "div2erroftot"), class = "data.frame", row.names = c(NA,
-177L))
If I understand you correctly, this is simple enough that it can be handled with a slightly modified panel function.
The panel function below first runs panel.xyplot(x,y,...) to get the plot you already have, and then adds small black points to it using the function lpoints(), which is just lattice's grid-based version of the base graphics function points().
Try this:
plot1<-xyplot(Percent ~ Distance, key = mykey, cex = dat$Pixels / 15000000,
col = dat$Forest_type+1,
xlab="Distance", ylab="Percent of pixels",
data = dat,
panel = function(x,y,...){
panel.xyplot(x,y,...)
lpoints(x, y, col="black", pch=16, cex=0.01)
})
print(plot1, position=c(0, .5, 1, 1))

Resources