Can't covert from <fctr> to <date> adequately - r

This is a little strange. I converted data from .csv to .xts other times before, but this time for some reasons cannot.
Here is my data set (dput() of half the real data set, since the complete one was out of characters limits. And yeah, the problem persists):
structure(list(time = structure(c(347L, 369L, 391L, 413L, 435L,
457L, 479L, 501L, 522L, 543L, 564L, 585L, 605L, 624L, 641L, 12L,
33L, 54L, 75L, 96L, 117L, 138L, 159L, 180L, 201L, 222L, 243L,
264L, 285L, 306L, 327L, 349L, 371L, 393L, 415L, 437L, 459L, 481L,
503L, 524L, 545L, 566L, 587L, 607L, 626L, 643L, 14L, 35L, 56L,
77L, 98L, 119L, 140L, 161L, 182L, 203L, 224L, 245L, 266L, 287L,
308L, 329L, 351L, 373L, 395L, 417L, 439L, 461L, 483L, 505L, 526L,
547L, 568L, 589L, 609L, 628L, 16L, 37L, 58L, 79L, 100L, 121L,
142L, 163L, 184L, 205L, 226L, 247L, 268L, 289L, 310L, 331L, 353L,
375L, 397L, 419L, 441L, 463L, 485L, 507L, 528L, 549L, 570L, 591L,
611L, 630L, 645L, 18L, 39L, 60L, 81L, 102L, 123L, 144L, 165L,
186L, 207L, 228L, 249L, 270L, 291L, 312L, 333L, 355L, 377L, 399L,
421L, 443L, 465L, 487L, 509L, 530L, 551L, 572L, 593L, 613L, 632L,
20L, 41L, 62L, 83L, 104L, 125L, 146L, 167L, 188L, 209L, 230L,
251L, 272L, 293L, 314L, 335L, 357L, 379L, 401L, 423L, 445L, 467L,
489L, 511L, 532L, 553L, 574L, 595L, 615L, 634L, 647L, 1L, 22L,
43L, 64L, 85L, 106L, 127L, 148L, 169L, 190L, 211L, 232L, 253L,
274L, 295L, 316L, 337L, 359L, 381L, 403L, 425L, 447L, 469L, 491L,
513L, 534L, 555L, 576L, 597L, 617L, 636L, 3L, 24L, 45L, 66L,
87L, 108L, 129L, 150L, 171L, 192L, 213L, 234L, 255L, 276L, 297L,
318L, 339L, 361L, 383L, 405L, 427L, 449L, 471L, 493L, 515L, 536L,
557L, 578L, 5L, 26L, 47L, 68L, 89L, 110L, 131L, 152L, 173L, 194L,
215L, 236L, 257L, 278L, 299L, 320L, 341L, 363L, 385L, 407L, 429L,
451L, 473L, 495L, 517L, 538L, 559L, 580L, 600L, 619L, 638L, 7L,
28L, 49L, 70L, 91L, 112L, 133L, 154L, 175L, 196L, 217L, 238L,
259L, 280L, 301L, 322L, 343L, 365L, 387L, 409L, 431L, 453L, 475L,
497L, 519L, 540L, 561L, 582L, 602L, 621L, 9L, 30L, 51L, 72L,
93L, 114L, 135L, 156L, 177L, 198L, 219L, 240L, 261L, 282L, 303L,
324L, 345L, 367L, 389L, 411L, 433L, 455L, 477L, 499L, 520L, 541L,
562L, 583L, 603L, 622L, 640L, 10L, 31L, 52L, 73L, 94L, 115L,
136L, 157L, 178L, 199L, 220L, 241L, 262L, 283L, 304L, 325L, 346L,
368L, 390L, 412L, 434L, 456L, 478L, 500L, 521L, 542L, 563L, 584L,
604L, 623L, 11L, 32L, 53L, 74L, 95L, 116L, 137L, 158L, 179L,
200L, 221L, 242L, 263L, 284L, 305L, 326L, 348L, 370L, 392L, 414L,
436L, 458L, 480L, 502L, 523L, 544L, 565L, 586L, 606L, 625L, 642L,
13L, 34L, 55L, 76L, 97L, 118L, 139L, 160L, 181L, 202L, 223L,
244L, 265L, 286L, 307L, 328L, 350L, 372L, 394L, 416L, 438L, 460L,
482L, 504L, 525L, 546L, 567L, 588L, 608L, 627L, 644L, 15L, 36L,
57L, 78L, 99L, 120L, 141L, 162L, 183L, 204L, 225L, 246L, 267L,
288L, 309L, 330L, 352L, 374L, 396L, 418L, 440L, 462L, 484L, 506L,
527L, 548L, 569L, 590L, 610L, 629L, 17L, 38L, 59L, 80L, 101L,
122L, 143L, 164L, 185L, 206L, 227L, 248L, 269L, 290L, 311L, 332L,
354L, 376L, 398L, 420L, 442L, 464L, 486L, 508L, 529L, 550L, 571L,
592L, 612L, 631L, 646L, 19L, 40L, 61L, 82L, 103L, 124L, 145L,
166L, 187L, 208L, 229L, 250L, 271L, 292L, 313L, 334L, 356L, 378L,
400L, 422L, 444L, 466L, 488L, 510L, 531L, 552L, 573L, 594L, 614L,
633L, 21L, 42L, 63L, 84L, 105L, 126L, 147L, 168L, 189L, 210L,
231L, 252L, 273L, 294L, 315L, 336L, 358L, 380L, 402L, 424L, 446L,
468L, 490L, 512L, 533L, 554L, 575L, 596L, 616L, 635L, 648L, 2L,
23L, 44L, 65L, 86L, 107L, 128L, 149L, 170L, 191L, 212L, 233L,
254L, 275L, 296L, 317L, 338L, 360L, 382L, 404L, 426L, 448L, 470L,
492L, 514L, 535L, 556L, 577L, 598L, 618L, 637L, 4L, 25L, 46L,
67L, 88L, 109L, 130L, 151L, 172L, 193L, 214L, 235L, 256L, 277L,
298L, 319L, 340L, 362L, 384L, 406L, 428L, 450L, 472L, 494L, 516L,
537L, 558L, 579L, 599L, 6L, 27L, 48L, 69L, 90L, 111L, 132L, 153L,
174L, 195L, 216L, 237L, 258L, 279L, 300L, 321L, 342L, 364L, 386L,
408L, 430L, 452L, 474L, 496L, 518L, 539L, 560L, 581L, 601L, 620L,
639L, 8L, 29L, 50L, 71L, 92L, 113L, 134L, 155L, 176L, 197L, 218L,
239L, 260L, 281L, 302L, 323L, 344L, 366L, 388L, 410L, 432L, 454L,
476L, 498L), .Label = c("01/01/2015", "01/01/2016", "01/02/2015",
"01/02/2016", "01/03/2015", "01/03/2016", "01/04/2015", "01/04/2016",
"01/05/2015", "01/06/2015", "01/07/2015", "01/08/2014", "01/08/2015",
"01/09/2014", "01/09/2015", "01/10/2014", "01/10/2015", "01/11/2014",
"01/11/2015", "01/12/2014", "01/12/2015", "02/01/2015", "02/01/2016",
"02/02/2015", "02/02/2016", "02/03/2015", "02/03/2016", "02/04/2015",
"02/04/2016", "02/05/2015", "02/06/2015", "02/07/2015", "02/08/2014",
"02/08/2015", "02/09/2014", "02/09/2015", "02/10/2014", "02/10/2015",
"02/11/2014", "02/11/2015", "02/12/2014", "02/12/2015", "03/01/2015",
"03/01/2016", "03/02/2015", "03/02/2016", "03/03/2015", "03/03/2016",
"03/04/2015", "03/04/2016", "03/05/2015", "03/06/2015", "03/07/2015",
"03/08/2014", "03/08/2015", "03/09/2014", "03/09/2015", "03/10/2014",
"03/10/2015", "03/11/2014", "03/11/2015", "03/12/2014", "03/12/2015",
"04/01/2015", "04/01/2016", "04/02/2015", "04/02/2016", "04/03/2015",
"04/03/2016", "04/04/2015", "04/04/2016", "04/05/2015", "04/06/2015",
"04/07/2015", "04/08/2014", "04/08/2015", "04/09/2014", "04/09/2015",
"04/10/2014", "04/10/2015", "04/11/2014", "04/11/2015", "04/12/2014",
"04/12/2015", "05/01/2015", "05/01/2016", "05/02/2015", "05/02/2016",
"05/03/2015", "05/03/2016", "05/04/2015", "05/04/2016", "05/05/2015",
"05/06/2015", "05/07/2015", "05/08/2014", "05/08/2015", "05/09/2014",
"05/09/2015", "05/10/2014", "05/10/2015", "05/11/2014", "05/11/2015",
"05/12/2014", "05/12/2015", "06/01/2015", "06/01/2016", "06/02/2015",
"06/02/2016", "06/03/2015", "06/03/2016", "06/04/2015", "06/04/2016",
"06/05/2015", "06/06/2015", "06/07/2015", "06/08/2014", "06/08/2015",
"06/09/2014", "06/09/2015", "06/10/2014", "06/10/2015", "06/11/2014",
"06/11/2015", "06/12/2014", "06/12/2015", "07/01/2015", "07/01/2016",
"07/02/2015", "07/02/2016", "07/03/2015", "07/03/2016", "07/04/2015",
"07/04/2016", "07/05/2015", "07/06/2015", "07/07/2015", "07/08/2014",
"07/08/2015", "07/09/2014", "07/09/2015", "07/10/2014", "07/10/2015",
"07/11/2014", "07/11/2015", "07/12/2014", "07/12/2015", "08/01/2015",
"08/01/2016", "08/02/2015", "08/02/2016", "08/03/2015", "08/03/2016",
"08/04/2015", "08/04/2016", "08/05/2015", "08/06/2015", "08/07/2015",
"08/08/2014", "08/08/2015", "08/09/2014", "08/09/2015", "08/10/2014",
"08/10/2015", "08/11/2014", "08/11/2015", "08/12/2014", "08/12/2015",
"09/01/2015", "09/01/2016", "09/02/2015", "09/02/2016", "09/03/2015",
"09/03/2016", "09/04/2015", "09/04/2016", "09/05/2015", "09/06/2015",
"09/07/2015", "09/08/2014", "09/08/2015", "09/09/2014", "09/09/2015",
"09/10/2014", "09/10/2015", "09/11/2014", "09/11/2015", "09/12/2014",
"09/12/2015", "10/01/2015", "10/01/2016", "10/02/2015", "10/02/2016",
"10/03/2015", "10/03/2016", "10/04/2015", "10/04/2016", "10/05/2015",
"10/06/2015", "10/07/2015", "10/08/2014", "10/08/2015", "10/09/2014",
"10/09/2015", "10/10/2014", "10/10/2015", "10/11/2014", "10/11/2015",
"10/12/2014", "10/12/2015", "11/01/2015", "11/01/2016", "11/02/2015",
"11/02/2016", "11/03/2015", "11/03/2016", "11/04/2015", "11/04/2016",
"11/05/2015", "11/06/2015", "11/07/2015", "11/08/2014", "11/08/2015",
"11/09/2014", "11/09/2015", "11/10/2014", "11/10/2015", "11/11/2014",
"11/11/2015", "11/12/2014", "11/12/2015", "12/01/2015", "12/01/2016",
"12/02/2015", "12/02/2016", "12/03/2015", "12/03/2016", "12/04/2015",
"12/04/2016", "12/05/2015", "12/06/2015", "12/07/2015", "12/08/2014",
"12/08/2015", "12/09/2014", "12/09/2015", "12/10/2014", "12/10/2015",
"12/11/2014", "12/11/2015", "12/12/2014", "12/12/2015", "13/01/2015",
"13/01/2016", "13/02/2015", "13/02/2016", "13/03/2015", "13/03/2016",
"13/04/2015", "13/04/2016", "13/05/2015", "13/06/2015", "13/07/2015",
"13/08/2014", "13/08/2015", "13/09/2014", "13/09/2015", "13/10/2014",
"13/10/2015", "13/11/2014", "13/11/2015", "13/12/2014", "13/12/2015",
"14/01/2015", "14/01/2016", "14/02/2015", "14/02/2016", "14/03/2015",
"14/03/2016", "14/04/2015", "14/04/2016", "14/05/2015", "14/06/2015",
"14/07/2015", "14/08/2014", "14/08/2015", "14/09/2014", "14/09/2015",
"14/10/2014", "14/10/2015", "14/11/2014", "14/11/2015", "14/12/2014",
"14/12/2015", "15/01/2015", "15/01/2016", "15/02/2015", "15/02/2016",
"15/03/2015", "15/03/2016", "15/04/2015", "15/04/2016", "15/05/2015",
"15/06/2015", "15/07/2015", "15/08/2014", "15/08/2015", "15/09/2014",
"15/09/2015", "15/10/2014", "15/10/2015", "15/11/2014", "15/11/2015",
"15/12/2014", "15/12/2015", "16/01/2015", "16/01/2016", "16/02/2015",
"16/02/2016", "16/03/2015", "16/03/2016", "16/04/2015", "16/04/2016",
"16/05/2015", "16/06/2015", "16/07/2015", "16/08/2014", "16/08/2015",
"16/09/2014", "16/09/2015", "16/10/2014", "16/10/2015", "16/11/2014",
"16/11/2015", "16/12/2014", "16/12/2015", "17/01/2015", "17/01/2016",
"17/02/2015", "17/02/2016", "17/03/2015", "17/03/2016", "17/04/2015",
"17/04/2016", "17/05/2015", "17/06/2015", "17/07/2014", "17/07/2015",
"17/08/2014", "17/08/2015", "17/09/2014", "17/09/2015", "17/10/2014",
"17/10/2015", "17/11/2014", "17/11/2015", "17/12/2014", "17/12/2015",
"18/01/2015", "18/01/2016", "18/02/2015", "18/02/2016", "18/03/2015",
"18/03/2016", "18/04/2015", "18/04/2016", "18/05/2015", "18/06/2015",
"18/07/2014", "18/07/2015", "18/08/2014", "18/08/2015", "18/09/2014",
"18/09/2015", "18/10/2014", "18/10/2015", "18/11/2014", "18/11/2015",
"18/12/2014", "18/12/2015", "19/01/2015", "19/01/2016", "19/02/2015",
"19/02/2016", "19/03/2015", "19/03/2016", "19/04/2015", "19/04/2016",
"19/05/2015", "19/06/2015", "19/07/2014", "19/07/2015", "19/08/2014",
"19/08/2015", "19/09/2014", "19/09/2015", "19/10/2014", "19/10/2015",
"19/11/2014", "19/11/2015", "19/12/2014", "19/12/2015", "20/01/2015",
"20/01/2016", "20/02/2015", "20/02/2016", "20/03/2015", "20/03/2016",
"20/04/2015", "20/04/2016", "20/05/2015", "20/06/2015", "20/07/2014",
"20/07/2015", "20/08/2014", "20/08/2015", "20/09/2014", "20/09/2015",
"20/10/2014", "20/10/2015", "20/11/2014", "20/11/2015", "20/12/2014",
"20/12/2015", "21/01/2015", "21/01/2016", "21/02/2015", "21/02/2016",
"21/03/2015", "21/03/2016", "21/04/2015", "21/04/2016", "21/05/2015",
"21/06/2015", "21/07/2014", "21/07/2015", "21/08/2014", "21/08/2015",
"21/09/2014", "21/09/2015", "21/10/2014", "21/10/2015", "21/11/2014",
"21/11/2015", "21/12/2014", "21/12/2015", "22/01/2015", "22/01/2016",
"22/02/2015", "22/02/2016", "22/03/2015", "22/03/2016", "22/04/2015",
"22/04/2016", "22/05/2015", "22/06/2015", "22/07/2014", "22/07/2015",
"22/08/2014", "22/08/2015", "22/09/2014", "22/09/2015", "22/10/2014",
"22/10/2015", "22/11/2014", "22/11/2015", "22/12/2014", "22/12/2015",
"23/01/2015", "23/01/2016", "23/02/2015", "23/02/2016", "23/03/2015",
"23/03/2016", "23/04/2015", "23/04/2016", "23/05/2015", "23/06/2015",
"23/07/2014", "23/07/2015", "23/08/2014", "23/08/2015", "23/09/2014",
"23/09/2015", "23/10/2014", "23/10/2015", "23/11/2014", "23/11/2015",
"23/12/2014", "23/12/2015", "24/01/2015", "24/01/2016", "24/02/2015",
"24/02/2016", "24/03/2015", "24/03/2016", "24/04/2015", "24/04/2016",
"24/05/2015", "24/06/2015", "24/07/2014", "24/07/2015", "24/08/2014",
"24/08/2015", "24/09/2014", "24/09/2015", "24/10/2014", "24/10/2015",
"24/11/2014", "24/11/2015", "24/12/2014", "24/12/2015", "25/01/2015",
"25/01/2016", "25/02/2015", "25/02/2016", "25/03/2015", "25/03/2016",
"25/04/2015", "25/05/2015", "25/06/2015", "25/07/2014", "25/07/2015",
"25/08/2014", "25/08/2015", "25/09/2014", "25/09/2015", "25/10/2014",
"25/10/2015", "25/11/2014", "25/11/2015", "25/12/2014", "25/12/2015",
"26/01/2015", "26/01/2016", "26/02/2015", "26/02/2016", "26/03/2015",
"26/03/2016", "26/04/2015", "26/05/2015", "26/06/2015", "26/07/2014",
"26/07/2015", "26/08/2014", "26/08/2015", "26/09/2014", "26/09/2015",
"26/10/2014", "26/10/2015", "26/11/2014", "26/11/2015", "26/12/2014",
"26/12/2015", "27/01/2015", "27/01/2016", "27/02/2015", "27/02/2016",
"27/03/2015", "27/03/2016", "27/04/2015", "27/05/2015", "27/06/2015",
"27/07/2014", "27/07/2015", "27/08/2014", "27/08/2015", "27/09/2014",
"27/09/2015", "27/10/2014", "27/10/2015", "27/11/2014", "27/11/2015",
"27/12/2014", "27/12/2015", "28/01/2015", "28/01/2016", "28/02/2015",
"28/02/2016", "28/03/2015", "28/03/2016", "28/04/2015", "28/05/2015",
"28/06/2015", "28/07/2014", "28/07/2015", "28/08/2014", "28/08/2015",
"28/09/2014", "28/09/2015", "28/10/2014", "28/10/2015", "28/11/2014",
"28/11/2015", "28/12/2014", "28/12/2015", "29/01/2015", "29/01/2016",
"29/02/2016", "29/03/2015", "29/03/2016", "29/04/2015", "29/05/2015",
"29/06/2015", "29/07/2014", "29/07/2015", "29/08/2014", "29/08/2015",
"29/09/2014", "29/09/2015", "29/10/2014", "29/10/2015", "29/11/2014",
"29/11/2015", "29/12/2014", "29/12/2015", "30/01/2015", "30/01/2016",
"30/03/2015", "30/03/2016", "30/04/2015", "30/05/2015", "30/06/2015",
"30/07/2014", "30/07/2015", "30/08/2014", "30/08/2015", "30/09/2014",
"30/09/2015", "30/10/2014", "30/10/2015", "30/11/2014", "30/11/2015",
"30/12/2014", "30/12/2015", "31/01/2015", "31/01/2016", "31/03/2015",
"31/03/2016", "31/05/2015", "31/07/2014", "31/07/2015", "31/08/2014",
"31/08/2015", "31/10/2014", "31/10/2015", "31/12/2014", "31/12/2015"
), class = "factor"), index = c(11.54043, 14.27814, 11.5583,
12.37828, 12.54057, 12.10189, 12.12189, 12.28188, 11.96189, 12.35303,
13.023, 12.55187, 11.04192, 8.722033, 6.952167, 6.732189, 9.022016,
8.432052, 5.882287, 5.276563, 4.731485, 4.403024, 4.651509, 6.319038,
7.818936, 7.948929, 6.809, 6.199048, 6.749004, 6.499023, 5.899076,
4.529247, 4.02078, 3.760833, 3.617566, 3.36093, 3.950794, 4.230742,
4.320727, 4.720667, 4.570688, 4.080769, 4.360721, 4.580687, 4.730665,
4.630679, 4.960635, 4.180751, 4.270736, 4.210746, 4.440708, 3.670853,
3.570877, 3.650858, 3.740838, 3.880808, 3.840816, 3.240964, 3.160988,
3.250961, 3.580874, 3.560879, 5.380586, 4.510697, 4.390716, 4.260737,
3.890806, 3.36093, 3.721801, 3.591829, 3.560497, 4.120431, 4.55039,
4.4404, 4.470397, 4.670381, 3.660484, 3.730475, 3.160559, 3.320533,
3.380523, 3.600492, 3.030583, 3.260542, 2.970594, 3.040581, 2.99059,
3.40052, 3.730475, 3.430516, 3.530501, 2.970594, 3.820464, 3.830463,
3.870458, 3.700479, 3.710477, 3.680481, 3.490507, 3.740474, 3.260542,
3.318999, 3.298999, 3.328999, 3.368284, 3.41828, 3.238295, 3.008317,
2.878331, 2.788342, 2.598366, 2.488382, 2.468385, 2.448388, 2.548373,
2.308412, 2.448388, 2.658358, 2.048463, 2.568371, 2.838336, 2.868332,
2.998318, 3.358285, 3.118306, 2.618364, 2.478384, 3.1783, 3.018316,
3.07831, 2.898329, 2.938325, 2.88833, 2.848335, 2.948324, 2.908328,
2.958322, 2.968321, 2.736638, 2.927969, 2.95236, 2.92152, 4.159778,
3.274662, 3.716456, 4.321648, 4.33252, 4.942867, 4.324445, 3.925162,
3.485163, 3.945088, 3.467801, 3.84071, 3.542677, 3.207959, 3.097636,
3.229113, 3.049058, 3.487368, 2.946642, 3.194158, 3.033129, 2.741163,
2.646968, 2.514944, 2.612467, 2.806449, 2.708465, 2.567833, 2.783192,
2.99844, 2.858031, 2.860846, 2.422666, 2.08108, 2.192705, 2.407469,
2.951197, 2.425093, 2.561358, 2.162087, 2.164641, 2.295119, 1.817072,
1.385466, 2.399334, 2.859039, 2.098575, 2.406024, 2.369869, 2.744476,
3.224035, 2.8761, 2.99883, 3.079353, 2.99788, 2.957237, 2.329897,
2.556688, 2.261765, 2.211449, 2.077952, 2.172062, 2.501332, 2.271251,
2.567649, 1.985015, 2.011745, 2.378133, 1.937532, 2.295658, 1.967439,
1.922405, 1.77076, 1.877509, 1.903558, 1.843825, 2.033853, 2.107302,
2.038126, 2.054973, 1.993873, 2.042604, 1.981318, 2.286632, 1.902597,
2.202905, 2.262768, 2.493253, 2.105771, 2.113826, 2.7515, 2.085522,
2.613089, 2.118656, 2.310738, 2.626212, 2.629956, 2.752603, 2.746964,
2.766788, 2.696453, 2.159032, 2.134599, 1.714365, 1.55678, 1.626582,
1.607851, 1.532417, 1.571745, 1.500041, 1.543227, 1.480322, 1.762261,
1.515217, 1.304601, 1.447073, 1.475861, 1.498862, 1.573622, 1.515242,
1.606151, 1.581706, 1.443625, 1.442918, 1.450428, 1.56483, 1.502704,
1.555937, 1.593459, 1.459013, 1.365548, 1.530271, 1.522306, 1.164105,
1.449812, 1.34549, 1.277848, 1.140585, 1.035555, 1.161103, 1.085743,
1.174396, 1.188879, 1.245301, 0.985737, 1.169837, 1.21196, 1.132433,
1.199008, 1.16729, 1.176818, 1.202165, 1.191286, 1.199928, 1.16782,
1.163427, 1.147315, 1.152607, 1.229492, 1.464407, 1.35002, 1.326579,
1.254948, 1.333277, 0.965398, 1.246482, 1.068102, 1.05843, 1.15212,
1.182821, 1.328945, 1.261149, 1.319696, 0.815034, 1.242683, 1.222728,
1.351629, 1.311053, 1.299895, 1.161236, 0.913985, 1.021523, 0.974081,
1.312736, 0.84724, 0.784337, 0.910343, 0.911839, 0.988695, 1.204447,
1.188309, 1.209292, 1.269653, 1.131285, 1.196762, 1.122018, 1.278813,
1.306997, 1.507417, 1.808925, 1.422698, 1.362512, 1.456492, 1.339841,
1.408134, 1.464803, 1.472624, 1.507043, 1.55663, 1.48721, 1.481805,
1.350952, 1.394053, 1.505662, 1.552468, 1.835227, 1.529406, 1.542733,
2.472506, 2.051214, 2.04605, 2.332706, 2.51142, 2.856563, 2.625034,
2.642861, 2.351145, 2.318266, 2.551799, 2.332817, 2.073351, 1.730547,
2.268209, 2.08866, 1.918522, 2.225836, 2.343466, 2.1983, 2.214688,
2.249369, 2.320987, 2.158788, 2.250545, 1.86419, 1.960187, 2.145659,
1.785818, 1.812893, 1.670426, 1.759863, 1.930967, 1.911622, 1.682475,
1.77137, 1.566444, 1.802325, 1.586361, 1.294167, 1.483635, 1.699373,
1.980278, 1.628827, 2.130249, 1.65064, 1.830685, 2.334663, 2.239406,
2.374907, 2.174426, 2.11795, 1.962688, 1.970793, 2.334288, 1.97112,
2.109338, 2.380336, 1.974693, 2.231339, 1.150346, 1.248199, 1.104014,
1.145332, 1.376, 1.365866, 1.431675, 1.411714, 1.470395, 1.463537,
1.479107, 1.571953, 1.582307, 1.425284, 1.357404, 1.459058, 1.29251,
2.079904, 2.043994, 2.02053, 1.854421, 2.024019, 2.027243, 2.024739,
2.020098, 2.072994, 1.89817, 1.970579, 1.925721, 1.940698, 1.958429,
1.97927, 1.990377, 2.545347, 2.343933, 2.110605, 2.372304, 2.614607,
2.65837, 1.253188, 2.371879, 2.48065, 2.581769, 2.201459, 1.705221,
2.662408, 1.769794, 2.160805, 1.933198, 2.318748, 2.279574, 2.206514,
1.86008, 2.221785, 2.732116, 2.876525, 2.45854, 2.093711, 1.990731,
2.119744, 1.88928, 1.906683, 1.711405, 1.290373, 1.965132, 1.639966,
1.579937, 1.896039, 1.955329, 1.970785, 1.41028, 1.963055, 1.935048,
1.958985, 1.912964, 1.915689, 1.844459, 2.267502, 2.263569, 2.260751,
1.863576, 1.810112, 1.739387, 1.646463, 1.552307, 1.871372, 1.735762,
1.694135, 1.627406, 1.789137, 1.636116, 1.65404, 1.655442, 1.466584,
1.630533, 1.474457, 1.505985, 1.435338, 1.537106, 1.521365, 1.464372,
1.450722, 1.387195, 1.432416, 1.409623, 1.943541, 1.895353, 1.727831,
1.915016, 2.142965, 1.78175, 1.757019, 4.046341, 2.268203, 1.695811,
1.714067, 1.689575, 1.810448, 1.587102, 1.83034, 1.513751, 1.535203,
1.531233, 1.43809, 1.390571, 1.292746, 1.3538, 1.201273, 1.481288,
1.600983, 1.438571, 1.583992, 1.766542, 1.717157, 1.773975, 1.95323,
2.0458, 1.965663, 1.868745, 1.862877, 1.717166, 1.85268, 1.865566,
2.831913, 1.858382, 1.926938, 1.911859, 2.364972, 2.271169, 2.147911,
2.273932, 2.173164, 2.235003, 2.160419, 2.58684, 2.440009, 2.334429,
2.374356, 2.637341, 2.751997, 2.662583, 2.570964, 2.643219, 2.196613,
2.226018, 2.142688, 2.403963, 2.384954, 2.661776, 2.711935, 2.714279,
2.329776, 2.370735, 2.100872, 1.943771, 1.575529, 1.544865, 1.51201,
1.443336, 1.655716, 1.664355, 1.717507, 1.717282, 1.806321, 1.788896,
1.803193, 1.401859, 1.762782, 1.537422, 2.145965, 2.305251, 2.110511,
1.934735, 1.946052, 2.138253, 2.025721, 1.993805, 2.072526, 1.888899,
1.803845, 1.830216, 1.821895, 1.843385, 1.999159, 1.951067, 1.889941,
2.360204, 2.645206, 2.347469, 2.241971, 2.043113, 1.962672, 1.903516,
1.609725, 1.71036, 1.801525, 1.748996, 1.566542, 1.588622, 1.507817,
1.629962, 1.669554, 1.624924, 1.555608, 1.474775, 1.438227, 1.664659,
1.499378)), .Names = c("time", "index"), class = "data.frame", row.names = c(NA,
-648L))
So, what I generally do is to write this code:
library(fBasics)
pw_index <- read.csv("~/data/index.csv",
header=T)
# Set time in date format
index$time <- as.Date(index$time, format="%d/%m/%y")
index <- index[order(index$time), ]
# Save the date in a separate identifier as character
dates = as.character(index$time)
index <- index[order(dates), ]
# Convert the data frame to an .xts object:
index_xts <- as.xts(index$index, order.by=index$time)
head(index_xts)
If I initially inspect the dataset vie head() I obtain this:
time index
<fctr> <dbl>
1 17/07/2014 11.54043
2 18/07/2014 14.27814
3 19/07/2014 11.55830
4 20/07/2014 12.37828
5 21/07/2014 12.54057
6 22/07/2014 12.10189
However, what I do obtain after the code is a completely messed out dataset (last observation should be of 2016...):
[,1]
2020-01-01 2.708465
2020-01-01 2.268203
2020-01-02 2.567833
2020-01-02 1.695811
2020-01-03 2.783192
2020-01-03 1.714067
Who knows what's going on?

