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

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

Related

Reorder dendrogram in R

I want to plot a heatmap where the x-axis is clustered by "normal" and "KIRP" from left to right.
Currently, my code clusters by dendrogram/similarity, and unfortunately I have one outlier "KIRP" on the left-most. I want to move this "KIRP" sample so that it appears after all the "normal" samples. Nevertheless, both groups "normal" and "KIRP" should still be clustered and arranged based on group similarity.
[![enter image description here][1]][1]
Code:
dge <- DGEList(counts=mat, group=group)
keep <- filterByExpr(dge)
v <- voom(mat, design, plot=TRUE)
vfit <- lmFit(v, design)
vfit <- contrasts.fit(vfit, contrasts=contrasts)
efit <- eBayes(vfit)
tfit <- treat(vfit, lfc=1)
dt <- decideTests(tfit)
de.common <- which(dt[,1]!=0)
kirp.vs.normal <- topTreat(tfit, coef=1, n=Inf)
topgenes <- rownames(kirp.vs.normal)[1:150]
i <- which(rownames(dge) %in% topgenes)
mycol <- colorpanel(1000,"#FFA500","white","#2E2787")
heatmap.2(lcpm[i,], scale="row",
labRow=rownames(dge)[i], labCol=group,
col=mycol, trace="none", density.info="none",
margin=c(8,1), lhei=c(2,10), dendrogram="column", main="Differential Gene Expression in\nNormal vs KIRP Type II CIMP samples", cex.main=0.3, lmat=rbind(c(0,3,4), c(2,1,0)), lwid=c(0.5,10,3))
Data:
dput(dge[1:50,1:50])
new("DGEList", .Data = list(structure(c(2L, 47L, 5L, 185L, 124L,
272L, 197L, 405L, 59L, 258L, 270L, 226L, 112L, 322L, 381L, 281L,
145L, 53L, 325L, 107L, 103L, 375L, 70L, 298L, 131L, 79L, 297L,
2L, 345L, 390L, 113L, 289L, 58L, 400L, 389L, 414L, 228L, 188L,
392L, 222L, 86L, 355L, 20L, 49L, 211L, 311L, 96L, 304L, 378L,
145L, 3L, 363L, 199L, 22L, 313L, 305L, 182L, 338L, 32L, 266L,
314L, 35L, 384L, 361L, 37L, 241L, 4L, 340L, 356L, 26L, 100L,
212L, 27L, 273L, 25L, 43L, 355L, 5L, 211L, 155L, 372L, 253L,
180L, 380L, 105L, 13L, 242L, 221L, 401L, 215L, 197L, 233L, 345L,
136L, 254L, 183L, 111L, 390L, 392L, 298L, 1L, 308L, 89L, 118L,
306L, 219L, 50L, 100L, 352L, 286L, 229L, 340L, 135L, 194L, 130L,
124L, 323L, 54L, 105L, 279L, 91L, 99L, 391L, 291L, 395L, 83L,
353L, 1L, 322L, 185L, 196L, 263L, 33L, 274L, 362L, 265L, 234L,
356L, 297L, 154L, 81L, 65L, 293L, 144L, 2L, 132L, 270L, 360L,
371L, 5L, 2L, 95L, 1L, 93L, 248L, 317L, 269L, 373L, 71L, 192L,
375L, 340L, 60L, 108L, 42L, 128L, 3L, 292L, 312L, 173L, 363L,
178L, 17L, 387L, 143L, 329L, 385L, 2L, 252L, 118L, 413L, 16L,
87L, 339L, 88L, 75L, 347L, 184L, 337L, 297L, 136L, 229L, 85L,
358L, 8L, 283L, 162L, 316L, 45L, 7L, 1L, 319L, 2L, 117L, 137L,
199L, 300L, 114L, 291L, 92L, 125L, 168L, 153L, 238L, 3L, 259L,
192L, 360L, 125L, 230L, 80L, 262L, 34L, 266L, 220L, 237L, 272L,
1L, 326L, 38L, 350L, 273L, 352L, 320L, 45L, 218L, 209L, 224L,
288L, 145L, 372L, 192L, 307L, 203L, 2L, 277L, 280L, 233L, 368L,
6L, 2L, 83L, 2L, 192L, 141L, 297L, 203L, 338L, 323L, 210L, 289L,
275L, 91L, 263L, 3L, 4L, 2L, 28L, 259L, 264L, 317L, 198L, 361L,
365L, 373L, 312L, 300L, 2L, 283L, 63L, 123L, 324L, 286L, 251L,
253L, 104L, 284L, 143L, 371L, 237L, 325L, 314L, 16L, 208L, 1L,
191L, 134L, 279L, 348L, 180L, 2L, 126L, 1L, 369L, 368L, 377L,
305L, 314L, 38L, 24L, 407L, 223L, 320L, 66L, 3L, 136L, 2L, 240L,
404L, 227L, 336L, 356L, 403L, 49L, 195L, 260L, 365L, 2L, 405L,
350L, 302L, 351L, 11L, 358L, 225L, 37L, 340L, 132L, 380L, 276L,
146L, 80L, 200L, 328L, 2L, 317L, 184L, 269L, 304L, 280L, 3L,
147L, 4L, 183L, 279L, 198L, 69L, 90L, 337L, 192L, 9L, 173L, 201L,
265L, 2L, 237L, 291L, 392L, 96L, 287L, 30L, 78L, 383L, 317L,
325L, 333L, 275L, 1L, 354L, 12L, 37L, 245L, 378L, 316L, 51L,
284L, 223L, 330L, 308L, 113L, 44L, 321L, 298L, 92L, 4L, 18L,
241L, 269L, 336L, 22L, 1L, 272L, 4L, 114L, 134L, 224L, 315L,
72L, 361L, 200L, 135L, 269L, 98L, 260L, 4L, 42L, 4L, 371L, 148L,
168L, 110L, 323L, 48L, 271L, 33L, 49L, 345L, 2L, 285L, 95L, 79L,
277L, 38L, 327L, 352L, 124L, 230L, 189L, 283L, 160L, 54L, 220L,
357L, 211L, 2L, 287L, 273L, 275L, 339L, 2L, 2L, 302L, 203L, 210L,
190L, 276L, 351L, 51L, 361L, 155L, 232L, 213L, 184L, 330L, 130L,
56L, 342L, 79L, 209L, 178L, 163L, 86L, 375L, 337L, 96L, 286L,
335L, 5L, 382L, 398L, 116L, 322L, 16L, 268L, 40L, 261L, 229L,
263L, 359L, 181L, 117L, 71L, 400L, 113L, 1L, 390L, 23L, 329L,
284L, 5L, 1L, 330L, 3L, 40L, 102L, 200L, 269L, 67L, 284L, 149L,
186L, 145L, 93L, 296L, 4L, 321L, 1L, 35L, 53L, 148L, 57L, 283L,
366L, 280L, 85L, 43L, 357L, 1L, 304L, 9L, 41L, 259L, 326L, 310L,
106L, 153L, 229L, 214L, 243L, 172L, 30L, 289L, 331L, 174L, 111L,
359L, 273L, 294L, 365L, 4L, 3L, 379L, 10L, 171L, 216L, 301L,
151L, 70L, 40L, 34L, 394L, 245L, 390L, 142L, 3L, 146L, 10L, 341L,
154L, 35L, 263L, 65L, 387L, 356L, 23L, 290L, 24L, 1L, 227L, 91L,
323L, 389L, 376L, 275L, 55L, 369L, 328L, 257L, 256L, 304L, 102L,
57L, 62L, 336L, 7L, 217L, 187L, 310L, 401L, 10L, 2L, 284L, 2L,
141L, 222L, 278L, 376L, 67L, 342L, 276L, 142L, 179L, 74L, 310L,
253L, 228L, 8L, 14L, 193L, 190L, 89L, 62L, 40L, 330L, 184L, 283L,
324L, 2L, 244L, 390L, 183L, 277L, 402L, 357L, 388L, 156L, 256L,
255L, 343L, 114L, 79L, 38L, 361L, 167L, 2L, 301L, 375L, 262L,
356L, 205L, 1L, 111L, 1L, 58L, 109L, 315L, 210L, 23L, 18L, 218L,
36L, 268L, 285L, 301L, 7L, 186L, 8L, 258L, 142L, 130L, 291L,
335L, 71L, 19L, 16L, 385L, 69L, 2L, 276L, 375L, 128L, 42L, 369L,
333L, 91L, 318L, 371L, 225L, 270L, 226L, 31L, 329L, 106L, 224L,
2L, 172L, 88L, 292L, 35L, 143L, 2L, 23L, 2L, 74L, 207L, 257L,
357L, 27L, 341L, 124L, 202L, 72L, 86L, 237L, 7L, 287L, 3L, 44L,
224L, 221L, 116L, 35L, 30L, 305L, 71L, 337L, 350L, 1L, 365L,
353L, 62L, 292L, 17L, 288L, 21L, 143L, 228L, 253L, 271L, 178L,
39L, 382L, 363L, 238L, 6L, 137L, 374L, 331L, 346L, 100L, 1L,
26L, 114L, 270L, 218L, 370L, 151L, 361L, 48L, 121L, 345L, 68L,
280L, 308L, 326L, 217L, 2L, 234L, 93L, 40L, 73L, 102L, 85L, 265L,
335L, 301L, 375L, 1L, 163L, 201L, 123L, 260L, 109L, 357L, 208L,
319L, 286L, 108L, 252L, 284L, 184L, 181L, 235L, 240L, 2L, 56L,
194L, 248L, 20L, 232L, 1L, 379L, 4L, 39L, 188L, 291L, 352L, 17L,
363L, 57L, 177L, 215L, 127L, 300L, 3L, 112L, 5L, 23L, 77L, 199L,
32L, 385L, 47L, 311L, 139L, 277L, 346L, 1L, 305L, 60L, 162L,
284L, 13L, 332L, 36L, 159L, 224L, 230L, 304L, 228L, 53L, 376L,
371L, 190L, 2L, 366L, 380L, 258L, 386L, 132L, 2L, 34L, 5L, 120L,
375L, 338L, 126L, 41L, 24L, 173L, 33L, 387L, 54L, 146L, 198L,
292L, 6L, 15L, 226L, 267L, 333L, 153L, 335L, 57L, 380L, 148L,
32L, 3L, 349L, 189L, 298L, 49L, 403L, 350L, 88L, 94L, 343L, 260L,
322L, 311L, 92L, 80L, 371L, 388L, 1L, 274L, 73L, 47L, 280L, 229L,
1L, 316L, 1L, 118L, 261L, 254L, 347L, 58L, 388L, 209L, 264L,
298L, 56L, 288L, 365L, 227L, 365L, 321L, 179L, 92L, 86L, 135L,
35L, 279L, 133L, 190L, 353L, 2L, 314L, 93L, 223L, 329L, 45L,
379L, 16L, 211L, 216L, 262L, 306L, 202L, 108L, 90L, 319L, 204L,
4L, 273L, 381L, 332L, 15L, 118L, 3L, 211L, 138L, 48L, 255L, 302L,
283L, 22L, 368L, 29L, 228L, 363L, 405L, 309L, 58L, 350L, 286L,
301L, 78L, 159L, 65L, 149L, 399L, 306L, 74L, 198L, 336L, 1L,
327L, 91L, 312L, 259L, 108L, 345L, 168L, 70L, 251L, 221L, 314L,
253L, 169L, 97L, 231L, 177L, 4L, 325L, 291L, 293L, 386L, 138L,
2L, 181L, 1L, 10L, 214L, 292L, 311L, 28L, 382L, 163L, 262L, 347L,
77L, 242L, 404L, 340L, 9L, 416L, 232L, 168L, 157L, 95L, 14L,
297L, 303L, 113L, 354L, 129L, 313L, 70L, 190L, 296L, 67L, 355L,
17L, 103L, 266L, 257L, 377L, 226L, 98L, 132L, 345L, 241L, 7L,
108L, 54L, 320L, 395L, 205L, 196L, 265L, 3L, 140L, 257L, 190L,
74L, 95L, 315L, 177L, 151L, 130L, 169L, 235L, 5L, 188L, 311L,
9L, 72L, 277L, 23L, 16L, 354L, 302L, 378L, 353L, 229L, 2L, 338L,
19L, 156L, 262L, 352L, 288L, 274L, 228L, 200L, 351L, 310L, 127L,
28L, 355L, 237L, 78L, 2L, 385L, 273L, 304L, 330L, 1L, 2L, 377L,
138L, 141L, 22L, 286L, 318L, 399L, 38L, 230L, 97L, 295L, 57L,
312L, 2L, 263L, 4L, 385L, 240L, 149L, 58L, 24L, 45L, 300L, 104L,
263L, 367L, 1L, 319L, 100L, 169L, 298L, 36L, 383L, 130L, 91L,
241L, 159L, 320L, 233L, 120L, 347L, 351L, 194L, 4L, 372L, 355L,
329L, 394L, 8L, 3L, 110L, 1L, 311L, 143L, 373L, 178L, 181L, 379L,
393L, 307L, 274L, 121L, 86L, 277L, 3L, 202L, 365L, 124L, 378L,
374L, 107L, 348L, 57L, 370L, 304L, 18L, 139L, 255L, 325L, 286L,
13L, 391L, 293L, 46L, 271L, 345L, 116L, 372L, 242L, 213L, 248L,
149L, 295L, 4L, 106L, 263L, 303L, 279L, 119L, 2L, 262L, 2L, 242L,
373L, 304L, 246L, 216L, 18L, 265L, 255L, 199L, 30L, 291L, 6L,
357L, 133L, 359L, 168L, 266L, 80L, 114L, 352L, 15L, 157L, 389L,
61L, 40L, 60L, 273L, 91L, 130L, 156L, 329L, 387L, 292L, 397L,
403L, 323L, 301L, 215L, 332L, 177L, 375L, 9L, 161L, 181L, 330L,
335L, 254L, 2L, 178L, 120L, 70L, 49L, 352L, 287L, 351L, 368L,
214L, 389L, 221L, 44L, 60L, 2L, 121L, 272L, 363L, 36L, 388L,
284L, 63L, 38L, 57L, 47L, 263L, 342L, 237L, 312L, 300L, 228L,
22L, 373L, 201L, 116L, 77L, 347L, 168L, 318L, 257L, 107L, 27L,
61L, 326L, 5L, 390L, 229L, 349L, 279L, 120L, 1L, 205L, 6L, 108L,
141L, 129L, 47L, 83L, 327L, 23L, 54L, 184L, 181L, 249L, 188L,
143L, 188L, 102L, 107L, 228L, 35L, 82L, 343L, 290L, 12L, 311L,
232L, 1L, 339L, 53L, 159L, 212L, 346L, 239L, 316L, 305L, 158L,
313L, 280L, 139L, 40L, 68L, 320L, 84L, 4L, 143L, 276L, 275L,
310L, 2L, 1L, 147L, 1L, 339L, 67L, 301L, 117L, 329L, 347L, 372L,
276L, 258L, 24L, 310L, 1L, 5L, 1L, 335L, 368L, 47L, 74L, 209L,
262L, 83L, 371L, 28L, 341L, 1L, 305L, 31L, 9L, 357L, 325L, 259L,
30L, 313L, 344L, 216L, 311L, 210L, 100L, 48L, 234L, 330L, 6L,
19L, 285L, 350L, 275L, 180L, 8L, 128L, 2L, 120L, 162L, 266L,
309L, 35L, 375L, 83L, 192L, 258L, 147L, 202L, 142L, 41L, 2L,
313L, 9L, 197L, 126L, 75L, 368L, 323L, 14L, 199L, 63L, 5L, 380L,
91L, 114L, 311L, 29L, 296L, 301L, 203L, 276L, 246L, 335L, 182L,
45L, 11L, 387L, 215L, 1L, 15L, 383L, 364L, 332L, 7L, 2L, 132L,
2L, 116L, 130L, 218L, 321L, 47L, 351L, 264L, 192L, 171L, 126L,
227L, 2L, 363L, 229L, 379L, 210L, 188L, 154L, 285L, 11L, 314L,
338L, 177L, 323L, 2L, 362L, 382L, 49L, 237L, 375L, 310L, 244L,
185L, 254L, 241L, 346L, 111L, 27L, 230L, 383L, 175L, 2L, 232L,
386L, 284L, 287L, 1L, 1L, 347L, 9L, 95L, 285L, 339L, 245L, 397L,
91L, 229L, 298L, 26L, 396L, 385L, 401L, 24L, 8L, 347L, 264L,
122L, 146L, 128L, 28L, 306L, 93L, 390L, 415L, 2L, 300L, 207L,
262L, 287L, 127L, 51L, 230L, 43L, 297L, 194L, 348L, 209L, 213L,
193L, 365L, 165L, 3L, 69L, 17L, 342L, 382L, 131L, 2L, 328L, 128L,
109L, 207L, 199L, 305L, 43L, 357L, 269L, 207L, 209L, 162L, 251L,
4L, 195L, 190L, 104L, 275L, 153L, 117L, 302L, 51L, 307L, 69L,
358L, 341L, 4L, 283L, 359L, 46L, 238L, 12L, 293L, 312L, 212L,
215L, 271L, 348L, 136L, 15L, 353L, 52L, 174L, 190L, 18L, 123L,
321L, 335L, 1L, 2L, 301L, 5L, 365L, 226L, 285L, 206L, 28L, 395L,
297L, 245L, 13L, 75L, 281L, 32L, 324L, 7L, 393L, 94L, 181L, 125L,
153L, 368L, 268L, 10L, 191L, 369L, 1L, 326L, 180L, 188L, 314L,
58L, 375L, 53L, 122L, 236L, 235L, 265L, 207L, 156L, 165L, 313L,
171L, 5L, 385L, 256L, 276L, 29L, 129L, 1L, 216L, 128L, 72L, 117L,
233L, 287L, 378L, 353L, 204L, 80L, 257L, 65L, 315L, 190L, 201L,
227L, 12L, 198L, 146L, 103L, 15L, 48L, 280L, 155L, 308L, 359L,
1L, 253L, 144L, 19L, 288L, 49L, 329L, 119L, 113L, 200L, 151L,
284L, 196L, 102L, 295L, 358L, 240L, 128L, 241L, 281L, 264L, 349L,
3L, 2L, 20L, 3L, 131L, 144L, 180L, 356L, 57L, 331L, 232L, 175L,
199L, 114L, 215L, 191L, 74L, 5L, 88L, 144L, 202L, 135L, 355L,
30L, 283L, 291L, 98L, 350L, 1L, 371L, 26L, 251L, 288L, 398L,
270L, 353L, 93L, 237L, 225L, 333L, 174L, 45L, 8L, 391L, 197L,
6L, 65L, 330L, 346L, 367L, 122L, 1L, 244L, 2L, 95L, 191L, 166L,
360L, 63L, 305L, 118L, 182L, 172L, 110L, 236L, 6L, 17L, 6L, 365L,
237L, 173L, 52L, 322L, 23L, 290L, 373L, 354L, 307L, 1L, 340L,
47L, 97L, 273L, 329L, 284L, 316L, 203L, 219L, 253L, 315L, 142L,
377L, 325L, 321L, 274L, 2L, 40L, 336L, 321L, 351L, 1L, 4L, 236L,
5L, 278L, 195L, 243L, 348L, 40L, 9L, 230L, 215L, 364L, 39L, 196L,
58L, 332L, 114L, 297L, 130L, 165L, 65L, 151L, 75L, 288L, 322L,
200L, 380L, 4L, 227L, 198L, 217L, 233L, 111L, 276L, 293L, 167L,
211L, 303L, 287L, 160L, 128L, 177L, 308L, 147L, 1L, 136L, 376L,
274L, 335L, 176L, 1L, 397L, 1L, 46L, 137L, 315L, 271L, 373L,
393L, 323L, 196L, 232L, 351L, 130L, 207L, 174L, 1L, 197L, 229L,
123L, 228L, 368L, 36L, 383L, 140L, 322L, 342L, 2L, 200L, 345L,
336L, 294L, 249L, 227L, 276L, 88L, 307L, 119L, 390L, 234L, 70L,
46L, 64L, 241L, 205L, 132L, 208L, 270L, 343L, 13L, 1L, 221L,
1L, 168L, 38L, 266L, 333L, 272L, 358L, 335L, 79L, 241L, 110L,
33L, 1L, 1L, 2L, 41L, 295L, 165L, 287L, 368L, 332L, 337L, 357L,
30L, 284L, 117L, 324L, 356L, 162L, 309L, 270L, 306L, 49L, 7L,
282L, 155L, 12L, 194L, 68L, 259L, 208L, 85L, 1L, 214L, 63L, 252L,
305L, 117L, 1L, 398L, 131L, 399L, 221L, 323L, 142L, 373L, 397L,
262L, 277L, 389L, 315L, 324L, 98L, 393L, 8L, 190L, 285L, 58L,
56L, 194L, 87L, 340L, 192L, 239L, 382L, 1L, 246L, 400L, 254L,
269L, 161L, 314L, 148L, 146L, 276L, 200L, 334L, 284L, 185L, 147L,
341L, 140L, 203L, 280L, 16L, 330L, 392L, 325L, 6L, 271L, 6L,
75L, 241L, 205L, 14L, 108L, 363L, 190L, 148L, 269L, 169L, 226L,
6L, 34L, 137L, 398L, 234L, 276L, 104L, 381L, 355L, 341L, 12L,
322L, 325L, 1L, 360L, 123L, 224L, 291L, 400L, 301L, 285L, 245L,
232L, 359L, 344L, 145L, 64L, 369L, 272L, 194L, 4L, 26L, 326L,
309L, 323L, 6L, 1L, 376L, 2L, 102L, 109L, 202L, 298L, 72L, 366L,
230L, 213L, 221L, 84L, 242L, 2L, 339L, 8L, 42L, 247L, 204L, 111L,
28L, 17L, 281L, 375L, 150L, 337L, 1L, 349L, 24L, 229L, 262L,
378L, 277L, 358L, 170L, 254L, 261L, 285L, 205L, 48L, 10L, 333L,
198L, 2L, 324L, 309L, 355L, 359L, 4L, 1L, 55L, 4L, 91L, 386L,
289L, 45L, 13L, 347L, 263L, 370L, 303L, 384L, 127L, 2L, 131L,
6L, 353L, 165L, 333L, 271L, 295L, 105L, 18L, 109L, 20L, 14L,
1L, 305L, 30L, 380L, 397L, 12L, 293L, 385L, 213L, 357L, 201L,
312L, 282L, 23L, 318L, 72L, 301L, 4L, 146L, 96L, 302L, 379L,
2L, 2L, 379L, 2L, 188L, 174L, 223L, 304L, 388L, 392L, 312L, 278L,
182L, 144L, 334L, 5L, 283L, 195L, 59L, 310L, 179L, 192L, 30L,
316L, 335L, 68L, 387L, 332L, 3L, 374L, 402L, 143L, 305L, 20L,
358L, 21L, 208L, 239L, 214L, 407L, 133L, 97L, 326L, 42L, 256L,
123L, 63L, 45L, 395L, 276L, 124L, 1L, 282L, 204L, 21L, 248L,
311L, 74L, 62L, 416L, 347L, 278L, 276L, 69L, 385L, 20L, 297L,
6L, 324L, 176L, 188L, 127L, 108L, 331L, 307L, 214L, 93L, 390L,
2L, 300L, 80L, 349L, 344L, 109L, 398L, 339L, 173L, 244L, 323L,
272L, 259L, 185L, 172L, 366L, 226L, 129L, 35L, 303L, 404L, 391L,
206L, 2L, 346L, 4L, 109L, 30L, 218L, 171L, 17L, 332L, 240L, 239L,
384L, 102L, 73L, 2L, 290L, 245L, 350L, 221L, 39L, 214L, 233L,
74L, 378L, 329L, 286L, 359L, 4L, 315L, 115L, 353L, 306L, 328L,
226L, 305L, 255L, 297L, 167L, 356L, 228L, 392L, 244L, 398L, 210L,
1L, 170L, 92L, 264L, 300L, 8L, 4L, 197L, 2L, 7L, 231L, 352L,
118L, 320L, 24L, 176L, 252L, 392L, 13L, 133L, 3L, 209L, 258L,
121L, 186L, 204L, 80L, 168L, 379L, 374L, 335L, 271L, 39L, 279L,
44L, 148L, 262L, 95L, 99L, 388L, 37L, 309L, 365L, 136L, 333L,
291L, 164L, 215L, 27L, 343L, 3L, 115L, 233L, 368L, 342L, 51L,
4L, 329L, 2L, 14L, 37L, 301L, 125L, 31L, 315L, 389L, 34L, 218L,
38L, 312L, 11L, 4L, 227L, 271L, 228L, 102L, 357L, 208L, 316L,
47L, 20L, 369L, 45L, 4L, 390L, 302L, 94L, 19L, 362L, 251L, 394L,
335L, 32L, 258L, 325L, 256L, 73L, 52L, 110L, 288L, 8L, 58L, 113L,
353L, 320L, 136L, 2L, 230L, 2L, 42L, 123L, 327L, 222L, 354L,
338L, 345L, 236L, 128L, 102L, 41L, 135L, 7L, 3L, 31L, 293L, 347L,
328L, 296L, 16L, 47L, 385L, 86L, 294L, 2L, 19L, 246L, 164L, 371L,
317L, 297L, 100L, 346L, 352L, 206L, 379L, 252L, 40L, 363L, 54L,
186L, 1L, 339L, 139L, 274L, 391L, 194L, 1L, 287L, 1L, 128L, 53L,
268L, 180L, 343L, 13L, 213L, 377L, 270L, 80L, 204L, 119L, 118L,
118L, 10L, 103L, 375L, 202L, 21L, 63L, 316L, 393L, 342L, 344L,
1L, 353L, 93L, 86L, 324L, 327L, 336L, 319L, 230L, 308L, 167L,
355L, 219L, 43L, 32L, 389L, 199L, 3L, 96L, 386L, 290L, 388L,
6L), dim = c(50L, 50L), dimnames = list(c("A2ML1", "ABCA4", "ABCB5",
"ABHD1", "ACRBP", "ACSL5", "ACSM5", "ACSS3", "ACVRL1", "ADH1C",
"ADRB2", "AEBP1", "AFMID", "AIF1", "AIM2", "AKR1B10", "AKR1C4",
"AKR7L", "ALDH3B2", "ALDH8A1", "ALDOC", "ALOX5AP", "ALPK3", "AMFR",
"ANKRD22", "ANKRD2", "ANKRD45", "ANXA8L2", "ANXA9", "AOC3", "APBB1IP",
"APH1B", "APOBEC3C", "APOL3", "APOL4", "APOM", "APP", "AQP1",
"ARFRP1", "ARHGAP29", "ARHGDIB", "ARL11", "ARL4D", "ARRDC3",
"ASCL3", "B3GNT3", "B3GNT8", "BAMBI", "BAZ2B", "BCL2L14"), c("TCGA.BQ.7051.11A",
"TCGA.DZ.6132.11A", "TCGA.CZ.4864.11A", "TCGA.KN.8426.11A", "TCGA.CZ.5982.11A",
"TCGA.A4.A4ZT.11A", "TCGA.CZ.5468.11A", "TCGA.BQ.5894.11A", "TCGA.B0.5699.11A",
"TCGA.KL.8339.11A", "TCGA.CZ.5988.11A", "TCGA.CZ.5461.11A", "TCGA.CJ.6030.11A",
"TCGA.B8.5549.11A", "TCGA.CW.5587.11A", "TCGA.CZ.5987.11A", "TCGA.CJ.5677.11A",
"TCGA.CZ.5470.11A", "TCGA.B2.5636.11A", "TCGA.CJ.5676.11A", "TCGA.KN.8435.11A",
"TCGA.BQ.5877.11A", "TCGA.CZ.5984.11A", "TCGA.CZ.5457.11A", "TCGA.CZ.4863.11A",
"TCGA.CZ.5467.11A", "TCGA.A3.3387.11A", "TCGA.CZ.5456.11A", "TCGA.B9.4115.11A",
"TCGA.GL.6846.11A", "TCGA.B0.5402.11A", "TCGA.DZ.6133.11A", "TCGA.B0.5691.11A",
"TCGA.B0.4700.11A", "TCGA.B0.5696.11A", "TCGA.CW.5581.11A", "TCGA.BQ.7045.11A",
"TCGA.KN.8427.11A", "TCGA.GL.A59R.11A", "TCGA.CW.5584.11A", "TCGA.BQ.5878.11A",
"TCGA.CW.5589.11A", "TCGA.CJ.5672.11A", "TCGA.BQ.7044.11A", "TCGA.CZ.5466.11A",
"TCGA.BQ.5887.11A", "TCGA.CZ.4865.11A", "TCGA.CZ.5458.11A", "TCGA.Y8.A8RY.11A",
"TCGA.KN.8422.11A"))), structure(list(group = 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), levels = "normal", class = "factor"), lib.size = c(94225,
93733, 87671, 94478, 81956, 81966, 91604, 87469, 81048, 91004,
81264, 92424, 91496, 85877, 87734, 83846, 88254, 92553, 89254,
91736, 96220, 84907, 90231, 87189, 90384, 87166, 81495, 81436,
90285, 83664, 95495, 84763, 91291, 85265, 87117, 81741, 88278,
92099, 81840, 90942, 88312, 89034, 89739, 90942, 95178, 88887,
91263, 85695, 87717, 90705), norm.factors = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1)), row.names = c("TCGA.BQ.7051.11A", "TCGA.DZ.6132.11A",
"TCGA.CZ.4864.11A", "TCGA.KN.8426.11A", "TCGA.CZ.5982.11A", "TCGA.A4.A4ZT.11A",
"TCGA.CZ.5468.11A", "TCGA.BQ.5894.11A", "TCGA.B0.5699.11A", "TCGA.KL.8339.11A",
"TCGA.CZ.5988.11A", "TCGA.CZ.5461.11A", "TCGA.CJ.6030.11A", "TCGA.B8.5549.11A",
"TCGA.CW.5587.11A", "TCGA.CZ.5987.11A", "TCGA.CJ.5677.11A", "TCGA.CZ.5470.11A",
"TCGA.B2.5636.11A", "TCGA.CJ.5676.11A", "TCGA.KN.8435.11A", "TCGA.BQ.5877.11A",
"TCGA.CZ.5984.11A", "TCGA.CZ.5457.11A", "TCGA.CZ.4863.11A", "TCGA.CZ.5467.11A",
"TCGA.A3.3387.11A", "TCGA.CZ.5456.11A", "TCGA.B9.4115.11A", "TCGA.GL.6846.11A",
"TCGA.B0.5402.11A", "TCGA.DZ.6133.11A", "TCGA.B0.5691.11A", "TCGA.B0.4700.11A",
"TCGA.B0.5696.11A", "TCGA.CW.5581.11A", "TCGA.BQ.7045.11A", "TCGA.KN.8427.11A",
"TCGA.GL.A59R.11A", "TCGA.CW.5584.11A", "TCGA.BQ.5878.11A", "TCGA.CW.5589.11A",
"TCGA.CJ.5672.11A", "TCGA.BQ.7044.11A", "TCGA.CZ.5466.11A", "TCGA.BQ.5887.11A",
"TCGA.CZ.4865.11A", "TCGA.CZ.5458.11A", "TCGA.Y8.A8RY.11A", "TCGA.KN.8422.11A"
), class = "data.frame")))
I used some simulated data to try this, since it seems that the question can be generalized to other datasets. I also ran into errors with your subset of data.
set.seed(123)
data <- as.matrix(data.frame(
a1 = rnorm(100, 0, 1),
a2 = rnorm(100, 0, 1),
a3 = rnorm(100, 0, 1),
b1 = rnorm(100, 0, 1.2),
b2 = rnorm(100, 0, 1.2),
b3 = rnorm(100, 0, 1.2)
))
Here we see that b1 would look nicer next to b3 and b2.
library(gplots)
ht <- heatmap.2(
x = data,
ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
col = colorpanel(1000,"#FFA500","white","#2E2787"),
trace = "none",
key = FALSE,
dendrogram = "column",
main = "Example Data",
labRow = FALSE
)
Looking at the structure of the heatmap output, the dendrogram is stored as a "dendrogram" class object, which can be manipulated with with the reorder() generic.
The documentation doesn't reveal too much, but the second argument wts describes arbitrary weights to determine a reordered dendrogram. From what I can tell, large values generally get placed to the right. From trial-and-error, it appears that supplying weights in the original order of the columns worked out. This essentially flips branches without affecting the distance metrics.
colden <- ht$colDendrogram
colden_reordered <- reorder(colden, c(10, 1, 1, 100, 300, 200))
plot(colden, main = "original dendrogram")
plot(colden_reordered, main = "modified dendrogram")
When can then plot the heatmap with the new dendrogram using the Colv option.
ht2 <- heatmap.2(
x = data,
ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
col = colorpanel(1000,"#FFA500","white","#2E2787"),
trace = "none",
key = FALSE,
dendrogram = "column",
Colv = colden_reordered,
main = "Reordered manually",
labRow = FALSE
)
The dendsort package may be a better go-to tool for sorting dendrograms. In short, the dendsort() moves clusters with smaller average distances to the left. Using this alone seems to solve the issue. With larger heatmaps, the benefit may be apparent when looking for patterns in the data. Seems preferable to manual reordering, when possible. Below I've modified the option Colv to use this function.
library(dendsort)
ht3 <- heatmap.2(
x = data,
ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
col = colorpanel(1000,"#FFA500","white","#2E2787"),
trace = "none",
density.info = "none",
key = FALSE,
dendrogram = "column",
Colv = dendsort(colden),
main = "Reordered w/ dendsort()",
labRow = FALSE
)
For more options for heatmaps concerning clustering and groups, I use the ComplexHeatmap package, which has wonderful documentation. They have many options for splitting columns; for instance, slicing up the columns into groups first, and then clustering within those slices. See this section 2.7: Chapter 2 A Single Heatmap | ComplexHeatmap Complete Reference.

