Turn a large number interval into a smaller interval with a specified range

Viewed 33

Let's say I have this table:

chr start_coor  end_coor
1   122000001   128000000

I would like to turn this table into the following table, where the interval is set to 1 million:

chr start_coor  end_coor
1   122000001   123000000
1   123000001   124000000
1   124000001   125000000
1   125000001   126000000
1   126000001   127000000
1   127000001   128000000

With base R or tidyverse functions, how can I achieve this?

Thank you.

1 Answers

With tidyverse you can do :

library(tidyverse)

df %>%
  mutate(start_coor = map2(start_coor, end_coor, seq, 1000000)) %>%
  unnest(start_coor) %>%
  mutate(end_coor = start_coor + 999999)

#    chr start_coor  end_coor
#   <int>      <dbl>     <dbl>
#1     1  122000001 123000000
#2     1  123000001 124000000
#3     1  124000001 125000000
#4     1  125000001 126000000
#5     1  126000001 127000000
#6     1  127000001 128000000

data

df <- structure(list(chr = 1L, start_coor = 122000001L, end_coor = 128000000L), 
class = "data.frame", row.names = c(NA, -1L))
Related