Your code is somewhat convoluted, and I'm not entirely sure what you're trying to do. For converting the data in your data.frame into an xts object you can do the following:
library(xts);
xts <- xts(x = df$index, order.by = as.POSIXct(df$time, format = "%d/%m/%Y"));
tail(xts);
# [,1]
#2016-04-19 1.624924
#2016-04-20 1.555608
#2016-04-21 1.474775
#2016-04-22 1.438227
#2016-04-23 1.664659
#2016-04-24 1.499378
I assume that df is your data.frame the content of which you provided with dput.

Related

Error in Eval Try to define a model für lm

I try do define the model for my test and training dataset. But I get the following Error:
Error in eval(predvars, data, env) : object 'avg_rating' not found
But all of my datasets have the "avg_rating"
This is my code
lm_model <- train(avg_rating ~., data = trainingindex,method = "lm",na.action = na.omit, preProcess = c("scale", "center"),trControl = trainControl(method = "none"))
structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 21L, 23L, 24L, 25L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 52L, 53L, 55L, 58L, 61L, 62L, 63L, 65L,
66L, 67L, 68L, 69L, 70L, 71L, 74L, 77L, 78L, 80L, 81L, 83L, 84L,
85L, 86L, 87L, 88L, 90L, 91L, 92L, 93L, 94L, 96L, 97L, 99L, 102L,
103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 113L, 115L,
116L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 150L,
152L, 154L, 155L, 157L, 158L, 160L, 161L, 162L, 165L, 166L, 167L,
168L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L,
180L, 181L, 182L, 185L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 199L, 200L, 201L, 202L, 203L, 204L, 205L,
207L, 208L, 209L, 210L, 213L, 214L, 216L, 217L, 219L, 220L, 221L,
223L, 224L, 225L, 226L, 227L, 228L, 230L, 231L, 232L, 233L, 234L,
235L, 236L, 237L, 238L, 239L, 240L, 242L, 243L, 244L, 245L, 246L,
247L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 257L, 259L,
260L, 261L, 262L, 263L, 264L, 266L, 267L, 268L, 271L, 272L, 273L,
274L, 275L, 276L, 277L, 278L, 280L, 281L, 282L, 284L, 285L, 286L,
287L, 288L, 290L, 291L, 294L, 295L, 296L, 297L, 298L, 299L, 300L,
301L, 302L, 303L, 304L, 305L, 308L, 309L, 310L, 311L, 312L, 313L,
314L, 315L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 326L,
327L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L,
340L, 341L, 343L, 344L, 345L, 346L, 348L, 349L, 350L, 351L, 353L,
354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 363L, 364L, 365L,
366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L,
377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L,... 3687L), .Dim = c(2952L, 1
), .Dimnames = list(NULL, "Resample1"))
15L, 16L, 17L, 18L, 19L, 21L, 23L, 24L, 25L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 35L, 36L), .Dim = c(30L, 1L), .Dimnames = list(
NULL, "Resample1"))