Solve minimization question in R using dynamic program

I am currently trying to find a solution to a transportation problem. I have a table with the travel times from 30 customers to each other. The goal is to use dynamic program to go from this table with direct durations to a table with shortest duration possible (via possible subtours between pairs of customers)
I have no clue how to solve this. The question states "Let 𝐷𝑖,𝑗,𝑘 be the shortest (in duration) path between customer 𝑖 and 𝑗, using only nodes 0 to 𝑘 as intermediaries."
Is there someone who can help me with this?
Thank you in advance. :-)
to use only nodes 0 to k as intermediates.
structure(list(ID = 1:30, X1 = c(0L, 98L, 132L, 245L, 17L, 69L,
139L, 112L, 207L, 35L, 249L, 43L, 215L, 62L, 152L, 237L, 59L,
119L, 45L, 59L, 23L, 12L, 16L, 66L, 177L, 31L, 118L, 165L, 117L,
193L), X2 = c(37L, 0L, 74L, 176L, 91L, 111L, 143L, 208L, 202L,
40L, 172L, 101L, 163L, 51L, 220L, 180L, 48L, 98L, 45L, 62L, 91L,
9L, 50L, 145L, 198L, 48L, 3L, 163L, 159L, 176L), X3 = c(118L,
10L, 0L, 138L, 108L, 115L, 221L, 274L, 231L, 89L, 197L, 36L,
175L, 93L, 174L, 184L, 13L, 109L, 109L, 22L, 172L, 91L, 12L,
149L, 140L, 108L, 8L, 133L, 168L, 139L), X4 = c(252L, 137L, 112L,
0L, 248L, 276L, 373L, 350L, 353L, 137L, 31L, 186L, 49L, 170L,
325L, 81L, 117L, 184L, 175L, 185L, 279L, 182L, 120L, 205L, 130L,
132L, 133L, 226L, 158L, 67L), X5 = c(40L, 93L, 118L, 188L, 0L,
38L, 147L, 106L, 208L, 46L, 228L, 26L, 208L, 54L, 163L, 212L,
81L, 49L, 45L, 84L, 12L, 82L, 114L, 81L, 173L, 67L, 52L, 63L,
146L, 156L), X6 = c(54L, 117L, 143L, 296L, 74L, 0L, 78L, 97L,
218L, 52L, 239L, 47L, 267L, 111L, 216L, 269L, 167L, 91L, 105L,
99L, 16L, 117L, 95L, 15L, 264L, 136L, 148L, 100L, 145L, 242L),
X7 = c(143L, 145L, 208L, 302L, 130L, 86L, 0L, 84L, 308L,
151L, 348L, 102L, 305L, 139L, 254L, 272L, 156L, 182L, 87L,
214L, 110L, 82L, 145L, 59L, 283L, 98L, 171L, 192L, 211L,
228L), X8 = c(142L, 195L, 238L, 336L, 161L, 80L, 2L, 0L,
289L, 135L, 342L, 134L, 307L, 169L, 197L, 347L, 261L, 143L,
128L, 243L, 52L, 128L, 169L, 97L, 318L, 214L, 229L, 225L,
287L, 288L), X9 = c(230L, 238L, 217L, 264L, 229L, 154L, 299L,
269L, 0L, 210L, 335L, 204L, 342L, 260L, 25L, 258L, 264L,
76L, 170L, 222L, 126L, 193L, 272L, 203L, 218L, 186L, 214L,
120L, 240L, 207L), X10 = c(2L, 30L, 34L, 186L, 75L, 96L,
161L, 179L, 244L, 0L, 199L, 17L, 181L, 59L, 240L, 178L, 101L,
117L, 1L, 48L, 79L, 72L, 24L, 60L, 236L, 5L, 32L, 151L, 203L,
133L), X11 = c(243L, 191L, 114L, 38L, 164L, 211L, 295L, 370L,
350L, 211L, 0L, 174L, 14L, 166L, 278L, 164L, 165L, 223L,
252L, 129L, 201L, 196L, 201L, 271L, 98L, 199L, 209L, 219L,
168L, 62L), X12 = c(22L, 14L, 64L, 164L, 68L, 113L, 117L,
115L, 208L, 8L, 239L, 0L, 204L, 36L, 230L, 217L, 37L, 62L,
41L, 30L, 64L, 25L, 22L, 60L, 150L, 30L, 90L, 109L, 92L,
178L), X13 = c(224L, 168L, 120L, 23L, 213L, 310L, 324L, 377L,
366L, 212L, 54L, 178L, 0L, 207L, 316L, 105L, 117L, 266L,
235L, 106L, 231L, 186L, 147L, 223L, 109L, 182L, 211L, 183L,
130L, 132L), X14 = c(66L, 22L, 77L, 204L, 23L, 143L, 93L,
171L, 259L, 37L, 182L, 68L, 218L, 0L, 216L, 186L, 56L, 139L,
65L, 44L, 118L, 64L, 19L, 112L, 183L, 14L, 40L, 142L, 96L,
131L), X15 = c(167L, 236L, 263L, 275L, 187L, 189L, 212L,
282L, 9L, 181L, 309L, 231L, 340L, 238L, 0L, 207L, 216L, 97L,
204L, 237L, 120L, 160L, 217L, 188L, 258L, 203L, 183L, 106L,
174L, 267L), X16 = c(173L, 196L, 161L, 152L, 168L, 268L,
322L, 280L, 265L, 230L, 133L, 160L, 102L, 209L, 193L, 0L,
162L, 168L, 228L, 208L, 283L, 159L, 195L, 270L, 14L, 233L,
176L, 172L, 48L, 38L), X17 = c(70L, 44L, 13L, 184L, 90L,
186L, 194L, 267L, 257L, 72L, 179L, 38L, 107L, 88L, 177L,
172L, 0L, 93L, 71L, 14L, 167L, 117L, 37L, 146L, 116L, 35L,
62L, 83L, 93L, 129L), X18 = c(115L, 128L, 153L, 196L, 78L,
109L, 147L, 185L, 103L, 138L, 218L, 123L, 200L, 85L, 91L,
178L, 86L, 0L, 35L, 125L, 67L, 68L, 142L, 42L, 202L, 84L,
81L, 91L, 87L, 210L), X19 = c(36L, 104L, 71L, 166L, 16L,
99L, 88L, 133L, 200L, 35L, 246L, 2L, 263L, 89L, 144L, 229L,
47L, 103L, 0L, 31L, 114L, 32L, 44L, 106L, 219L, 43L, 92L,
119L, 150L, 103L), X20 = c(74L, 66L, 22L, 101L, 76L, 101L,
194L, 211L, 170L, 21L, 179L, 105L, 197L, 36L, 180L, 204L,
48L, 71L, 59L, 0L, 178L, 32L, 58L, 125L, 180L, 46L, 16L,
77L, 130L, 75L), X21 = c(85L, 158L, 144L, 210L, 29L, 16L,
44L, 65L, 141L, 88L, 258L, 70L, 279L, 124L, 168L, 190L, 145L,
45L, 40L, 110L, 0L, 129L, 157L, 52L, 177L, 117L, 175L, 153L,
133L, 190L), X22 = c(25L, 44L, 124L, 236L, 62L, 39L, 108L,
149L, 175L, 18L, 177L, 16L, 259L, 44L, 163L, 179L, 74L, 110L,
28L, 104L, 110L, 0L, 97L, 123L, 216L, 5L, 112L, 170L, 153L,
196L), X23 = c(26L, 2L, 39L, 205L, 102L, 74L, 205L, 203L,
257L, 49L, 194L, 96L, 207L, 5L, 174L, 160L, 37L, 159L, 55L,
55L, 91L, 1L, 0L, 153L, 216L, 52L, 11L, 192L, 116L, 149L),
X24 = c(12L, 127L, 115L, 288L, 90L, 60L, 73L, 79L, 194L,
87L, 203L, 81L, 257L, 108L, 188L, 263L, 185L, 90L, 25L, 177L,
51L, 85L, 93L, 0L, 234L, 114L, 111L, 72L, 131L, 153L), X25 = c(194L,
120L, 191L, 143L, 212L, 253L, 281L, 318L, 291L, 160L, 108L,
174L, 94L, 188L, 203L, 43L, 127L, 153L, 204L, 162L, 170L,
237L, 194L, 251L, 0L, 237L, 122L, 149L, 75L, 104L), X26 = c(10L,
58L, 22L, 171L, 101L, 131L, 144L, 154L, 177L, 5L, 222L, 48L,
220L, 59L, 192L, 193L, 9L, 134L, 23L, 80L, 75L, 19L, 19L,
132L, 197L, 0L, 57L, 146L, 163L, 154L), X27 = c(104L, 58L,
15L, 175L, 46L, 178L, 206L, 185L, 199L, 44L, 191L, 115L,
211L, 33L, 200L, 148L, 25L, 104L, 47L, 8L, 154L, 35L, 41L,
106L, 159L, 14L, 0L, 171L, 88L, 87L), X28 = c(80L, 137L,
93L, 225L, 76L, 138L, 245L, 195L, 102L, 130L, 197L, 112L,
169L, 163L, 68L, 83L, 108L, 50L, 125L, 108L, 139L, 106L,
131L, 111L, 136L, 179L, 150L, 0L, 103L, 151L), X29 = c(170L,
113L, 89L, 120L, 138L, 172L, 224L, 263L, 233L, 155L, 170L,
132L, 194L, 133L, 160L, 99L, 106L, 86L, 154L, 90L, 132L,
158L, 142L, 209L, 55L, 127L, 118L, 93L, 0L, 67L), X30 = c(197L,
170L, 155L, 66L, 178L, 193L, 226L, 303L, 213L, 161L, 104L,
94L, 140L, 138L, 241L, 29L, 102L, 131L, 108L, 129L, 182L,
167L, 167L, 198L, 83L, 154L, 89L, 106L, 87L, 0L)), class = "data.frame", row.names = c(NA,
-30L))
Following up on my comment to use the igraph package, below is a way to accomplish what is described.
library(igraph)
m <- data.frame(X1 = c(0L, 98L, 132L, 245L, 17L, 69L, 139L, 112L, 207L, 35L, 249L, 43L, 215L, 62L, 152L, 237L, 59L, 119L, 45L, 59L, 23L, 12L, 16L, 66L, 177L, 31L, 118L, 165L, 117L, 193L),
X2 = c(37L, 0L, 74L, 176L, 91L, 111L, 143L, 208L, 202L, 40L, 172L, 101L, 163L, 51L, 220L, 180L, 48L, 98L, 45L, 62L, 91L, 9L, 50L, 145L, 198L, 48L, 3L, 163L, 159L, 176L),
X3 = c(118L, 10L, 0L, 138L, 108L, 115L, 221L, 274L, 231L, 89L, 197L, 36L, 175L, 93L, 174L, 184L, 13L, 109L, 109L, 22L, 172L, 91L, 12L, 149L, 140L, 108L, 8L, 133L, 168L, 139L),
X4 = c(252L, 137L, 112L, 0L, 248L, 276L, 373L, 350L, 353L, 137L, 31L, 186L, 49L, 170L, 325L, 81L, 117L, 184L, 175L, 185L, 279L, 182L, 120L, 205L, 130L, 132L, 133L, 226L, 158L, 67L),
X5 = c(40L, 93L, 118L, 188L, 0L, 38L, 147L, 106L, 208L, 46L, 228L, 26L, 208L, 54L, 163L, 212L, 81L, 49L, 45L, 84L, 12L, 82L, 114L, 81L, 173L, 67L, 52L, 63L, 146L, 156L),
X6 = c(54L, 117L, 143L, 296L, 74L, 0L, 78L, 97L, 218L, 52L, 239L, 47L, 267L, 111L, 216L, 269L, 167L, 91L, 105L, 99L, 16L, 117L, 95L, 15L, 264L, 136L, 148L, 100L, 145L, 242L),
X7 = c(143L, 145L, 208L, 302L, 130L, 86L, 0L, 84L, 308L, 151L, 348L, 102L, 305L, 139L, 254L, 272L, 156L, 182L, 87L, 214L, 110L, 82L, 145L, 59L, 283L, 98L, 171L, 192L, 211L, 228L),
X8 = c(142L, 195L, 238L, 336L, 161L, 80L, 2L, 0L, 289L, 135L, 342L, 134L, 307L, 169L, 197L, 347L, 261L, 143L, 128L, 243L, 52L, 128L, 169L, 97L, 318L, 214L, 229L, 225L, 287L, 288L),
X9 = c(230L, 238L, 217L, 264L, 229L, 154L, 299L, 269L, 0L, 210L, 335L, 204L, 342L, 260L, 25L, 258L, 264L, 76L, 170L, 222L, 126L, 193L, 272L, 203L, 218L, 186L, 214L, 120L, 240L, 207L),
X10 = c(2L, 30L, 34L, 186L, 75L, 96L, 161L, 179L, 244L, 0L, 199L, 17L, 181L, 59L, 240L, 178L, 101L, 117L, 1L, 48L, 79L, 72L, 24L, 60L, 236L, 5L, 32L, 151L, 203L, 133L),
X11 = c(243L, 191L, 114L, 38L, 164L, 211L, 295L, 370L, 350L, 211L, 0L, 174L, 14L, 166L, 278L, 164L, 165L, 223L, 252L, 129L, 201L, 196L, 201L, 271L, 98L, 199L, 209L, 219L, 168L, 62L),
X12 = c(22L, 14L, 64L, 164L, 68L, 113L, 117L, 115L, 208L, 8L, 239L, 0L, 204L, 36L, 230L, 217L, 37L, 62L, 41L, 30L, 64L, 25L, 22L, 60L, 150L, 30L, 90L, 109L, 92L, 178L),
X13 = c(224L, 168L, 120L, 23L, 213L, 310L, 324L, 377L, 366L, 212L, 54L, 178L, 0L, 207L, 316L, 105L, 117L, 266L, 235L, 106L, 231L, 186L, 147L, 223L, 109L, 182L, 211L, 183L, 130L, 132L),
X14 = c(66L, 22L, 77L, 204L, 23L, 143L, 93L, 171L, 259L, 37L, 182L, 68L, 218L, 0L, 216L, 186L, 56L, 139L, 65L, 44L, 118L, 64L, 19L, 112L, 183L, 14L, 40L, 142L, 96L, 131L),
X15 = c(167L, 236L, 263L, 275L, 187L, 189L, 212L, 282L, 9L, 181L, 309L, 231L, 340L, 238L, 0L, 207L, 216L, 97L, 204L, 237L, 120L, 160L, 217L, 188L, 258L, 203L, 183L, 106L, 174L, 267L),
X16 = c(173L, 196L, 161L, 152L, 168L, 268L, 322L, 280L, 265L, 230L, 133L, 160L, 102L, 209L, 193L, 0L, 162L, 168L, 228L, 208L, 283L, 159L, 195L, 270L, 14L, 233L, 176L, 172L, 48L, 38L),
X17 = c(70L, 44L, 13L, 184L, 90L, 186L, 194L, 267L, 257L, 72L, 179L, 38L, 107L, 88L, 177L, 172L, 0L, 93L, 71L, 14L, 167L, 117L, 37L, 146L, 116L, 35L, 62L, 83L, 93L, 129L),
X18 = c(115L, 128L, 153L, 196L, 78L, 109L, 147L, 185L, 103L, 138L, 218L, 123L, 200L, 85L, 91L, 178L, 86L, 0L, 35L, 125L, 67L, 68L, 142L, 42L, 202L, 84L, 81L, 91L, 87L, 210L),
X19 = c(36L, 104L, 71L, 166L, 16L, 99L, 88L, 133L, 200L, 35L, 246L, 2L, 263L, 89L, 144L, 229L, 47L, 103L, 0L, 31L, 114L, 32L, 44L, 106L, 219L, 43L, 92L, 119L, 150L, 103L),
X20 = c(74L, 66L, 22L, 101L, 76L, 101L, 194L, 211L, 170L, 21L, 179L, 105L, 197L, 36L, 180L, 204L, 48L, 71L, 59L, 0L, 178L, 32L, 58L, 125L, 180L, 46L, 16L, 77L, 130L, 75L),
X21 = c(85L, 158L, 144L, 210L, 29L, 16L, 44L, 65L, 141L, 88L, 258L, 70L, 279L, 124L, 168L, 190L, 145L, 45L, 40L, 110L, 0L, 129L, 157L, 52L, 177L, 117L, 175L, 153L, 133L, 190L),
X22 = c(25L, 44L, 124L, 236L, 62L, 39L, 108L, 149L, 175L, 18L, 177L, 16L, 259L, 44L, 163L, 179L, 74L, 110L, 28L, 104L, 110L, 0L, 97L, 123L, 216L, 5L, 112L, 170L, 153L, 196L),
X23 = c(26L, 2L, 39L, 205L, 102L, 74L, 205L, 203L, 257L, 49L, 194L, 96L, 207L, 5L, 174L, 160L, 37L, 159L, 55L, 55L, 91L, 1L, 0L, 153L, 216L, 52L, 11L, 192L, 116L, 149L),
X24 = c(12L, 127L, 115L, 288L, 90L, 60L, 73L, 79L, 194L, 87L, 203L, 81L, 257L, 108L, 188L, 263L, 185L, 90L, 25L, 177L, 51L, 85L, 93L, 0L, 234L, 114L, 111L, 72L, 131L, 153L),
X25 = c(194L, 120L, 191L, 143L, 212L, 253L, 281L, 318L, 291L, 160L, 108L, 174L, 94L, 188L, 203L, 43L, 127L, 153L, 204L, 162L, 170L, 237L, 194L, 251L, 0L, 237L, 122L, 149L, 75L, 104L),
X26 = c(10L, 58L, 22L, 171L, 101L, 131L, 144L, 154L, 177L, 5L, 222L, 48L, 220L, 59L, 192L, 193L, 9L, 134L, 23L, 80L, 75L, 19L, 19L, 132L, 197L, 0L, 57L, 146L, 163L, 154L),
X27 = c(104L, 58L, 15L, 175L, 46L, 178L, 206L, 185L, 199L, 44L, 191L, 115L, 211L, 33L, 200L, 148L, 25L, 104L, 47L, 8L, 154L, 35L, 41L, 106L, 159L, 14L, 0L, 171L, 88L, 87L),
X28 = c(80L, 137L, 93L, 225L, 76L, 138L, 245L, 195L, 102L, 130L, 197L, 112L, 169L, 163L, 68L, 83L, 108L, 50L, 125L, 108L, 139L, 106L, 131L, 111L, 136L, 179L, 150L, 0L, 103L, 151L),
X29 = c(170L, 113L, 89L, 120L, 138L, 172L, 224L, 263L, 233L, 155L, 170L, 132L, 194L, 133L, 160L, 99L, 106L, 86L, 154L, 90L, 132L, 158L, 142L, 209L, 55L, 127L, 118L, 93L, 0L, 67L),
X30 = c(197L, 170L, 155L, 66L, 178L, 193L, 226L, 303L, 213L, 161L, 104L, 94L, 140L, 138L, 241L, 29L, 102L, 131L, 108L, 129L, 182L, 167L, 167L, 198L, 83L, 154L, 89L, 106L, 87L, 0L))
system.time({
g <- graph_from_adjacency_matrix(as.matrix(m), "directed", TRUE, FALSE)
mOpt <- shortest.paths(g, V(g), V(g), mode = "out")
})
#> user system elapsed
#> 0 0 0
mOpt[1:10, 1:10]
#> X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
#> X1 0 21 25 133 36 27 71 73 123 2
#> X2 18 0 10 122 40 45 89 91 127 17
#> X3 36 18 0 112 58 63 107 109 145 27
#> X4 130 112 117 0 152 157 201 203 239 129
#> X5 17 36 40 148 0 44 88 81 127 17
#> X6 39 48 52 160 28 0 86 68 142 41
#> X7 67 88 92 200 56 60 0 2 170 69
#> X8 88 109 113 221 77 81 84 0 191 90
#> X9 157 165 170 250 140 157 208 193 0 154
#> X10 22 19 23 131 34 49 92 94 121 0
mOptPaths <- lapply(V(g), function(from) all_shortest_paths(g, from = from, mode = "out"))
# optimal path from 1 to 2
(path_1_2 <- as.integer(mOptPaths[[1]]$res[[2]]))
#> [1] 1 10 26 22 2
# distances for path from 1 to 2
m[matrix(c(head(path_1_2, -1), path_1_2[-1]), ncol = 2)]
#> [1] 2 5 5 9

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]

