Create Integer Sequences Defined by 'From' and 'To' Vectors

Create integer sequences defined by 'from' and 'to' vectors

Just use mapply:

Start = c(1,10,20)
Finish = c(9,19,30)
mapply(":", Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##

You could, of course, also use Vectorize, but that's just a wrapper for mapply. However, Vectorize cannot be used with primitive functions, so you'll have to specify seq.default rather than seq, or seq.int.

Example:

Vectorize(seq.default)(Start, Finish)
## [[1]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[2]]
## [1] 10 11 12 13 14 15 16 17 18 19
##
## [[3]]
## [1] 20 21 22 23 24 25 26 27 28 29 30
##

R Build a sequence of vectors by index

mapply(":", start, end, SIMPLIFY = FALSE)
#OR
lapply(1:length(start), function(i) start[i]:end[i])
#OR
apply(cbind(start, end), 1, function(x) x[1]:x[2])
#[[1]]
#[1] 11 12 13 14 15 16

#[[2]]
#[1] 26 27 28 29 30 31 32 33

#[[3]]
#[1] 55 56 57 58 59 60

#[[4]]
# [1] 110 111 112 113 114 115 116 117 118 119 120

Two vectors sequence one to another

We can use Map to create a list of vectors. Here, the : will get the sequence of values from each corresponding elements of 'v1' and 'v2'

Map(`:`, v1, v2)

data

v1 <- 1:2
v2 <- 9:10

R: generate many number sequences in one call

You can try Map

 Map(`:`, starts, ends)

It will generate sequence for corresponding elements of 'starts', 'ends' in a list.

Or instead of :, we can use seq (as @MrFlick mentioned)

 Map(seq, starts, ends)

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

mapply works nicely:

> as.vector(mapply(seq,start_values,end_values))
[1] 88 89 90 91 92 93 94 95 96 97 98 99 100 101
[15] 102 103 104 105 106 107 108 109 110 111 112 113 114 115
[29] 116 117 118 119 120 121 122 123 124 125 126 127 128 129
[43] 130 131 132 133 134 135 136 137 138 139 140 141 142 143
[57] 144 145 146 147 241 242 243 244 245 246 247 248 249 250
[71] 251 252 253 254 255 256 257 258 259 260 261 262 263 264
[85] 265 266 267 268 269 270 271 272 273 274 275 276 277 278
[99] 279 280 281 282 283 284 285 286 287 288 289 290 291 292
[113] 293 294 295 296 297 298 299 300 394 395 396 397 398 399
[127] 400 401 402 403 404 405 406 407 408 409 410 411 412 413
[141] 414 415 416 417 418 419 420 421 422 423 424 425 426 427
[155] 428 429 430 431 432 433 434 435 436 437 438 439 440 441
[169] 442 443 444 445 446 447 448 449 450 451 452 453 545 546
[183] 547 548 549 550 551 552 553 554 555 556 557 558 559 560
[197] 561 562 563 564 565 566 567 568 569 570 571 572 573 574
[211] 575 576 577 578 579 580 581 582 583 584 585 586 587 588
[225] 589 590 591 592 593 594 595 596 597 598 599 600 601 602
[239] 603 604

Creating a sequence in R

test<-NULL
for(i in 1:(Ncla-1)) {
A.1=c(seq(CC.1[i],CC.2[i],1))
test<-c(test,A.1)
}

test

Your mistake: You were not saving your results.

Create sequence between certain values across two vectors

We can use findInterval to subset the 'b' based on the value of 'a' and then with Map get the corresponding sequence (:=) between the elements of the 'a' and the subset elements of 'b'

Map(`:`, a, b[findInterval(a, b) + 1])
#[[1]]
#[1] 1 2 3

#[[2]]
#[1] 8 9 10 11 12

#[[3]]
#[1] 14 15 16 17 18

#[[4]]
#[1] 34 35 36 37 38 39 40 41 42

#[[5]]
#[1] 46 47 48 49

#[[6]]
#[1] 55 56 57

Populate vector with integer sequence, on construction

Don't over do it. The simple thing to do is to default initialize the vector, reserve and initialize from the range.

Make sure objects are initialized before they are used.

That does not mean that the member must be fully initialized in the initialization list, rather than the my_class object must be fully initialized when the constructor completes.

Other than that, just for the sake of it, there are different things you can do in vanilla C++ to handle this, like creating a helper function and returning the vector by value:

std::vector<int> create_vector() {
std::vector<int> v;
// ...
return v;
}

But I would not use this (or any other alternative) to initialize a member, only if needed (the vector is const might be sufficient excuse :))



Related Topics



Leave a reply



Submit