Reduce large data-frame of samples to ensure maximum variability between samples

I have a list of vectors that each entry in the list is a vector of indices, for example:
list(c(563L, 688L, 630L, 160L, 568L, 908L, 457L, 798L, 3L, 558L,
56L, 389L, 506L, 106L, 807L, 556L, 809L, 63L, 343L, 242L, 470L,
894L, 804L, 970L, 406L, 881L, 893L, 952L, 126L, 827L, 282L, 910L,
61L, 66L, 763L, 787L, 337L, 41L, 712L, 144L, 450L, 12L, 200L,
574L, 945L, 236L, 336L, 684L, 280L, 721L, 233L, 686L, 64L, 504L,
174L, 934L, 40L, 850L, 26L, 799L, 853L, 978L), c(85L, 564L, 591L,
662L, 377L, 536L, 325L, 402L, 72L, 410L, 687L, 216L, 603L, 67L,
794L, 388L, 627L, 376L, 863L, 491L, 598L, 861L, 991L, 651L, 670L,
401L, 459L, 39L, 997L, 806L, 623L, 954L), c(427L, 791L, 212L,
779L, 657L, 740L, 800L, 838L, 104L, 985L, 167L, 486L, 685L, 739L,
60L, 862L, 130L, 134L, 175L, 375L, 683L, 885L, 575L, 859L, 341L,
726L, 472L, 802L, 76L, 424L, 177L, 624L, 189L, 334L, 378L, 329L,
581L, 224L, 851L, 218L, 993L, 678L, 248L, 365L, 188L, 774L, 58L,
813L, 514L, 59L, 777L, 485L, 606L, 480L, 826L, 350L, 608L, 27L,
661L, 775L, 340L, 10L, 207L, 260L, 483L, 150L, 205L), c(138L,
587L, 165L, 1L, 722L, 300L, 500L, 535L, 832L, 392L, 432L, 139L,
744L, 676L, 839L, 107L, 769L, 589L, 647L, 548L, 704L, 197L, 689L,
111L, 342L, 319L, 567L, 17L, 925L, 5L, 116L, 493L, 241L, 965L
), c(89L, 440L, 228L, 884L, 88L, 147L, 413L, 821L, 70L, 95L,
71L, 917L, 463L, 990L, 672L, 981L, 765L, 937L, 75L, 766L, 374L,
636L, 449L, 816L, 1000L, 356L, 629L), c(421L, 650L, 453L, 666L,
584L, 717L, 220L, 605L, 182L, 811L, 157L, 523L, 28L, 527L, 737L,
812L, 263L, 675L, 132L, 879L, 438L, 451L, 883L, 950L, 114L, 466L,
348L, 711L, 209L, 887L, 593L, 949L, 349L, 764L, 595L, 736L, 660L,
801L, 118L, 877L), c(23L, 231L, 78L, 988L, 55L, 57L, 753L, 994L,
437L, 202L, 842L, 190L, 822L, 968L, 331L, 733L, 782L, 886L, 105L,
943L, 743L, 815L, 311L, 498L, 792L, 795L, 184L, 728L, 573L, 771L,
117L, 251L, 192L, 735L, 15L, 776L, 295L, 677L, 631L, 235L, 237L,
705L, 856L, 97L, 725L), c(229L, 671L, 129L, 405L, 115L, 644L,
98L, 492L, 871L, 935L, 435L, 707L, 773L, 754L, 803L, 120L, 656L,
345L, 875L, 330L, 533L, 366L, 240L, 408L, 332L, 577L, 550L, 452L,
963L, 8L, 187L, 226L, 901L, 371L, 426L, 339L, 519L, 86L, 501L,
274L, 831L), c(16L, 79L, 68L, 477L, 133L, 659L, 2L, 973L, 264L,
953L, 90L, 234L, 420L, 588L, 21L, 788L, 363L, 539L, 227L, 565L,
30L, 642L, 786L, 982L, 347L, 680L, 52L, 96L, 592L, 409L, 643L,
81L, 419L, 245L, 658L, 416L, 590L, 448L, 819L, 277L, 357L, 442L,
789L, 516L, 980L, 93L, 998L, 149L, 166L, 299L, 454L, 529L, 986L,
127L, 541L, 45L, 829L, 289L, 418L, 179L, 310L, 113L, 729L), c(429L,
781L, 303L, 434L, 83L, 259L, 387L, 583L, 393L, 770L, 246L, 428L,
947L, 976L, 31L, 382L, 710L, 944L, 164L, 868L, 373L, 899L, 74L,
468L, 614L, 701L, 221L, 645L, 268L, 785L, 293L, 632L, 24L, 749L,
283L, 741L, 796L, 915L), c(258L, 844L, 649L, 752L, 474L, 613L,
351L, 551L, 309L, 380L, 497L, 724L, 327L, 992L, 845L, 607L, 818L,
693L, 914L, 291L, 720L, 633L, 974L, 367L, 639L, 94L, 467L, 92L,
522L, 141L, 496L, 276L, 542L, 665L, 695L, 634L, 602L, 913L, 396L,
597L, 443L, 892L, 65L, 394L, 222L, 778L, 169L, 960L, 35L, 655L,
422L, 927L, 154L, 215L, 262L, 203L, 880L, 217L, 423L, 755L, 904L,
180L, 620L), c(507L, 628L, 29L, 902L, 738L, 897L, 664L, 967L,
294L, 682L, 254L, 302L, 128L, 559L, 511L, 526L, 7L, 742L, 464L,
621L, 265L, 599L, 102L, 546L, 458L, 969L, 751L, 860L, 326L, 873L,
335L, 580L, 499L, 962L, 290L, 557L, 213L, 716L, 53L, 835L, 600L,
610L, 321L, 673L, 713L, 876L, 244L, 462L, 136L, 272L, 195L, 447L,
230L, 679L, 465L, 611L, 297L, 731L, 44L, 824L, 162L, 837L), c(446L,
561L, 391L, 652L, 857L, 946L, 560L, 784L, 854L, 204L, 512L, 82L,
455L, 372L, 407L, 328L, 808L, 152L, 178L, 185L, 543L, 108L, 473L,
490L, 955L, 719L, 757L, 198L, 338L, 223L, 919L, 531L, 653L, 734L,
923L, 487L, 637L, 398L, 431L, 46L, 848L, 324L, 948L, 43L, 183L,
288L, 697L, 87L, 307L, 42L, 571L, 360L, 433L, 390L, 569L, 956L,
534L, 6L, 381L, 549L, 301L, 920L, 69L, 322L, 267L, 503L, 285L,
961L, 370L, 425L), c(344L, 959L, 364L, 552L, 11L, 481L, 287L,
891L, 692L, 762L, 47L, 292L, 358L, 810L, 942L, 730L, 746L, 638L,
750L, 759L, 761L, 140L, 444L, 191L, 805L, 306L, 691L, 170L, 715L,
508L, 984L, 461L, 911L, 103L, 938L, 718L, 928L), c(124L, 284L,
123L, 513L, 417L, 933L, 121L, 168L, 208L, 385L, 32L, 273L, 869L,
932L, 397L, 509L, 239L, 797L, 379L, 723L, 898L, 163L, 320L, 833L,
151L, 906L, 648L, 732L, 279L, 834L, 489L, 840L, 783L, 971L, 49L,
145L, 253L, 352L, 137L, 261L, 247L, 143L, 544L, 109L, 921L, 830L,
972L, 585L, 690L, 609L, 703L, 250L, 708L, 225L, 889L, 181L, 987L,
54L, 502L, 148L, 355L, 888L, 579L, 983L, 825L, 855L, 62L, 918L,
979L, 586L, 681L, 384L, 709L, 333L, 758L, 194L, 368L), c(646L,
930L, 361L, 399L, 13L, 298L, 395L, 975L, 482L, 940L, 596L, 772L,
700L, 843L, 171L, 537L, 173L, 836L, 767L, 989L, 532L, 890L, 99L,
865L, 142L, 135L, 271L, 346L, 441L, 48L, 941L, 866L, 201L, 872L,
36L, 520L, 530L, 77L, 270L), c(238L, 699L, 22L, 50L, 615L, 702L,
4L, 469L, 101L, 314L, 616L, 995L, 996L, 414L, 566L, 249L, 572L,
369L, 553L, 158L, 159L, 199L, 317L, 515L, 517L, 524L, 562L, 19L,
476L, 20L, 146L, 618L, 895L, 312L, 912L), c(768L, 939L, 578L,
849L, 196L, 640L, 323L, 635L, 304L, 318L, 874L, 977L, 488L, 619L,
155L, 905L, 9L, 112L, 484L, 847L, 313L, 900L, 494L, 727L, 625L,
931L, 119L, 846L, 186L, 219L, 471L, 696L, 404L, 460L, 668L, 896L,
439L, 964L, 275L, 756L, 411L, 878L, 538L, 669L, 478L, 570L, 255L,
547L, 257L, 841L, 37L, 576L, 456L, 663L, 525L, 817L, 612L, 820L
), c(243L, 594L, 33L, 176L, 415L, 667L, 748L, 852L, 232L, 922L,
308L, 436L, 153L, 505L, 14L, 281L, 316L, 495L, 540L, 622L, 156L,
926L, 521L, 698L, 545L, 760L, 84L, 210L, 359L, 131L, 745L, 34L,
91L, 555L, 858L, 445L, 867L, 125L, 814L, 604L, 706L, 315L, 654L,
747L, 936L, 269L, 957L), c(80L, 924L, 110L, 193L, 958L, 296L,
475L, 18L, 907L, 626L, 999L, 278L, 362L, 51L, 641L, 211L, 929L,
122L, 694L, 73L, 353L, 25L, 100L, 305L, 864L, 214L, 790L, 286L,
518L, 674L, 206L, 400L, 554L, 903L, 780L, 916L, 38L, 430L, 617L,
823L, 172L, 966L, 412L, 951L, 510L, 828L, 479L, 909L, 266L, 582L,
870L, 882L, 161L, 252L, 256L, 383L, 403L, 601L, 386L, 793L, 528L,
354L, 714L))
Where each entry (or each nested list) represents a group obtained using a clustering method.
Now I have the following piece of code that takes this list of nested lists and the amount of samples required and returns a data-frame where each row represents a single sample and each column is a single sample from a group from one of the nested list.
groups_samples <- function(groups, repetition) {
return(as.data.frame(sapply(groups, sample, repetition, TRUE)))
}
Let's take the following as an example:
df <- groups_samples(ll, 100)
structure(list(V1 = c(106L, 686L, 721L, 200L, 970L, 910L, 556L,
807L, 908L, 568L, 688L, 389L, 56L, 470L, 630L, 893L, 574L, 236L,
804L, 798L, 721L, 934L, 763L, 807L, 457L, 568L, 684L, 934L, 787L,
450L, 688L, 64L, 568L, 934L, 894L, 558L, 568L, 343L, 450L, 853L,
336L, 64L, 712L, 144L, 934L, 144L, 809L, 763L, 457L, 763L, 558L,
457L, 688L, 763L, 504L, 66L, 406L, 881L, 3L, 343L, 556L, 799L,
712L, 568L, 61L, 799L, 908L, 688L, 64L, 881L, 236L, 787L, 66L,
160L, 853L, 343L, 809L, 200L, 827L, 893L, 894L, 799L, 470L, 406L,
337L, 389L, 63L, 952L, 236L, 337L, 763L, 41L, 945L, 144L, 56L,
978L, 233L, 978L, 881L, 910L), V2 = c(72L, 651L, 861L, 651L,
591L, 72L, 564L, 662L, 402L, 623L, 603L, 377L, 401L, 603L, 598L,
67L, 991L, 376L, 67L, 325L, 325L, 377L, 536L, 861L, 564L, 670L,
806L, 377L, 687L, 603L, 954L, 627L, 67L, 388L, 954L, 564L, 991L,
564L, 591L, 863L, 376L, 991L, 85L, 85L, 564L, 598L, 591L, 687L,
806L, 564L, 401L, 72L, 603L, 536L, 459L, 603L, 954L, 67L, 216L,
410L, 687L, 806L, 623L, 388L, 67L, 401L, 491L, 662L, 85L, 627L,
598L, 954L, 459L, 591L, 997L, 687L, 687L, 536L, 863L, 459L, 670L,
459L, 603L, 401L, 39L, 687L, 39L, 651L, 991L, 376L, 388L, 954L,
997L, 85L, 39L, 627L, 861L, 670L, 39L, 459L), V3 = c(424L, 775L,
862L, 791L, 683L, 826L, 60L, 205L, 802L, 740L, 58L, 985L, 683L,
341L, 838L, 212L, 993L, 59L, 851L, 657L, 375L, 885L, 150L, 167L,
218L, 205L, 58L, 260L, 341L, 661L, 791L, 350L, 726L, 378L, 188L,
150L, 60L, 813L, 774L, 104L, 207L, 207L, 485L, 514L, 424L, 514L,
859L, 130L, 350L, 188L, 188L, 740L, 859L, 177L, 212L, 802L, 606L,
104L, 608L, 260L, 329L, 993L, 427L, 427L, 485L, 472L, 859L, 424L,
661L, 514L, 791L, 678L, 993L, 726L, 188L, 340L, 483L, 150L, 340L,
514L, 606L, 248L, 205L, 188L, 581L, 813L, 175L, 657L, 862L, 775L,
212L, 341L, 27L, 885L, 575L, 334L, 350L, 486L, 483L, 340L), V4 = c(138L,
493L, 111L, 241L, 548L, 107L, 548L, 965L, 839L, 1L, 139L, 1L,
165L, 769L, 111L, 965L, 548L, 1L, 676L, 319L, 689L, 769L, 567L,
197L, 139L, 319L, 319L, 832L, 116L, 500L, 392L, 704L, 689L, 500L,
689L, 832L, 165L, 138L, 116L, 676L, 197L, 589L, 832L, 165L, 925L,
165L, 647L, 832L, 116L, 744L, 587L, 925L, 500L, 116L, 107L, 832L,
500L, 319L, 17L, 925L, 116L, 548L, 17L, 107L, 676L, 111L, 832L,
925L, 111L, 107L, 17L, 722L, 139L, 432L, 319L, 548L, 241L, 769L,
319L, 17L, 689L, 342L, 165L, 722L, 676L, 319L, 197L, 241L, 139L,
139L, 111L, 744L, 689L, 722L, 965L, 432L, 647L, 432L, 1L, 111L
), V5 = c(816L, 95L, 884L, 821L, 88L, 374L, 981L, 672L, 70L,
71L, 89L, 95L, 374L, 75L, 917L, 765L, 917L, 449L, 71L, 884L,
766L, 70L, 672L, 89L, 816L, 937L, 937L, 440L, 413L, 1000L, 1000L,
413L, 70L, 356L, 821L, 440L, 990L, 821L, 147L, 356L, 629L, 374L,
766L, 766L, 71L, 937L, 89L, 95L, 917L, 937L, 937L, 449L, 95L,
463L, 1000L, 440L, 821L, 884L, 917L, 816L, 89L, 1000L, 766L,
356L, 765L, 440L, 75L, 463L, 440L, 440L, 765L, 636L, 672L, 629L,
88L, 356L, 374L, 374L, 463L, 95L, 463L, 75L, 71L, 89L, 449L,
88L, 990L, 884L, 765L, 463L, 884L, 672L, 463L, 449L, 629L, 821L,
981L, 75L, 990L, 440L), V6 = c(650L, 675L, 737L, 466L, 883L,
877L, 209L, 887L, 584L, 263L, 605L, 132L, 584L, 950L, 650L, 451L,
737L, 453L, 348L, 675L, 949L, 349L, 209L, 584L, 801L, 593L, 711L,
666L, 466L, 605L, 527L, 666L, 584L, 717L, 114L, 660L, 118L, 466L,
811L, 595L, 438L, 28L, 593L, 811L, 118L, 711L, 605L, 593L, 466L,
650L, 801L, 438L, 348L, 349L, 118L, 584L, 114L, 584L, 801L, 209L,
157L, 466L, 801L, 182L, 812L, 132L, 523L, 666L, 605L, 527L, 950L,
950L, 812L, 421L, 584L, 801L, 132L, 182L, 737L, 887L, 883L, 605L,
737L, 711L, 28L, 675L, 220L, 157L, 118L, 887L, 675L, 132L, 736L,
811L, 887L, 438L, 182L, 717L, 737L, 950L), V7 = c(994L, 202L,
311L, 725L, 437L, 725L, 776L, 295L, 792L, 57L, 57L, 295L, 842L,
15L, 776L, 331L, 822L, 795L, 78L, 988L, 498L, 822L, 988L, 782L,
776L, 728L, 631L, 725L, 735L, 573L, 105L, 295L, 23L, 78L, 202L,
117L, 190L, 705L, 105L, 57L, 792L, 251L, 251L, 968L, 192L, 23L,
231L, 822L, 295L, 231L, 631L, 842L, 57L, 235L, 815L, 331L, 117L,
705L, 331L, 994L, 795L, 237L, 815L, 815L, 23L, 822L, 235L, 631L,
78L, 97L, 57L, 192L, 677L, 184L, 57L, 231L, 231L, 753L, 733L,
237L, 743L, 677L, 631L, 988L, 815L, 311L, 815L, 311L, 771L, 728L,
23L, 988L, 728L, 705L, 97L, 988L, 994L, 57L, 728L, 192L), V8 = c(754L,
875L, 332L, 935L, 86L, 339L, 86L, 644L, 339L, 501L, 803L, 229L,
644L, 426L, 550L, 129L, 330L, 129L, 229L, 86L, 773L, 803L, 129L,
901L, 452L, 8L, 229L, 98L, 129L, 366L, 187L, 8L, 773L, 187L,
229L, 8L, 98L, 935L, 98L, 345L, 754L, 533L, 332L, 550L, 240L,
875L, 773L, 229L, 426L, 754L, 120L, 803L, 129L, 901L, 901L, 644L,
345L, 707L, 707L, 773L, 533L, 120L, 332L, 330L, 803L, 86L, 803L,
8L, 226L, 345L, 871L, 240L, 550L, 963L, 330L, 345L, 226L, 533L,
366L, 452L, 803L, 405L, 803L, 405L, 550L, 577L, 8L, 339L, 901L,
577L, 330L, 229L, 330L, 656L, 452L, 330L, 519L, 226L, 366L, 435L
), V9 = c(643L, 953L, 642L, 21L, 592L, 16L, 127L, 539L, 409L,
516L, 419L, 277L, 986L, 590L, 45L, 980L, 998L, 516L, 541L, 980L,
454L, 81L, 149L, 986L, 227L, 45L, 420L, 363L, 986L, 90L, 409L,
986L, 953L, 45L, 982L, 588L, 68L, 127L, 127L, 16L, 418L, 21L,
953L, 442L, 418L, 419L, 565L, 980L, 659L, 16L, 149L, 448L, 789L,
454L, 516L, 2L, 127L, 79L, 277L, 980L, 234L, 357L, 357L, 642L,
980L, 680L, 729L, 81L, 21L, 454L, 986L, 357L, 980L, 973L, 680L,
592L, 788L, 2L, 264L, 79L, 680L, 729L, 52L, 986L, 539L, 79L,
277L, 416L, 786L, 477L, 113L, 454L, 419L, 442L, 953L, 79L, 245L,
788L, 93L, 234L), V10 = c(31L, 468L, 468L, 387L, 164L, 796L,
701L, 785L, 915L, 614L, 741L, 770L, 770L, 583L, 373L, 373L, 393L,
221L, 303L, 83L, 74L, 785L, 387L, 741L, 741L, 393L, 468L, 701L,
382L, 393L, 387L, 899L, 429L, 947L, 781L, 781L, 645L, 645L, 710L,
915L, 74L, 796L, 259L, 749L, 373L, 393L, 246L, 632L, 785L, 259L,
614L, 785L, 428L, 741L, 632L, 382L, 770L, 710L, 781L, 749L, 868L,
915L, 434L, 221L, 429L, 303L, 393L, 468L, 632L, 976L, 781L, 373L,
947L, 428L, 781L, 781L, 645L, 868L, 645L, 710L, 283L, 31L, 868L,
583L, 915L, 246L, 373L, 373L, 781L, 164L, 428L, 710L, 373L, 303L,
632L, 868L, 614L, 947L, 74L, 382L), V11 = c(351L, 154L, 423L,
496L, 818L, 913L, 665L, 913L, 380L, 720L, 542L, 380L, 634L, 551L,
258L, 818L, 634L, 474L, 222L, 639L, 974L, 755L, 262L, 665L, 522L,
217L, 927L, 351L, 755L, 914L, 380L, 65L, 844L, 633L, 613L, 222L,
649L, 892L, 752L, 423L, 755L, 169L, 904L, 309L, 639L, 276L, 217L,
394L, 291L, 522L, 203L, 720L, 35L, 422L, 724L, 423L, 720L, 914L,
180L, 327L, 92L, 422L, 258L, 467L, 724L, 620L, 665L, 367L, 639L,
443L, 892L, 724L, 141L, 422L, 327L, 396L, 92L, 309L, 844L, 258L,
914L, 634L, 497L, 222L, 141L, 880L, 467L, 443L, 496L, 913L, 394L,
217L, 35L, 396L, 35L, 880L, 351L, 755L, 474L, 215L), V12 = c(102L,
546L, 682L, 464L, 162L, 876L, 162L, 302L, 682L, 162L, 302L, 53L,
967L, 679L, 837L, 824L, 44L, 53L, 294L, 738L, 254L, 557L, 546L,
7L, 902L, 244L, 128L, 499L, 621L, 499L, 458L, 526L, 837L, 465L,
290L, 969L, 265L, 507L, 835L, 837L, 546L, 136L, 897L, 213L, 195L,
244L, 465L, 835L, 464L, 621L, 162L, 511L, 969L, 230L, 580L, 335L,
610L, 969L, 546L, 897L, 835L, 447L, 526L, 302L, 464L, 302L, 682L,
628L, 610L, 272L, 53L, 254L, 969L, 962L, 511L, 621L, 290L, 458L,
559L, 860L, 136L, 507L, 462L, 136L, 462L, 731L, 873L, 462L, 335L,
897L, 580L, 447L, 628L, 731L, 7L, 335L, 102L, 128L, 679L, 742L
), V13 = c(108L, 637L, 757L, 734L, 534L, 42L, 808L, 322L, 757L,
204L, 808L, 324L, 288L, 82L, 285L, 961L, 955L, 652L, 808L, 961L,
503L, 549L, 697L, 87L, 734L, 43L, 204L, 455L, 398L, 961L, 183L,
433L, 431L, 854L, 490L, 69L, 407L, 808L, 398L, 69L, 87L, 338L,
446L, 178L, 6L, 198L, 82L, 543L, 370L, 534L, 87L, 267L, 455L,
360L, 534L, 407L, 431L, 446L, 854L, 857L, 46L, 637L, 848L, 923L,
560L, 531L, 919L, 223L, 307L, 561L, 6L, 719L, 560L, 43L, 734L,
288L, 324L, 87L, 808L, 322L, 757L, 446L, 425L, 324L, 757L, 857L,
87L, 848L, 223L, 503L, 307L, 152L, 503L, 757L, 956L, 152L, 43L,
69L, 719L, 637L), V14 = c(746L, 805L, 191L, 47L, 508L, 508L,
715L, 461L, 928L, 750L, 140L, 746L, 364L, 552L, 287L, 984L, 481L,
715L, 762L, 959L, 750L, 344L, 959L, 959L, 306L, 911L, 103L, 638L,
759L, 761L, 750L, 444L, 692L, 692L, 761L, 481L, 552L, 942L, 810L,
938L, 306L, 762L, 344L, 942L, 344L, 364L, 552L, 891L, 11L, 103L,
762L, 287L, 891L, 358L, 730L, 959L, 750L, 191L, 718L, 959L, 358L,
306L, 287L, 692L, 746L, 461L, 750L, 170L, 358L, 911L, 805L, 938L,
481L, 759L, 750L, 140L, 715L, 959L, 928L, 692L, 461L, 750L, 306L,
762L, 691L, 306L, 287L, 481L, 170L, 746L, 810L, 762L, 358L, 292L,
750L, 191L, 47L, 942L, 344L, 191L), V15 = c(987L, 972L, 151L,
397L, 250L, 825L, 681L, 825L, 723L, 49L, 585L, 109L, 833L, 137L,
49L, 690L, 681L, 253L, 385L, 921L, 708L, 151L, 109L, 385L, 54L,
247L, 979L, 121L, 225L, 124L, 825L, 417L, 320L, 979L, 681L, 918L,
145L, 397L, 681L, 145L, 586L, 709L, 284L, 840L, 121L, 368L, 250L,
898L, 840L, 109L, 417L, 513L, 544L, 194L, 417L, 544L, 320L, 987L,
840L, 987L, 888L, 489L, 855L, 906L, 62L, 579L, 379L, 783L, 368L,
379L, 49L, 732L, 279L, 509L, 54L, 145L, 797L, 979L, 709L, 840L,
368L, 830L, 502L, 123L, 681L, 194L, 855L, 703L, 247L, 833L, 609L,
830L, 708L, 609L, 509L, 397L, 987L, 609L, 320L, 124L), V16 = c(346L,
48L, 865L, 865L, 173L, 890L, 482L, 13L, 537L, 171L, 482L, 940L,
843L, 173L, 975L, 866L, 142L, 646L, 482L, 700L, 395L, 298L, 975L,
890L, 361L, 173L, 890L, 975L, 940L, 271L, 395L, 989L, 395L, 142L,
865L, 361L, 399L, 441L, 441L, 772L, 142L, 520L, 142L, 520L, 975L,
930L, 890L, 989L, 530L, 866L, 941L, 530L, 596L, 890L, 36L, 441L,
346L, 865L, 173L, 646L, 270L, 441L, 866L, 866L, 346L, 441L, 482L,
872L, 36L, 890L, 271L, 13L, 36L, 836L, 767L, 395L, 890L, 537L,
395L, 530L, 346L, 346L, 940L, 173L, 865L, 772L, 520L, 171L, 48L,
866L, 135L, 298L, 135L, 77L, 361L, 872L, 395L, 596L, 772L, 532L
), V17 = c(912L, 146L, 312L, 22L, 618L, 317L, 618L, 199L, 369L,
101L, 515L, 4L, 476L, 699L, 517L, 317L, 159L, 517L, 553L, 616L,
995L, 314L, 317L, 314L, 562L, 101L, 249L, 369L, 615L, 562L, 476L,
702L, 312L, 312L, 515L, 101L, 159L, 572L, 101L, 618L, 895L, 317L,
616L, 618L, 572L, 562L, 4L, 517L, 312L, 312L, 249L, 699L, 312L,
158L, 469L, 20L, 524L, 476L, 572L, 249L, 50L, 19L, 249L, 912L,
469L, 476L, 101L, 146L, 616L, 618L, 476L, 20L, 146L, 249L, 50L,
101L, 158L, 517L, 238L, 515L, 895L, 553L, 702L, 146L, 312L, 517L,
158L, 895L, 517L, 101L, 314L, 238L, 22L, 146L, 317L, 895L, 469L,
912L, 369L, 572L), V18 = c(525L, 635L, 488L, 456L, 878L, 119L,
119L, 849L, 768L, 817L, 931L, 275L, 460L, 900L, 494L, 669L, 846L,
488L, 768L, 494L, 570L, 439L, 878L, 275L, 471L, 896L, 768L, 619L,
727L, 977L, 155L, 155L, 896L, 112L, 817L, 768L, 411L, 304L, 964L,
612L, 905L, 768L, 456L, 255L, 119L, 404L, 304L, 576L, 219L, 756L,
612L, 668L, 255L, 768L, 196L, 668L, 155L, 931L, 896L, 878L, 488L,
576L, 640L, 37L, 846L, 494L, 257L, 37L, 411L, 411L, 625L, 820L,
304L, 112L, 619L, 9L, 669L, 494L, 471L, 323L, 318L, 570L, 817L,
578L, 878L, 696L, 977L, 768L, 896L, 525L, 669L, 841L, 471L, 727L,
619L, 304L, 874L, 931L, 37L, 619L), V19 = c(926L, 281L, 957L,
308L, 315L, 814L, 622L, 153L, 858L, 315L, 867L, 176L, 555L, 210L,
867L, 540L, 555L, 867L, 622L, 852L, 540L, 436L, 269L, 505L, 436L,
505L, 654L, 505L, 91L, 125L, 131L, 706L, 243L, 125L, 922L, 281L,
91L, 359L, 33L, 957L, 232L, 698L, 555L, 540L, 667L, 34L, 545L,
698L, 555L, 308L, 926L, 445L, 316L, 748L, 243L, 14L, 521L, 232L,
654L, 243L, 232L, 359L, 156L, 131L, 555L, 359L, 521L, 852L, 706L,
957L, 308L, 125L, 91L, 852L, 315L, 604L, 604L, 760L, 604L, 936L,
521L, 747L, 922L, 555L, 243L, 521L, 316L, 867L, 84L, 176L, 814L,
232L, 315L, 316L, 555L, 505L, 745L, 505L, 232L, 540L), V20 = c(554L,
882L, 823L, 386L, 966L, 694L, 286L, 354L, 214L, 25L, 25L, 110L,
353L, 475L, 479L, 252L, 582L, 999L, 266L, 211L, 18L, 278L, 828L,
412L, 528L, 386L, 296L, 353L, 412L, 80L, 206L, 714L, 18L, 211L,
475L, 554L, 38L, 882L, 25L, 362L, 510L, 110L, 206L, 823L, 362L,
694L, 256L, 479L, 582L, 25L, 828L, 193L, 951L, 80L, 793L, 999L,
882L, 903L, 38L, 386L, 354L, 214L, 916L, 25L, 110L, 864L, 882L,
25L, 353L, 780L, 296L, 864L, 510L, 38L, 386L, 400L, 694L, 793L,
999L, 122L, 278L, 475L, 916L, 903L, 958L, 161L, 828L, 73L, 790L,
73L, 430L, 18L, 958L, 828L, 582L, 383L, 51L, 278L, 18L, 122L)), class = "data.frame", row.names = c(NA,
-100L))
Now what I wish to do is reduce the amount, let's say from 100 to 50 entries, where each entry is couple of indices 1 from each group. I tried to calculate the distance matrix using several methods and chose the most distant entries, but when I examined it was not so informative.
Is there a way to do it, maybe to consider the list of lists or other sophisticated methods?
Would appreciate some help/insights
Edit - Clarifing the objective
Lets say I sampled 100 groups where each group contains 1 element from each list of the nested lists.
Some of the groups are close to others, let's say only 1 element is different between the 2 groups, so I will probably will want to discard it. Or even only 2 elements are different etc. But I wish to keep eventually the K groups which as "distant" as possible.
Also nice if it is possible to consider is the amount of elements in a specific nested list, some sort of weighting procedure.
Edit No.2
for the following list(c(1L, 5L, 6L), c(3L, 4L, 2L, 9L), c(8L, 7L, 10L)) we get the following data-frame:
structure(list(V1 = c(1L, 5L, 6L, 1L, 6L, 1L, 1L, 6L, 1L, 5L,
5L, 5L, 1L, 1L, 5L, 6L, 5L, 6L, 6L, 5L, 5L, 5L, 6L, 5L, 6L, 1L,
6L, 1L, 1L, 1L, 5L, 5L, 6L, 6L, 5L, 1L, 6L, 6L, 5L, 6L, 1L, 1L,
5L, 5L, 5L, 1L, 6L, 5L, 1L, 5L, 5L, 5L, 5L, 1L, 5L, 5L, 1L, 6L,
5L, 6L, 5L, 6L, 5L, 1L, 5L, 1L, 5L, 6L, 5L, 1L, 6L, 1L, 6L, 1L,
1L, 5L, 5L, 6L, 1L, 5L, 1L, 5L, 5L, 6L, 6L, 1L, 1L, 6L, 6L, 6L,
5L, 5L, 1L, 6L, 1L, 1L, 6L, 5L, 5L, 1L), V2 = c(9L, 3L, 9L, 4L,
2L, 4L, 3L, 3L, 3L, 2L, 2L, 9L, 3L, 3L, 2L, 2L, 9L, 9L, 9L, 3L,
4L, 3L, 2L, 3L, 4L, 2L, 2L, 3L, 4L, 9L, 9L, 2L, 3L, 2L, 9L, 9L,
3L, 2L, 4L, 4L, 3L, 4L, 3L, 2L, 2L, 9L, 9L, 2L, 4L, 4L, 4L, 9L,
2L, 3L, 9L, 3L, 3L, 2L, 2L, 2L, 4L, 2L, 4L, 3L, 3L, 3L, 2L, 9L,
9L, 9L, 2L, 9L, 3L, 3L, 9L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L,
2L, 9L, 9L, 4L, 9L, 2L, 2L, 9L, 4L, 4L, 9L, 9L, 2L, 4L, 4L, 3L
), V3 = c(7L, 7L, 7L, 8L, 7L, 7L, 7L, 7L, 10L, 8L, 10L, 8L, 7L,
7L, 10L, 10L, 10L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 10L, 7L, 10L,
10L, 7L, 8L, 7L, 8L, 7L, 8L, 8L, 8L, 7L, 8L, 8L, 8L, 10L, 7L,
8L, 7L, 7L, 10L, 7L, 7L, 10L, 7L, 10L, 8L, 8L, 7L, 10L, 10L,
10L, 8L, 8L, 10L, 7L, 8L, 8L, 10L, 8L, 10L, 10L, 10L, 8L, 10L,
10L, 10L, 8L, 10L, 8L, 7L, 10L, 7L, 7L, 10L, 8L, 7L, 8L, 10L,
7L, 8L, 10L, 7L, 7L, 7L, 7L, 10L, 7L, 7L, 10L, 10L, 7L, 7L, 8L,
10L)), class = "data.frame", row.names = c(NA, -100L))
running #Allan Cameron code, will produce the following where there are better 5:
V1 V2 V3
26 1 2 7
68 6 9 10
7 1 3 7
17 5 9 10
13 1 3 7
As you have described it, the concept of overall "distance" between two groups is a bit vague. It's clear that a pair like c(1, 5, 2, 6) and c(2, 9, 12, 3) are closer than the pair c(1, 5, 2, 6) and c(101, 78, 96, 54), but should there be a penalty for an exact match? Is variance important? In the absence of a clearer notion of distance, the best measure we have is the mean of each group. This is easy to obtain by rowMeans(df).
There's also some vagueness with regards to the concept of "the K furthest apart groups". Distance between groups is a function of pairs of groups, not individual groups. If K = 1, then presumably any group is fine. If K = 2, then you want the single pair of groups with the largest difference between their means. After that, it's not clear what you are looking for, but one approach would be to find the set of K groups which has the highest variance.
So if we do something like:
k <- 5
group_means <- rowMeans(df)
indices <- seq(nrow(df))
k_furthest <- c(which.min(group_means), which.max(group_means))
k_vals <- c(min(group_means), max(group_means))
group_means <- group_means[-k_furthest]
indices <- indices[-k_furthest]
while(length(k_furthest) < k)
{
best <- which.max(rowSums(sapply(k_vals, function(x) (x - group_means)^2)))
k_vals <- c(k_vals, group_means[best])
k_furthest <- c(k_furthest, indices[best])
group_means <- group_means[-best]
indices <- indices[-best]
}
Then k_furthest will contain the set of 5 rows of the data frame with the highest possible variance between all the means. Your result would be obtained like:
df[k_furthest,]
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
#> 63 236 794 885 300 71 114 725 492 52 468 92 128 948 191 585 441 414 196 156 18
#> 51 798 536 739 704 1000 883 237 644 299 915 695 860 338 47 972 890 996 939 957 793
#> 61 41 388 624 689 672 466 55 229 454 164 542 265 338 170 32 271 314 640 922 582
#> 33 970 598 775 548 228 132 842 644 986 781 818 679 920 287 825 361 562 756 748 929
#> 12 336 216 774 107 71 801 725 492 642 74 613 297 948 306 124 646 19 439 281 122
Note though that this algorithm effectively just takes the rows with the highest and lowest means alternately on each iteration. Although this produces the largest overall collective "difference" between the samples, you might end up with some samples that are very close together, provided that they are also both very far apart from another sample. This may not be what you are looking for, and it is why it might be a good idea to specify exactly what you mean by "distance" in this context.
EDIT
With further clarification and a new example from the OP, it seems that we are looking to maximize the sum of element-wise difference between groups. This means we can do:
distances <- as.data.frame(t(sapply(1:nrow(df), function(i) {
a <- rowSums(apply(df, 2, function(x) abs(x[i] - x)))
c(row = i, most_distant = which.max(a), difference = max(a))
})))
This will give us a data frame which for each row tells us the most "distant" other group.
head(distances)
#> row most_distant difference
#> 1 1 16 15
#> 2 2 46 13
#> 3 3 9 14
#> 4 4 68 12
#> 5 5 46 15
#> 6 6 68 13
If we sort this according to the biggest difference, and take the first K groups mentioned in the first two columns, we will have our result:
i <- unique(c(t(distances[order(-distances$difference)[seq(k)], 1:2])))[seq(k)]
df[i,]
#> V1 V2 V3
#> 1 1 9 7
#> 16 6 2 10
#> 5 6 2 7
#> 46 1 9 10
#> 26 1 2 7