Label points in a circle plot R

Is there a way to show the numbers that correspond to a point on a circle? I read up on the text(xy) function but it is for scatter plots which this is not. The scripts is as follows and the image attached shows what the result is. I would like to identify the point in the plots. Any help rendered is appreciated! Thanks.
library (circular)
df<- read.csv("Direction.csv", header = TRUE)
df1 <- df [ which(df$Month==1 & df$Day>0 & df$Day <32) ,]
df2 <- df1[c(-1,-2,-3)]
df3<- lapply(df2, function(df2) circular(df2, units='degrees', template='geographics'))
dens<- lapply(df3, density.circular, bw =5)
par(mfrow=c(5,4), oma=c(2,1.3,2,2), mar=c(1.5,2,2,1), tcl=-0.2, mgp=c(0,1,0))
titles <- c("1000mb", "925mb", "850mb", "700mb", "600mb", "500mb", "400mb", "300mb",
"250mb", "200mb", "150mb", "100mb","70mb", "50mb", "30mb", "20mb", "10mb")
for(i in 1:17){
plot(mean(df3[[1]]), main = titles[1],)
print(mean(df3[[1]]))
print(var(df3[[1]]))
print(summary(df3[[1]]))
}
dput(df3[1])
structure(list(X1000mb = structure(c(86L, 130L, 75L, 59L, 56L,
69L, 139L, 358L, 98L, 175L, 322L, 17L, 336L, 46L, 137L, 1L, 2L,
102L, 225L, 121L, 179L, 291L, 325L, 317L, 321L, 349L, 28L, 38L,
36L, 117L, 144L, 73L, 121L, 135L, 131L, 127L, 139L, 167L, 298L,
213L, 37L, 33L, 71L, 120L, 156L, 14L, 51L, 92L, 168L, 332L, 24L,
71L, 128L, 98L, 104L, 86L, 155L, 5L, 281L, 342L, 356L, 346L,
210L, 186L, 199L, 133L, 191L, 282L, 139L, 168L, 158L, 154L, 117L,
149L, 162L, 157L, 192L, 175L, 197L, 171L, 184L, 305L, 70L, 169L,
207L, 8L, 72L, 134L, 160L, 135L, 154L, 149L, 161L, 182L, 259L,
173L, 205L, 331L, 112L, 26L, 129L, 137L, 120L, 136L, 156L, 327L,
332L, 349L, 16L, 28L, 42L, 352L, 94L, 149L, 153L, 183L, 183L,
196L, 170L, 164L, 212L, 169L, 180L, 206L, 81L, 135L, 145L, 148L,
172L, 174L, 160L, 188L, 193L, 197L, 247L, 68L, 181L, 177L, 219L,
204L, 86L, 333L, 354L, 132L, 0L, 35L, 27L, 38L, 77L, 123L, 174L,
172L, 191L, 312L, 307L, 29L, 161L, 62L, 104L, 240L, 300L, 292L,
194L, 202L, 274L, 349L, 26L, 198L, 294L, 185L, 178L, 324L, 28L,
36L, 93L, 115L, 280L, 24L, 353L, 348L, 68L, 24L, 357L, 17L, 47L,
45L, 238L, 333L, 342L, 111L, 233L, 183L, 193L, 212L, 188L, 164L,
142L, 158L, 179L, 300L, 336L, 297L, 346L, 17L, 149L, 115L, 8L,
358L, 341L, 22L, 142L, 283L, 349L, 273L, 271L, 224L, 313L, 62L,
100L, 137L, 158L, 235L, 155L, 184L, 132L, 153L, 206L, 182L, 187L,
238L, 275L, 292L, 1L, 36L, 148L, 334L, 30L, 58L, 356L, 6L, 345L,
91L, 157L, 332L, 327L, 11L, 170L, 169L, 120L, 158L, 160L, 177L,
168L, 300L, 295L, 7L, 75L, 172L, 328L, 3L, 63L, 348L, 34L, 185L,
347L, 66L, 105L, 130L, 151L, 83L, 120L, 154L, 172L, 152L, 174L,
174L, 159L, 147L, 173L, 212L, 327L, 55L, 203L, 192L, 95L, 139L,
200L, 227L, 209L, 262L, 129L, 151L, 200L, 133L, 190L, 112L, 85L,
184L, 185L, 186L, 256L, 28L, 157L, 54L, 55L, 88L, 315L, 27L,
53L, 126L, 179L, 161L, 163L, 168L, 280L, 336L, 89L, 175L, 253L,
357L, 250L, 36L, 62L, 103L, 1L, 5L, 55L, 97L, 114L, 143L, 156L,
156L, 178L, 183L, 191L, 285L, 4L, 16L, 69L, 340L, 63L, 131L,
128L, 137L, 137L, 253L, 213L, 165L, 166L, 166L, 171L, 193L, 186L,
180L, 194L, 255L, 294L, 60L, 175L, 123L, 136L, 147L, 144L, 146L,
135L, 157L, 228L, 177L, 165L, 168L, 176L, 182L, 352L, 23L, 260L,
298L, 283L, 152L, 151L, 180L, 170L, 2L, 60L, 121L, 110L, 153L,
174L, 204L, 312L, 153L, 250L, 223L, 244L, 345L, 225L, 233L, 289L,
212L, 190L, 285L, 226L, 136L, 111L, 179L, 200L, 274L, 2L, 351L,
10L, 12L, 13L, 340L, 336L, 331L, 258L, 36L, 95L, 117L, 149L,
151L, 155L, 135L, 187L, 191L, 195L, 15L, 103L, 161L, 194L, 186L,
167L, 90L, 174L, 205L, 173L, 208L, 197L, 217L, 246L, 151L, 161L,
119L, 128L, 159L, 232L, 198L, 227L, 175L, 213L, 220L, 226L, 171L,
244L, 203L, 167L, 185L, 156L, 182L, 157L, 154L, 144L, 146L, 174L,
196L, 141L, 348L, 22L, 63L, 125L, 163L, 32L, 331L, 19L, 72L,
85L, 186L, 297L, 353L, 32L, 242L, 240L, 191L, 200L, 192L, 208L,
256L, 193L, 243L, 3L, 18L, 293L, 357L, 233L, 169L, 160L, 189L,
310L, 305L, 288L, 201L, 334L, 56L, 274L, 269L, 303L, 237L, 224L,
230L, 170L, 192L, 135L, 194L, 132L, 122L, 149L, 171L, 199L, 217L,
133L, 172L, 195L, 329L, 11L, 48L, 120L, 158L, 198L, 23L, 109L,
154L, 145L, 86L, 41L, 156L, 186L, 222L, 150L, 163L, 19L, 278L,
325L, 352L, 5L, 72L, 136L, 123L, 149L, 154L, 132L, 155L, 233L,
187L, 168L, 9L, 41L, 262L, 4L, 40L, 154L, 157L, 233L, 97L, 162L,
171L, 171L, 181L, 355L, 35L, 103L, 214L, 355L, 335L, 345L, 13L,
331L, 347L, 323L, 294L, 234L, 295L, 190L, 151L, 182L, 231L, 268L,
286L, 20L, 11L, 144L, 181L, 149L, 160L, 180L, 343L, 65L, 130L,
108L, 166L, 164L, 182L, 160L, 174L, 101L, 27L, 62L, 110L, 76L,
25L, 150L, 173L, 169L, 183L, 181L, 189L, 167L, 232L, 345L, 154L,
216L, 195L, 212L, 242L, 289L, 252L, 111L, 148L, 161L, 159L, 153L,
162L, 139L, 158L, 150L, 164L, 198L, 14L, 141L, 156L, 288L, 355L,
36L, 73L, 208L, 215L, 323L, 135L, 188L, 289L, 232L, 227L, 317L,
222L, 192L, 76L, 40L, 172L, 157L, 142L, 216L, 223L, 163L, 237L,
344L, 30L, 126L, 143L, 162L, 162L, 104L, 103L, 123L, 110L, 140L,
146L, 149L, 139L, 161L, 194L, 187L, 283L, 13L, 16L, 185L, 177L,
200L, 155L, 152L, 169L, 238L, 282L, 161L, 185L, 224L, 198L, 159L,
208L, 309L, 179L, 182L, 244L, 290L, 217L, 236L, 20L, 61L, 130L,
162L, 262L, 245L, 206L, 225L, 193L, 331L, 34L, 133L, 216L, 277L,
343L, 300L, 342L, 15L, 50L, 307L, 314L, 5L, 24L, 19L, 86L, 120L,
356L, 34L, 19L, 346L, 359L, 25L, 45L, 97L, 151L, 67L, 100L, 23L,
66L, 9L, 223L, 121L, 164L, 175L, 174L, 217L, 227L, 241L, 184L,
265L, 196L, 215L, 178L, 326L, 102L, 339L, 21L, 43L, 19L, 65L,
289L, 288L, 94L, 97L, 132L, 123L, 141L, 141L, 282L, 220L, 281L,
202L, 252L, 225L, 350L, 77L, 199L, 274L, 209L, 229L, 5L, 67L,
19L, 28L, 56L, 89L, 71L, 68L, 126L, 120L, 124L, 112L, 83L, 171L,
25L, 306L, 305L, 338L, 3L, 319L, 12L, 70L, 19L, 185L, 199L, 88L,
140L, 176L, 207L, 149L, 155L, 162L, 152L, 164L, 178L, 201L, 214L,
169L, 175L, 180L, 168L, 183L, 163L, 186L, 257L, 223L, 166L, 157L,
133L, 24L, 115L, 162L, 173L, 245L, 147L, 105L, 81L, 75L, 75L,
47L, 27L, 15L, 347L, 21L, 116L, 160L, 178L, 193L, 51L, 232L,
295L, 358L, 311L, 16L, 17L, 7L, 47L, 345L, 4L, 36L, 118L, 209L,
173L, 231L, 8L, 90L, 156L, 237L, 163L, 343L, 350L, 354L, 36L,
62L, 45L, 43L, 95L, 113L, 164L, 317L, 315L, 168L, 188L, 190L,
168L, 227L, 185L, 142L, 249L, 200L, 228L, 7L, 50L, 95L, 265L,
10L, 75L, 63L, 151L, 124L, 146L, 35L, 303L, 331L, 218L, 303L,
312L, 341L, 33L, 36L, 9L, 74L, 85L, 105L, 99L, 101L, 91L, 130L,
152L, 14L, 211L, 271L, 319L, 315L, 309L, 358L, 31L), circularp = structure(list(
type = "angles", units = "degrees", template = "geographics",
modulo = "asis", zero = 1.5707963267949, rotation = "clock"), .Names = c("type",
"units", "template", "modulo", "zero", "rotation")), class = c("circular",
"integer"))), .Names = "X1000mb")
It doesn't matter that it's not strictly a "scatterplot" . Now that you've set up an array of subplots, you can cycle thru them again, but this time using text() to place data at the desired location within each subplot. Roughly,
for (i in 1:17 ) text(x_loc[i],y_loc[i], some_text_vector[i])
Where you've "preloaded" the text strings and locations.

