Test whether position x is between any start (i=1 to i=max) and end (i=1 to i=max) positions stored in lists

眉间皱痕 提交于 2021-02-08 09:09:48

问题


I have a simple data frame with specifies start and end positions within lists. These start and end positions define i number of regions. Now I would like to test whether a given position lies within such a region and if yes I need to know in which region (i).

Here is a simple example data frame:

start <- list(c(5,10,15), c(5) ,c(6,11),c(6,11))
end <- list(c(7,11,17), c(10), c(8,12),c(8,12))
imax <- c(3,1,2,2)
position <- c(11,6,9,8)

example <- data.frame(start = I(start), end = I(end), imax = imax, position = position)

When I have only one start and end position it is no problem (as in row 2 of example):

data.table::between(example$position[[1]], example$start[[1]], example$end[[1]])

[1] FALSE  TRUE FALSE

How can I turn this into a function which checks this pairwise for every element (from i=1 to i=max) within example$start and example$end?

The second step would be to retrieve for which region i (1 to imax) this was TRUE.

Thank you.


回答1:


It sounds like you might be looking for a function like this.

As your start and end are lists, you can unlist. To check each element pairwise, you can loop through start and end up to imax.

Assuming you can have more than one region, you can return a list (or something else) at the end of the function.

my_fun <- function(x) {
  vec <- integer(0)
  start <- unlist(x[["start"]])
  end <- unlist(x[["end"]])
  for (i in 1:x[["imax"]]) {
    if (between(x[["position"]], start[i], end[i])) vec <- c(vec, i)
  }
  list(vec)
}

example$regions <- apply(example, 1, my_fun)

Output

      start       end imax position regions
1 5, 10, 15 7, 11, 17    3       11       2
2         5        10    1        6       1
3     6, 11     8, 12    2        9        
4     6, 11     8, 12    2        8       1


来源:https://stackoverflow.com/questions/65049499/test-whether-position-x-is-between-any-start-i-1-to-i-max-and-end-i-1-to-i-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!