How to partition using createPartition

I have this data below. I am having problem partitioning this using caret's createPartition.
gg <- structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 145L, 145L, 145L, 146L, 146L, 146L, 147L,
147L, 147L, 148L, 148L, 148L, 149L, 149L, 149L, 150L, 150L, 150L,
193L, 193L, 193L, 194L, 194L, 194L, 195L, 195L, 195L, 196L, 196L,
196L, 197L, 197L, 197L, 198L, 198L, 198L, 199L, 199L, 199L, 200L,
200L, 200L, 201L, 201L, 201L, 202L, 202L, 202L, 203L, 203L, 203L,
204L, 204L, 204L, 205L, 205L, 205L, 206L, 206L, 206L, 207L, 207L,
207L, 208L, 208L, 208L, 209L, 209L, 209L, 210L, 210L, 210L, 211L,
211L, 211L, 212L, 212L, 212L, 213L, 213L, 213L, 214L, 214L, 214L,
215L, 215L, 215L, 216L, 216L, 216L, 217L, 217L, 217L, 218L, 218L,
218L, 219L, 219L, 219L, 220L, 220L, 220L, 221L, 221L, 221L, 222L,
222L, 222L, 223L, 223L, 223L, 224L, 224L, 224L, 225L, 225L, 225L,
226L, 226L, 226L, 227L, 227L, 227L, 228L, 228L, 228L, 229L, 229L,
229L, 230L, 230L, 230L, 231L, 231L, 231L, 232L, 232L, 232L, 233L,
233L, 233L, 234L, 234L, 234L, 235L, 235L, 235L, 236L, 236L, 236L,
237L, 237L, 237L, 238L, 238L, 238L, 239L, 239L, 239L, 240L, 240L,
240L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L,
11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L,
15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L,
20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L,
24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L,
28L, 29L, 29L, 29L, 30L, 30L, 30L, 31L, 31L, 31L, 32L, 32L, 32L,
33L, 33L, 33L, 34L, 34L, 34L, 35L, 35L, 35L, 36L, 36L, 36L, 37L,
37L, 37L, 38L, 38L, 38L, 39L, 39L, 39L, 40L, 40L, 40L, 41L, 41L,
41L, 42L, 42L, 42L, 43L, 43L, 43L, 44L, 44L, 44L, 45L, 45L, 45L,
46L, 46L, 46L, 47L, 47L, 47L, 48L, 48L, 48L, 49L, 49L, 49L, 50L,
50L, 50L, 51L, 51L, 51L, 52L, 52L, 52L, 53L, 53L, 53L, 54L, 54L,
54L, 55L, 55L, 55L, 56L, 56L, 56L, 57L, 57L, 57L, 58L, 58L, 58L,
59L, 59L, 59L, 60L, 60L, 60L, 61L, 61L, 61L, 62L, 62L, 62L, 63L,
63L, 63L, 64L, 64L, 64L, 65L, 65L, 65L, 66L, 66L, 66L, 67L, 67L,
67L, 68L, 68L, 68L, 69L, 69L, 69L, 70L, 70L, 70L, 71L, 71L, 71L,
72L, 72L, 72L, 73L, 73L, 73L, 74L, 74L, 74L, 75L, 75L, 75L, 76L,
76L, 76L, 77L, 77L, 77L, 78L, 78L, 78L, 79L, 79L, 79L, 80L, 80L,
80L, 81L, 81L, 81L, 82L, 82L, 82L, 83L, 83L, 83L, 84L, 84L, 84L,
85L, 85L, 85L, 86L, 86L, 86L, 87L, 87L, 87L, 88L, 88L, 88L, 89L,
89L, 89L, 90L, 90L, 90L, 91L, 91L, 91L, 92L, 92L, 92L, 93L, 93L,
93L, 94L, 94L, 94L, 95L, 95L, 95L, 96L, 96L, 96L, 97L, 97L, 97L,
98L, 98L, 98L, 99L, 99L, 99L, 100L, 100L, 100L, 101L, 101L, 101L,
102L, 102L, 102L, 103L, 103L, 103L, 104L, 104L, 104L, 105L, 105L,
105L, 106L, 106L, 106L, 107L, 107L, 107L, 108L, 108L, 108L, 109L,
109L, 109L, 110L, 110L, 110L, 111L, 111L, 111L, 112L, 112L, 112L,
113L, 113L, 113L, 114L, 114L, 114L, 115L, 115L, 115L, 116L, 116L,
116L, 117L, 117L, 117L, 118L, 118L, 118L, 119L, 119L, 119L, 120L,
120L, 120L, 121L, 121L, 121L, 122L, 122L, 122L, 123L, 123L, 123L,
124L, 124L, 124L, 125L, 125L, 125L, 126L, 126L, 126L, 127L, 127L,
127L, 128L, 128L, 128L, 129L, 129L, 129L, 130L, 130L, 130L, 131L,
131L, 131L, 132L, 132L, 132L, 151L, 151L, 151L, 152L, 152L, 152L,
153L, 153L, 153L, 154L, 154L, 154L, 155L, 155L, 155L, 156L, 156L,
156L, 157L, 157L, 157L, 158L, 158L, 158L, 159L, 159L, 159L, 160L,
160L, 160L, 161L, 161L, 161L, 162L, 162L, 162L, 163L, 163L, 163L,
164L, 164L, 164L, 165L, 165L, 165L, 166L, 166L, 166L, 167L, 167L,
167L, 168L, 168L, 168L, 169L, 169L, 169L, 170L, 170L, 170L, 171L,
171L, 171L, 172L, 172L, 172L, 173L, 173L, 173L, 174L, 174L, 174L,
175L, 175L, 175L, 176L, 176L, 176L, 177L, 177L, 177L, 178L, 178L,
178L, 179L, 179L, 179L, 180L, 180L, 180L, 181L, 181L, 181L, 182L,
182L, 182L, 183L, 183L, 183L, 184L, 184L, 184L, 185L, 185L, 185L,
186L, 186L, 186L, 187L, 187L, 187L, 188L, 188L, 188L, 189L, 189L,
189L, 190L, 190L, 190L, 191L, 191L, 191L, 192L, 192L, 192L, 133L,
133L, 133L, 134L, 134L, 134L, 135L, 135L, 135L, 136L, 136L, 136L,
137L, 137L, 137L, 138L, 138L, 138L, 139L, 139L, 139L, 140L, 140L,
140L, 141L, 141L, 141L, 142L, 142L, 142L, 143L, 143L, 143L, 144L,
144L, 144L, 241L, 241L, 241L, 242L, 242L, 242L, 243L, 243L, 243L,
244L, 244L, 244L, 245L, 245L, 245L, 246L, 246L, 246L, 385L, 385L,
385L, 386L, 386L, 386L, 387L, 387L, 387L, 388L, 388L, 388L, 389L,
389L, 389L, 390L, 390L, 390L, 433L, 433L, 433L, 434L, 434L, 434L,
435L, 435L, 435L, 436L, 436L, 436L, 437L, 437L, 437L, 438L, 438L,
438L, 439L, 439L, 439L, 440L, 440L, 440L, 441L, 441L, 441L, 442L,
442L, 442L, 443L, 443L, 443L, 444L, 444L, 444L, 445L, 445L, 445L,
446L, 446L, 446L, 447L, 447L, 447L, 448L, 448L, 448L, 449L, 449L,
449L, 450L, 450L, 450L, 451L, 451L, 451L, 452L, 452L, 452L, 453L,
453L, 453L, 454L, 454L, 454L, 455L, 455L, 455L, 456L, 456L, 456L,
457L, 457L, 457L, 458L, 458L, 458L, 459L, 459L, 459L, 460L, 460L,
460L, 461L, 461L, 461L, 462L, 462L, 462L, 463L, 463L, 463L, 464L,
464L, 464L, 465L, 465L, 465L, 466L, 466L, 466L, 467L, 467L, 467L,
468L, 468L, 468L, 469L, 469L, 469L, 470L, 470L, 470L, 471L, 471L,
471L, 472L, 472L, 472L, 473L, 473L, 473L, 474L, 474L, 474L, 475L,
475L, 475L, 476L, 476L, 476L, 477L, 477L, 477L, 478L, 478L, 478L,
479L, 479L, 479L, 480L, 480L, 480L, 247L, 247L, 247L, 248L, 248L,
248L, 249L, 249L, 249L, 250L, 250L, 250L, 251L, 251L, 251L, 252L,
252L, 252L, 253L, 253L, 253L, 254L, 254L, 254L, 255L, 255L, 255L,
256L, 256L, 256L, 257L, 257L, 257L, 258L, 258L, 258L, 259L, 259L,
259L, 260L, 260L, 260L, 261L, 261L, 261L, 262L, 262L, 262L, 263L,
263L, 263L, 264L, 264L, 264L, 265L, 265L, 265L, 266L, 266L, 266L,
267L, 267L, 267L, 268L, 268L, 268L, 269L, 269L, 269L, 270L, 270L,
270L, 271L, 271L, 271L, 272L, 272L, 272L, 273L, 273L, 273L, 274L,
274L, 274L, 275L, 275L, 275L, 276L, 276L, 276L, 277L, 277L, 277L,
278L, 278L, 278L, 279L, 279L, 279L, 280L, 280L, 280L, 281L, 281L,
281L, 282L, 282L, 282L, 283L, 283L, 283L, 284L, 284L, 284L, 285L,
285L, 285L, 286L, 286L, 286L, 287L, 287L, 287L, 288L, 288L, 288L,
289L, 289L, 289L, 290L, 290L, 290L, 291L, 291L, 291L, 292L, 292L,
292L, 293L, 293L, 293L, 294L, 294L, 294L, 295L, 295L, 295L, 296L,
296L, 296L, 297L, 297L, 297L, 298L, 298L, 298L, 299L, 299L, 299L,
300L, 300L, 300L, 301L, 301L, 301L, 302L, 302L, 302L, 303L, 303L,
303L, 304L, 304L, 304L, 305L, 305L, 305L, 306L, 306L, 306L, 307L,
307L, 307L, 308L, 308L, 308L, 309L, 309L, 309L, 310L, 310L, 310L,
311L, 311L, 311L, 312L, 312L, 312L, 319L, 319L, 319L, 320L, 320L,
320L, 321L, 321L, 321L, 322L, 322L, 322L, 323L, 323L, 323L, 324L,
324L, 324L, 325L, 325L, 325L, 326L, 326L, 326L, 327L, 327L, 327L,
328L, 328L, 328L, 329L, 329L, 329L, 330L, 330L, 330L, 331L, 331L,
331L, 332L, 332L, 332L, 333L, 333L, 333L, 334L, 334L, 334L, 335L,
335L, 335L, 336L, 336L, 336L, 337L, 337L, 337L, 338L, 338L, 338L,
339L, 339L, 339L, 340L, 340L, 340L, 341L, 341L, 341L, 342L, 342L,
342L, 343L, 343L, 343L, 344L, 344L, 344L, 345L, 345L, 345L, 346L,
346L, 346L, 347L, 347L, 347L, 348L, 348L, 348L, 349L, 349L, 349L,
350L, 350L, 350L, 351L, 351L, 351L, 352L, 352L, 352L, 353L, 353L,
353L, 354L, 354L, 354L, 355L, 355L, 355L, 356L, 356L, 356L, 357L,
357L, 357L, 358L, 358L, 358L, 359L, 359L, 359L, 360L, 360L, 360L,
361L, 361L, 361L, 362L, 362L, 362L, 363L, 363L, 363L, 364L, 364L,
364L, 365L, 365L, 365L, 366L, 366L, 366L, 367L, 367L, 367L, 368L,
368L, 368L, 369L, 369L, 369L, 370L, 370L, 370L, 371L, 371L, 371L,
372L, 372L, 372L, 391L, 391L, 391L, 392L, 392L, 392L, 393L, 393L,
393L, 394L, 394L, 394L, 395L, 395L, 395L, 396L, 396L, 396L, 397L,
397L, 397L, 398L, 398L, 398L, 399L, 399L, 399L, 400L, 400L, 400L,
401L, 401L, 401L, 402L, 402L, 402L, 403L, 403L, 403L, 404L, 404L,
404L, 405L, 405L, 405L, 406L, 406L, 406L, 407L, 407L, 407L, 408L,
408L, 408L, 409L, 409L, 409L, 410L, 410L, 410L, 411L, 411L, 411L,
412L, 412L, 412L, 413L, 413L, 413L, 414L, 414L, 414L, 415L, 415L,
415L, 416L, 416L, 416L, 417L, 417L, 417L, 418L, 418L, 418L, 419L,
419L, 419L, 420L, 420L, 420L, 421L, 421L, 421L, 422L, 422L, 422L,
423L, 423L, 423L, 424L, 424L, 424L, 425L, 425L, 425L, 426L, 426L,
426L, 427L, 427L, 427L, 428L, 428L, 428L, 429L, 429L, 429L, 430L,
430L, 430L, 431L, 431L, 431L, 432L, 432L, 432L, 373L, 373L, 373L,
374L, 374L, 374L, 375L, 375L, 375L, 376L, 376L, 376L, 377L, 377L,
377L, 378L, 378L, 378L, 379L, 379L, 379L, 380L, 380L, 380L, 381L,
381L, 381L, 382L, 382L, 382L, 383L, 383L, 383L, 384L, 384L, 384L,
313L, 313L, 313L, 314L, 314L, 314L, 315L, 315L, 315L, 316L, 316L,
316L, 317L, 317L, 317L, 318L, 318L, 318L), .Label = c("CUR:0:L1",
"CUR:0:L2", "CUR:0:L3", "CUR:0:L4", "CUR:0:L5", "CUR:0:L6", "CUR:00A:L1",
"CUR:00A:L2", "CUR:00A:L3", "CUR:00A:L4", "CUR:00A:L5", "CUR:00A:L6",
"CUR:00B:L1", "CUR:00B:L2", "CUR:00B:L3", "CUR:00B:L4", "CUR:00B:L5",
"CUR:00B:L6", "CUR:00C:L1", "CUR:00C:L2", "CUR:00C:L3", "CUR:00C:L4",
"CUR:00C:L5", "CUR:00C:L6", "CUR:00D:L1", "CUR:00D:L2", "CUR:00D:L3",
"CUR:00D:L4", "CUR:00D:L5", "CUR:00D:L6", "CUR:00F:L1", "CUR:00F:L2",
"CUR:00F:L3", "CUR:00F:L4", "CUR:00F:L5", "CUR:00F:L6", "CUR:00H:L1",
"CUR:00H:L2", "CUR:00H:L3", "CUR:00H:L4", "CUR:00H:L5", "CUR:00H:L6",
"CUR:00I:L1", "CUR:00I:L2", "CUR:00I:L3", "CUR:00I:L4", "CUR:00I:L5",
"CUR:00I:L6", "CUR:00J:L1", "CUR:00J:L2", "CUR:00J:L3", "CUR:00J:L4",
"CUR:00J:L5", "CUR:00J:L6", "CUR:00K:L1", "CUR:00K:L2", "CUR:00K:L3",
"CUR:00K:L4", "CUR:00K:L5", "CUR:00K:L6", "CUR:00L:L1", "CUR:00L:L2",
"CUR:00L:L3", "CUR:00L:L4", "CUR:00L:L5", "CUR:00L:L6", "CUR:00N:L1",
"CUR:00N:L2", "CUR:00N:L3", "CUR:00N:L4", "CUR:00N:L5", "CUR:00N:L6",
"CUR:00O:L1", "CUR:00O:L2", "CUR:00O:L3", "CUR:00O:L4", "CUR:00O:L5",
"CUR:00O:L6", "CUR:00P:L1", "CUR:00P:L2", "CUR:00P:L3", "CUR:00P:L4",
"CUR:00P:L5", "CUR:00P:L6", "CUR:00Q:L1", "CUR:00Q:L2", "CUR:00Q:L3",
"CUR:00Q:L4", "CUR:00Q:L5", "CUR:00Q:L6", "CUR:00R:L1", "CUR:00R:L2",
"CUR:00R:L3", "CUR:00R:L4", "CUR:00R:L5", "CUR:00R:L6", "CUR:00T:L1",
"CUR:00T:L2", "CUR:00T:L3", "CUR:00T:L4", "CUR:00T:L5", "CUR:00T:L6",
"CUR:00U:L1", "CUR:00U:L2", "CUR:00U:L3", "CUR:00U:L4", "CUR:00U:L5",
"CUR:00U:L6", "CUR:00V:L1", "CUR:00V:L2", "CUR:00V:L3", "CUR:00V:L4",
"CUR:00V:L5", "CUR:00V:L6", "CUR:00W:L1", "CUR:00W:L2", "CUR:00W:L3",
"CUR:00W:L4", "CUR:00W:L5", "CUR:00W:L6", "CUR:00X:L1", "CUR:00X:L2",
"CUR:00X:L3", "CUR:00X:L4", "CUR:00X:L5", "CUR:00X:L6", "CUR:00Z:L1",
"CUR:00Z:L2", "CUR:00Z:L3", "CUR:00Z:L4", "CUR:00Z:L5", "CUR:00Z:L6",
"CUR:01A:L1", "CUR:01A:L2", "CUR:01A:L3", "CUR:01A:L4", "CUR:01A:L5",
"CUR:01A:L6", "CUR:01B:L1", "CUR:01B:L2", "CUR:01B:L3", "CUR:01B:L4",
"CUR:01B:L5", "CUR:01B:L6", "CUR:1:L1", "CUR:1:L2", "CUR:1:L3",
"CUR:1:L4", "CUR:1:L5", "CUR:1:L6", "CUR:10:L1", "CUR:10:L2",
"CUR:10:L3", "CUR:10:L4", "CUR:10:L5", "CUR:10:L6", "CUR:11:L1",
"CUR:11:L2", "CUR:11:L3", "CUR:11:L4", "CUR:11:L5", "CUR:11:L6",
"CUR:12:L1", "CUR:12:L2", "CUR:12:L3", "CUR:12:L4", "CUR:12:L5",
"CUR:12:L6", "CUR:13:L1", "CUR:13:L2", "CUR:13:L3", "CUR:13:L4",
"CUR:13:L5", "CUR:13:L6", "CUR:16:L1", "CUR:16:L2", "CUR:16:L3",
"CUR:16:L4", "CUR:16:L5", "CUR:16:L6", "CUR:18:L1", "CUR:18:L2",
"CUR:18:L3", "CUR:18:L4", "CUR:18:L5", "CUR:18:L6", "CUR:19:L1",
"CUR:19:L2", "CUR:19:L3", "CUR:19:L4", "CUR:19:L5", "CUR:19:L6",
"CUR:2:L1", "CUR:2:L2", "CUR:2:L3", "CUR:2:L4", "CUR:2:L5", "CUR:2:L6",
"CUR:3:L1", "CUR:3:L2", "CUR:3:L3", "CUR:3:L4", "CUR:3:L5", "CUR:3:L6",
"CUR:4:L1", "CUR:4:L2", "CUR:4:L3", "CUR:4:L4", "CUR:4:L5", "CUR:4:L6",
"CUR:5:L1", "CUR:5:L2", "CUR:5:L3", "CUR:5:L4", "CUR:5:L5", "CUR:5:L6",
"CUR:6:L1", "CUR:6:L2", "CUR:6:L3", "CUR:6:L4", "CUR:6:L5", "CUR:6:L6",
"CUR:7:L1", "CUR:7:L2", "CUR:7:L3", "CUR:7:L4", "CUR:7:L5", "CUR:7:L6",
"CUR:8:L1", "CUR:8:L2", "CUR:8:L3", "CUR:8:L4", "CUR:8:L5", "CUR:8:L6",
"CUR:9:L1", "CUR:9:L2", "CUR:9:L3", "CUR:9:L4", "CUR:9:L5", "CUR:9:L6",
"PRI:0:L1", "PRI:0:L2", "PRI:0:L3", "PRI:0:L4", "PRI:0:L5", "PRI:0:L6",
"PRI:00A:L1", "PRI:00A:L2", "PRI:00A:L3", "PRI:00A:L4", "PRI:00A:L5",
"PRI:00A:L6", "PRI:00B:L1", "PRI:00B:L2", "PRI:00B:L3", "PRI:00B:L4",
"PRI:00B:L5", "PRI:00B:L6", "PRI:00C:L1", "PRI:00C:L2", "PRI:00C:L3",
"PRI:00C:L4", "PRI:00C:L5", "PRI:00C:L6", "PRI:00D:L1", "PRI:00D:L2",
"PRI:00D:L3", "PRI:00D:L4", "PRI:00D:L5", "PRI:00D:L6", "PRI:00F:L1",
"PRI:00F:L2", "PRI:00F:L3", "PRI:00F:L4", "PRI:00F:L5", "PRI:00F:L6",
"PRI:00H:L1", "PRI:00H:L2", "PRI:00H:L3", "PRI:00H:L4", "PRI:00H:L5",
"PRI:00H:L6", "PRI:00I:L1", "PRI:00I:L2", "PRI:00I:L3", "PRI:00I:L4",
"PRI:00I:L5", "PRI:00I:L6", "PRI:00J:L1", "PRI:00J:L2", "PRI:00J:L3",
"PRI:00J:L4", "PRI:00J:L5", "PRI:00J:L6", "PRI:00K:L1", "PRI:00K:L2",
"PRI:00K:L3", "PRI:00K:L4", "PRI:00K:L5", "PRI:00K:L6", "PRI:00L:L1",
"PRI:00L:L2", "PRI:00L:L3", "PRI:00L:L4", "PRI:00L:L5", "PRI:00L:L6",
"PRI:00N:L1", "PRI:00N:L2", "PRI:00N:L3", "PRI:00N:L4", "PRI:00N:L5",
"PRI:00N:L6", "PRI:00O:L1", "PRI:00O:L2", "PRI:00O:L3", "PRI:00O:L4",
"PRI:00O:L5", "PRI:00O:L6", "PRI:00P:L1", "PRI:00P:L2", "PRI:00P:L3",
"PRI:00P:L4", "PRI:00P:L5", "PRI:00P:L6", "PRI:00Q:L1", "PRI:00Q:L2",
"PRI:00Q:L3", "PRI:00Q:L4", "PRI:00Q:L5", "PRI:00Q:L6", "PRI:00R:L1",
"PRI:00R:L2", "PRI:00R:L3", "PRI:00R:L4", "PRI:00R:L5", "PRI:00R:L6",
"PRI:00T:L1", "PRI:00T:L2", "PRI:00T:L3", "PRI:00T:L4", "PRI:00T:L5",
"PRI:00T:L6", "PRI:00U:L1", "PRI:00U:L2", "PRI:00U:L3", "PRI:00U:L4",
"PRI:00U:L5", "PRI:00U:L6", "PRI:00V:L1", "PRI:00V:L2", "PRI:00V:L3",
"PRI:00V:L4", "PRI:00V:L5", "PRI:00V:L6", "PRI:00W:L1", "PRI:00W:L2",
"PRI:00W:L3", "PRI:00W:L4", "PRI:00W:L5", "PRI:00W:L6", "PRI:00X:L1",
"PRI:00X:L2", "PRI:00X:L3", "PRI:00X:L4", "PRI:00X:L5", "PRI:00X:L6",
"PRI:00Z:L1", "PRI:00Z:L2", "PRI:00Z:L3", "PRI:00Z:L4", "PRI:00Z:L5",
"PRI:00Z:L6", "PRI:01A:L1", "PRI:01A:L2", "PRI:01A:L3", "PRI:01A:L4",
"PRI:01A:L5", "PRI:01A:L6", "PRI:01B:L1", "PRI:01B:L2", "PRI:01B:L3",
"PRI:01B:L4", "PRI:01B:L5", "PRI:01B:L6", "PRI:1:L1", "PRI:1:L2",
"PRI:1:L3", "PRI:1:L4", "PRI:1:L5", "PRI:1:L6", "PRI:10:L1",
"PRI:10:L2", "PRI:10:L3", "PRI:10:L4", "PRI:10:L5", "PRI:10:L6",
"PRI:11:L1", "PRI:11:L2", "PRI:11:L3", "PRI:11:L4", "PRI:11:L5",
"PRI:11:L6", "PRI:12:L1", "PRI:12:L2", "PRI:12:L3", "PRI:12:L4",
"PRI:12:L5", "PRI:12:L6", "PRI:13:L1", "PRI:13:L2", "PRI:13:L3",
"PRI:13:L4", "PRI:13:L5", "PRI:13:L6", "PRI:16:L1", "PRI:16:L2",
"PRI:16:L3", "PRI:16:L4", "PRI:16:L5", "PRI:16:L6", "PRI:18:L1",
"PRI:18:L2", "PRI:18:L3", "PRI:18:L4", "PRI:18:L5", "PRI:18:L6",
"PRI:19:L1", "PRI:19:L2", "PRI:19:L3", "PRI:19:L4", "PRI:19:L5",
"PRI:19:L6", "PRI:2:L1", "PRI:2:L2", "PRI:2:L3", "PRI:2:L4",
"PRI:2:L5", "PRI:2:L6", "PRI:3:L1", "PRI:3:L2", "PRI:3:L3", "PRI:3:L4",
"PRI:3:L5", "PRI:3:L6", "PRI:4:L1", "PRI:4:L2", "PRI:4:L3", "PRI:4:L4",
"PRI:4:L5", "PRI:4:L6", "PRI:5:L1", "PRI:5:L2", "PRI:5:L3", "PRI:5:L4",
"PRI:5:L5", "PRI:5:L6", "PRI:6:L1", "PRI:6:L2", "PRI:6:L3", "PRI:6:L4",
"PRI:6:L5", "PRI:6:L6", "PRI:7:L1", "PRI:7:L2", "PRI:7:L3", "PRI:7:L4",
"PRI:7:L5", "PRI:7:L6", "PRI:8:L1", "PRI:8:L2", "PRI:8:L3", "PRI:8:L4",
"PRI:8:L5", "PRI:8:L6", "PRI:9:L1", "PRI:9:L2", "PRI:9:L3", "PRI:9:L4",
"PRI:9:L5", "PRI:9:L6"), class = "factor")
I wanted to use caret to partition my data, so this is what I did:
library(caret)
train.rows<- createDataPartition(gg, p=0.7,list = FALSE)
> length(train.rows)
[1] 1440
However, I am getting everything in gg in my train.rows even after 0.7 partitioning. What am I missing here?
Try it without class = factor
Then your partitioned vector will be:
indexes <- caret::createDataPartition(gg, times = 1, p = 0.7, list=FALSE)
train <- gg[indexes]
test <- gg[-indexes]

