89_Simple
89_Simple

Reputation: 3805

R generate sequence for each pair in a vector

I have a vector that could be of N length. For e.g

  x <- c(298, 307, 347, 374, 416)

I want to generate the sequence of number for each pair of vector like this:

  298:307
  308:347
  348:374
  375:416

and put that in a dataframe:

  temp_df <- data.frame(i = c(298:307, 308:347, 348:374, 375:416),
                    j = c(rep(1, length(298:307)), rep(2, length(308:347)), 
                          rep(3, length(348:374)), rep(4, length(375:416))))

I need to write a function that can take any length of vector and generate the temp_df

  temp_func <- function(my.vec){

     temp.length <- length(my.vec) - 1
     temp_list <- list()

    for(j in 1:temp.length){
    
       jk <- j + 1
      
       if(j == 1){
      
         temp_list[[j]] <- 
          data.frame(i = my.vec[j]:my.vec[jk],                        
                     j = j)
      
        } else {
          temp_list[[j]] <- data.frame(i = (my.vec[j] + 1):my.vec[jk],                        
                                      j = j)
       }
    }

   test <- do.call('rbind', temp_list)

  return(test)
}

  temp_func(x)

Is there a quicker one liner to do this in R?

Upvotes: 2

Views: 628

Answers (3)

Ronak Shah
Ronak Shah

Reputation: 389315

In this case you don't need any loop at all.

You can create a sequence between minimum value in x and maximum value and to get the j column you can use cut or findInterval.

vec <- seq(min(x), max(x))
data.frame(i =vec, j = cut(vec, x, labels = FALSE, include.lowest = TRUE))

#      i j
#1   298 1
#2   299 1
#3   300 1
#4   301 1
#5   302 1
#6   303 1
#7   304 1
#8   305 1
#9   306 1
#10  307 1
#11  308 2
#12  309 2
#....

Upvotes: 2

user12728748
user12728748

Reputation: 8516

Here is another base R solution:

x <- c(298, 307, 347, 374, 416)

splitInts <- function(x){
    x1 <- seq(x[1], x[length(x)])
    unname(split(x1, findInterval(x1, x[-1] + 1)))
}

splitInts(x)
#> [[1]]
#>  [1] 298 299 300 301 302 303 304 305 306 307
#> 
#> [[2]]
#>  [1] 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
#> [20] 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
#> [39] 346 347
#> 
#> [[3]]
#>  [1] 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
#> [20] 367 368 369 370 371 372 373 374
#> 
#> [[4]]
#>  [1] 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#> [20] 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#> [39] 413 414 415 416

Created on 2020-06-21 by the reprex package (v0.3.0)

Upvotes: 0

akrun
akrun

Reputation: 887911

One option is Map to create the sequence of corresponding elements to two vectors created by removing the first and last element of 'x', then create the data.frame by unlisting the list of vectors and the replicated sequence of list based on the lengths of the list

x1 <- x[-length(x)]
x1[-1] <- x1[-1] + 1
lst1 <-  Map(`:`, x1, x[-1])
temp_df2 <- data.frame(i = unlist(lst1), j = rep(seq_along(lst1), lengths(lst1)))
all.equal(temp_df2, temp_df)
#[1] TRUE

It can be wrapped as a function

f1 <- function(vec){
       vec1 <- vec[-length(vec)]
       vec1[-1] <- vec1[-1] + 1
       lst1 <- Map(`:`, vec1, vec[-1])
       data.frame(i = unlist(lst1), j = rep(seq_along(lst1), lengths(lst1)))
     }

f1(x)

Or with stack

stack(setNames(lst1, seq_along(lst1)))[2:1]

Or as a one-liner

stack(setNames(Map(`:`, (x + rep(0:1, c(1, length(x)-1)))[-length(x)], 
            x[-1]), seq_along(x[-1])))

Or with tidyverse

library(dplyr)
library(tidyr)
library(purrr)
tibble(x, x1 = lead(x) + 1) %>%
      na.omit %>% 
      transmute(j = row_number(),
                i = map2(x, x1, `:`)) %>% 
      unnest(c(i))

Upvotes: -1

Related Questions