Simple R puzzle (elimination of pairs)

I have toyed with a number of ideas to do this, but so far have only come up with some rather inelegant solutions. I'm sure I could make it work, but the code would neither be pretty nor efficient. Here's the problem:
I have a series of integer pairs that are presented as rows in a two-column data frame. The goal is three-fold:
You need to "eliminate" all the rows in this data frame. To "eliminate" a row, you must select either one of the units from that pair and send/save it to a vector of "selected" elements.
You must find the smallest possible combination of "selected elements" that will eliminate all the pairs in the data frame.
The code must be computationally efficient because it will be applied to rather large datasets.
For instance, one would choose items "1" and "2" from the following list of pairs:
1 3
1 4
2 5
3 2
The data below can be used as a working example.
Thanks!
Vincent
Update for some context:
Hi Cipi and SiggyF.
I understand your concerns about this being homework, so in case you read this again, here's some context that I hope may dispell your doubts.
I am working with time-series cross sectional data in which N is much larger than T. I would like to use panel-corrected standard errors like those proposed in Beck & Katz (1995). The packages "pcse" is mostly able to do this just fine. When you have an unbalanced panel, it essentially creates a "rectangular" dataset (every time units has the full amount of observations) by filling in missing values for the omitted observations in every panel. Then, pcse computes a matrix Sigma.hat which is essentially the weighted average of the outer product of the residuals within time periods (think of it as averaging over an N X N X T array to bring it down to a N X N Sigma.hat).
The problem is that if any two units have zero contemporaneous observation, then the corresponding cell in Sigma.hat will be NA, and pcse won't be able to use it to get the sandwich estimator of the variance covariance matrix. In my example, the data frame numbers correspond to the index of the missing values in Sigma.hat. I want to trim down Sigma.hat automatically, to get an estimate of the VCOV that uses the most information possible, hence my desire to keep as many of the numbers in the data frame.
This is probably very unclear to anyone who hasn't looked into pcse, but I hope you get the gist of it.
Sorry to have given an impression of impropriety, but I assure you, this is legit.
test<-structure(list(row = c(27L, 44L,
45L, 111L, 128L, 129L, 195L, 212L,
213L, 279L, 296L, 297L, 363L, 380L,
381L, 7L, 91L, 175L, 259L, 343L, 44L,
45L, 70L, 128L, 129L, 154L, 212L,
213L, 238L, 296L, 297L, 322L, 380L,
381L, 406L, 7L, 37L, 48L, 91L, 121L,
132L, 175L, 205L, 216L, 259L, 289L,
300L, 343L, 373L, 384L, 7L, 37L, 48L,
91L, 121L, 132L, 175L, 205L, 216L,
259L, 289L, 300L, 343L, 373L, 384L,
44L, 45L, 128L, 129L, 212L, 213L,
296L, 297L, 380L, 381L, 37L, 121L,
205L, 289L, 373L, 27L, 44L, 45L, 111L,
128L, 129L, 195L, 212L, 213L, 279L,
296L, 297L, 363L, 380L, 381L, 7L,
91L, 175L, 259L, 343L, 44L, 45L, 70L,
128L, 129L, 154L, 212L, 213L, 238L,
296L, 297L, 322L, 380L, 381L, 406L,
7L, 37L, 48L, 91L, 121L, 132L, 175L,
205L, 216L, 259L, 289L, 300L, 343L,
373L, 384L, 7L, 37L, 48L, 91L, 121L,
132L, 175L, 205L, 216L, 259L, 289L,
300L, 343L, 373L, 384L, 44L, 45L,
128L, 129L, 212L, 213L, 296L, 297L,
380L, 381L, 37L, 121L, 205L, 289L,
373L, 27L, 44L, 45L, 111L, 128L,
129L, 195L, 212L, 213L, 279L, 296L,
297L, 363L, 380L, 381L, 7L, 91L,
175L, 259L, 343L, 44L, 45L, 70L, 128L,
129L, 154L, 212L, 213L, 238L, 296L,
297L, 322L, 380L, 381L, 406L, 7L,
37L, 48L, 91L, 121L, 132L, 175L, 205L,
216L, 259L, 289L, 300L, 343L, 373L,
384L, 7L, 37L, 48L, 91L, 121L, 132L,
175L, 205L, 216L, 259L, 289L, 300L,
343L, 373L, 384L, 44L, 45L, 128L,
129L, 212L, 213L, 296L, 297L, 380L,
381L, 37L, 121L, 205L, 289L, 373L,
27L, 44L, 45L, 111L, 128L, 129L, 195L,
212L, 213L, 279L, 296L, 297L, 363L,
380L, 381L, 7L, 91L, 175L, 259L, 343L,
44L, 45L, 70L, 128L, 129L, 154L,
212L, 213L, 238L, 296L, 297L, 322L,
380L, 381L, 406L, 7L, 37L, 48L, 91L,
121L, 132L, 175L, 205L, 216L, 259L,
289L, 300L, 343L, 373L, 384L, 7L, 37L,
48L, 91L, 121L, 132L, 175L, 205L,
216L, 259L, 289L, 300L, 343L, 373L,
384L, 44L, 45L, 128L, 129L, 212L,
213L, 296L, 297L, 380L, 381L, 37L,
121L, 205L, 289L, 373L, 27L, 44L,
45L, 111L, 128L, 129L, 195L, 212L,
213L, 279L, 296L, 297L, 363L, 380L,
381L, 7L, 91L, 175L, 259L, 343L, 44L,
45L, 70L, 128L, 129L, 154L, 212L,
213L, 238L, 296L, 297L, 322L, 380L,
381L, 406L, 7L, 37L, 48L, 91L, 121L,
132L, 175L, 205L, 216L, 259L, 289L,
300L, 343L, 373L, 384L, 7L, 37L, 48L,
91L, 121L, 132L, 175L, 205L, 216L,
259L, 289L, 300L, 343L, 373L, 384L,
44L, 45L, 128L, 129L, 212L, 213L,
296L, 297L, 380L, 381L, 37L, 121L,
205L, 289L, 373L), col = c(7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 27L, 27L, 27L, 27L, 27L,
37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 45L, 45L, 45L, 45L, 45L,
45L, 45L, 45L, 45L, 45L, 45L, 45L,
45L, 45L, 45L, 48L, 48L, 48L, 48L,
48L, 48L, 48L, 48L, 48L, 48L, 70L,
70L, 70L, 70L, 70L, 91L, 91L, 91L,
91L, 91L, 91L, 91L, 91L, 91L, 91L,
91L, 91L, 91L, 91L, 91L, 111L, 111L,
111L, 111L, 111L, 121L, 121L, 121L,
121L, 121L, 121L, 121L, 121L, 121L,
121L, 121L, 121L, 121L, 121L, 121L,
128L, 128L, 128L, 128L, 128L, 128L,
128L, 128L, 128L, 128L, 128L, 128L,
128L, 128L, 128L, 129L, 129L, 129L,
129L, 129L, 129L, 129L, 129L, 129L,
129L, 129L, 129L, 129L, 129L, 129L,
132L, 132L, 132L, 132L, 132L, 132L,
132L, 132L, 132L, 132L, 154L, 154L,
154L, 154L, 154L, 175L, 175L, 175L,
175L, 175L, 175L, 175L, 175L, 175L,
175L, 175L, 175L, 175L, 175L, 175L,
195L, 195L, 195L, 195L, 195L, 205L,
205L, 205L, 205L, 205L, 205L, 205L,
205L, 205L, 205L, 205L, 205L, 205L,
205L, 205L, 212L, 212L, 212L, 212L,
212L, 212L, 212L, 212L, 212L, 212L,
212L, 212L, 212L, 212L, 212L, 213L,
213L, 213L, 213L, 213L, 213L, 213L,
213L, 213L, 213L, 213L, 213L, 213L,
213L, 213L, 216L, 216L, 216L, 216L,
216L, 216L, 216L, 216L, 216L, 216L,
238L, 238L, 238L, 238L, 238L, 259L,
259L, 259L, 259L, 259L, 259L, 259L,
259L, 259L, 259L, 259L, 259L, 259L,
259L, 259L, 279L, 279L, 279L, 279L,
279L, 289L, 289L, 289L, 289L, 289L,
289L, 289L, 289L, 289L, 289L, 289L,
289L, 289L, 289L, 289L, 296L, 296L,
296L, 296L, 296L, 296L, 296L, 296L,
296L, 296L, 296L, 296L, 296L, 296L,
296L, 297L, 297L, 297L, 297L, 297L,
297L, 297L, 297L, 297L, 297L, 297L,
297L, 297L, 297L, 297L, 300L, 300L,
300L, 300L, 300L, 300L, 300L, 300L,
300L, 300L, 322L, 322L, 322L, 322L,
322L, 343L, 343L, 343L, 343L, 343L,
343L, 343L, 343L, 343L, 343L, 343L,
343L, 343L, 343L, 343L, 363L, 363L,
363L, 363L, 363L, 373L, 373L, 373L,
373L, 373L, 373L, 373L, 373L, 373L,
373L, 373L, 373L, 373L, 373L, 373L,
380L, 380L, 380L, 380L, 380L, 380L,
380L, 380L, 380L, 380L, 380L, 380L,
380L, 380L, 380L, 381L, 381L, 381L,
381L, 381L, 381L, 381L, 381L, 381L,
381L, 381L, 381L, 381L, 381L, 381L,
384L, 384L, 384L, 384L, 384L, 384L,
384L, 384L, 384L, 384L, 406L, 406L,
406L, 406L, 406L)), .Names = c("row",
"col" ), row.names = c(NA, -400L),
class = "data.frame")
Ok, if you consider your elements as vertices, and your pairs as edges of a graph, and your problem becomes a case of the well known (and NP complete) vertex cover problem. You can easily find an approximate solution, guaranteed to be within a factor of two of optimal by choosing an arbitrary edge, and selecting both vertices, removing all eliminated edges, lather, rinse, repeat. You can do incrementally better with more complicated approximation algorithms, but finding the optimal solution with a large graph is probably not feasible.
Here is a simple function to do this. (Note R is not my native language, so this is probably hideously non idomatic, any suggestions for improvement would be appreciated).
good <- function(dat, result = NULL) {
sampr <- dat[sample(1:(dim(dat)[1]),1),]
if (dim(dat)[1] == 0){
result
} else {
good(subset(dat, row != sampr$row & row != sampr$col & col != sampr$row &
col != sampr$col),result = c(result, sampr$row, sampr$col))
}
}
I'd run this a number of times and keep the best one. (It might also be useful to keep track of the size of the worst one, as it gives you a lower bound on the optimal size). It might be useful to postprocess the result to remove excess vertices.
Running 10000 iterations (and removing redundant vertices) gives the following 19 element solution to your sample problem.
7 37 45 48 91 121 128 132 175 205 212 216 259 279 289 300 343 373 384
We also know that the optimal solution must have at least 15 vertices.

Resources