How to decompose trend and seasonality using STL in R?

I have weekly data for 3 years. Now my objective is to remove the trend and seasonality effects from the series using STL function.
I can decompose time series components using decompose function in stats package. But I am getting NA values for first and last 52 values of trend and random effects.
In my sample dataset there is perfect seasonality and mean and varience are changing over time. So, I wanted to build multiplicative model. Here I have used stl function in stats package to decompose trend and seasonality. I know that stl function can handle additive model. But we can build multiplicative model also by using log transformation. Here I tried both of the models. But I am not getting as results as expected. I am sure that i am missing something in this code.
series<-ts(series,frequency=365.25/7,start(2013,9))
series<-structure(c(62L, 72L, 48L, 50L, 302L, 396L, 66L, 33L, 77L, 91L,
38L, 38L, 43L, 45L, 134L, 754L, 1011L, 901L, 483L, 237L, 99L,
59L, 92L, 65L, 120L, 214L, 329L, 387L, 276L, 307L, 395L, 372L,
332L, 258L, 291L, 359L, 211L, 308L, 250L, 1374L, 1131L, 845L,
588L, 770L, 499L, 532L, 491L, 359L, 318L, 219L, 153L, 138L, 156L,
133L, 92L, 77L, 214L, 273L, 86L, 75L, 51L, 163L, 72L, 191L, 62L,
49L, 79L, 573L, 569L, 444L, 410L, 404L, 345L, 141L, 146L, 179L,
127L, 143L, 382L, 548L, 283L, 315L, 392L, 394L, 313L, 373L, 603L,
429L, 384L, 419L, 449L, 1774L, 2025L, 1532L, 1252L, 857L, 790L,
658L, 389L, 398L, 398L, 302L, 237L, 249L, 182L, 167L, 109L, 179L,
377L, 288L, 146L, 126L, 449L, 138L, 580L, 130L, 94L, 150L, 173L,
1246L, 1227L, 991L, 707L, 489L, 592L, 326L, 209L, 259L, 286L,
243L, 344L, 335L, 368L, 397L, 349L, 313L, 1345L, 301L, 1111L,
366L, 274L, 302L, 248L, 2518L, 2186L, 2094L, 2151L, 1847L, 1384L,
666L, 455L, 415L, 302L, 277L, 172L, 186L), .Tsp = c(1, 3.97056810403833,
52.1785714285714), class = "ts")
#Model 1
model1<-stl(series,"periodic",robust="TRUE")
op<-as.data.frame(model1$time.series)
head(op,25)
matplot(op,type="l")
#Model 2
model2<-stl(log(series),"periodic",robust="TRUE")
op<-exp(as.data.frame(model2$time.series))
matplot(op,type="l")
How can I improve the model performance?
Please suggest me if there are any better ways to solve with this problem.
Thanks in advance.

R - Conditionally replace multiple rows in a dataframe

Hi programming fellows,
Please consider the following data frame:
df <- structure(list(date = structure(c(1251350100.288, 1251351900,
1251353699.712, 1251355500.288, 1251357300, 1251359099.712), class = c("POSIXct",
"POSIXt")), mix.ratio.csi = c(442.78316237477, 436.757082063885,
425.742872761246, 395.770804307671, 386.758335309866, 392.115887652156
), mix.ratio.licor = c(447.141491945547, 441.319548211994, 430.854166343173,
402.232640566763, 393.683007533694, 398.388336602215), ToKeep = c(FALSE,
FALSE, TRUE, TRUE, TRUE, TRUE)), .Names = c("date", "value1",
"value2", "ToKeep"), index = structure(integer(0), ToKeep = c(1L,
2L, 8L, 52L, 53L, 54L, 55L, 85L, 86L, 87L, 88L, 89L, 92L, 93L,
94L, 95L, 96L, 97L, 98L, 99L, 100L, 102L, 103L, 105L, 106L, 192L,
193L, 220L, 223L, 225L, 228L, 229L, 260L, 263L, 264L, 265L, 266L,
267L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L,
315L, 352L, 353L, 354L, 375L, 376L, 378L, 379L, 380L, 383L, 411L,
412L, 413L, 414L, 415L, 416L, 418L, 419L, 445L, 453L, 463L, 464L,
465L, 466L, 467L, 468L, 497L, 504L, 547L, 548L, 549L, 586L, 589L,
630L, 631L, 632L, 633L, 634L, 635L, 636L, 644L, 645L, 646L, 647L,
648L, 649L, 650L, 651L, 674L, 675L, 676L, 677L, 678L, 682L, 687L,
690L, 691L, 724L, 725L, 726L, 727L, 728L, 729L, 730L, 731L, 732L,
733L, 734L, 735L, 736L, 739L, 740L, 741L, 742L, 768L, 771L, 772L,
773L, 774L, 775L, 776L, 777L, 778L, 779L, 3L, 4L, 5L, 6L, 7L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 90L, 91L, 101L, 104L, 107L,
108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L,
119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L,
130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L,
152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L,
163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L,
174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L,
185L, 186L, 187L, 188L, 189L, 190L, 191L, 194L, 195L, 196L, 197L,
198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L,
209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L,
221L, 222L, 224L, 226L, 227L, 230L, 231L, 232L, 233L, 234L, 235L,
236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L,
247L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L,
258L, 259L, 261L, 262L, 268L, 269L, 270L, 271L, 272L, 273L, 274L,
275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L,
286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L,
297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 316L, 317L, 318L,
319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L,
330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L,
341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L,
355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L, 364L, 365L,
366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 377L, 381L,
382L, 384L, 385L, 386L, 387L, 388L, 389L, 390L, 391L, 392L, 393L,
394L, 395L, 396L, 397L, 398L, 399L, 400L, 401L, 402L, 403L, 404L,
405L, 406L, 407L, 408L, 409L, 410L, 417L, 420L, 421L, 422L, 423L,
424L, 425L, 426L, 427L, 428L, 429L, 430L, 431L, 432L, 433L, 434L,
435L, 436L, 437L, 438L, 439L, 440L, 441L, 442L, 443L, 444L, 446L,
447L, 448L, 449L, 450L, 451L, 452L, 454L, 455L, 456L, 457L, 458L,
459L, 460L, 461L, 462L, 469L, 470L, 471L, 472L, 473L, 474L, 475L,
476L, 477L, 478L, 479L, 480L, 481L, 482L, 483L, 484L, 485L, 486L,
487L, 488L, 489L, 490L, 491L, 492L, 493L, 494L, 495L, 496L, 498L,
499L, 500L, 501L, 502L, 503L, 505L, 506L, 507L, 508L, 509L, 510L,
511L, 512L, 513L, 514L, 515L, 516L, 517L, 518L, 519L, 520L, 521L,
522L, 523L, 524L, 525L, 526L, 527L, 528L, 529L, 530L, 531L, 532L,
533L, 534L, 535L, 536L, 537L, 538L, 539L, 540L, 541L, 542L, 543L,
544L, 545L, 546L, 550L, 551L, 552L, 553L, 554L, 555L, 556L, 557L,
558L, 559L, 560L, 561L, 562L, 563L, 564L, 565L, 566L, 567L, 568L,
569L, 570L, 571L, 572L, 573L, 574L, 575L, 576L, 577L, 578L, 579L,
580L, 581L, 582L, 583L, 584L, 585L, 587L, 588L, 590L, 591L, 592L,
593L, 594L, 595L, 596L, 597L, 598L, 599L, 600L, 601L, 602L, 603L,
604L, 605L, 606L, 607L, 608L, 609L, 610L, 611L, 612L, 613L, 614L,
615L, 616L, 617L, 618L, 619L, 620L, 621L, 622L, 623L, 624L, 625L,
626L, 627L, 628L, 629L, 637L, 638L, 639L, 640L, 641L, 642L, 643L,
652L, 653L, 654L, 655L, 656L, 657L, 658L, 659L, 660L, 661L, 662L,
663L, 664L, 665L, 666L, 667L, 668L, 669L, 670L, 671L, 672L, 673L,
679L, 680L, 681L, 683L, 684L, 685L, 686L, 688L, 689L, 692L, 693L,
694L, 695L, 696L, 697L, 698L, 699L, 700L, 701L, 702L, 703L, 704L,
705L, 706L, 707L, 708L, 709L, 710L, 711L, 712L, 713L, 714L, 715L,
716L, 717L, 718L, 719L, 720L, 721L, 722L, 723L, 737L, 738L, 743L,
744L, 745L, 746L, 747L, 748L, 749L, 750L, 751L, 752L, 753L, 754L,
755L, 756L, 757L, 758L, 759L, 760L, 761L, 762L, 763L, 764L, 765L,
766L, 767L, 769L, 770L, 780L, 781L, 782L, 783L, 784L, 785L, 786L,
787L, 788L, 789L)), row.names = c(NA, 6L), class = "data.frame")
I need to create a new data.frame with the following structure:
1) if column 'ToKeep' is TRUE, then columns 'date', 'value1' and 'value2' remain the same;
2) if column 'ToKeep' is FALSE, then columns 'value1' e 'value2' receive NA (and 'date' remains the same).
I have been trying to use ifelse so far, but still haven't found the right indexing procedure:
df[, c(2,3)] <- lapply(df[, 4], function(x) ifelse(x == FALSE, NA, x))
Any suggestion?
Thanks in advance,
Thiago.
You can use the logical column to subset the rows, choose the columns you want, then assign the NA values with [<-
df2 <- df ## so that we don't over-write the original data set
df2[!df2$ToKeep, c("value1", "value2")] <- NA
which results in
df2
# date value1 value2 ToKeep
# 1 2009-08-26 22:15:00 NA NA FALSE
# 2 2009-08-26 22:45:00 NA NA FALSE
# 3 2009-08-26 23:14:59 425.7429 430.8542 TRUE
# 4 2009-08-26 23:45:00 395.7708 402.2326 TRUE
# 5 2009-08-27 00:15:00 386.7583 393.6830 TRUE
# 6 2009-08-27 00:44:59 392.1159 398.3883 TRUE
You could replace the lapply command with
df[,2:3] <- lapply(df[,2:3], function(x)
ifelse(df[,'ToKeep'], x, NA))
df
# date value1 value2 ToKeep
#1 2009-08-27 01:15:00 NA NA FALSE
#2 2009-08-27 01:45:00 NA NA FALSE
#3 2009-08-27 02:14:59 425.7429 430.8542 TRUE
#4 2009-08-27 02:45:00 395.7708 402.2326 TRUE
#5 2009-08-27 03:15:00 386.7583 393.6830 TRUE
#6 2009-08-27 03:44:59 392.1159 398.3883 TRUE
Or instead of ifelse, you can use replace
df[,2:3] <- lapply(df[,2:3], function(x)
replace(x, !df[,'ToKeep'], NA ))

Resources