# Multi-range query with R

**URL:** <https://forum.tiledb.com/t/multi-range-query-with-r/764>\
**Category:** Uncategorized\
**Created:** [January 16, 2025, 10:00pm UTC](https://forum.tiledb.com/t/multi-range-query-with-r/764 "2025-01-16T22:00:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![julio514](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/julio514/32/362_2.png) [@julio514](https://forum.tiledb.com/u/julio514)\
**Post date:** [January 16, 2025, 10:00pm UTC](https://forum.tiledb.com/t/multi-range-query-with-r/764/1 "2025-01-16T22:00:13Z")

</div>

I have a large tiledb database with 144000 fragments. At some point, I need to select that database by multi-range indexes. With python, it works well, Let’s say I need to select indexes 3 and 6, I can do the following which executes in less than a second:  
`matrix_tdb.df[[3,6], :]`

However, in R, it is generally much slower to load the db. But I also can’t select by specific indexes like I can in python. For instance, I can load the database with this instruction:  
`matrix_tdb = tiledb_array(matrix_tdb_uri, "READ", keep_open=FALSE, return_as="data.frame")`

Then, say I’d like to extract indexes 3 and 6, I do the following (which takes many seconds to execute, in contrast with python):  
`tmp = matrix_tdb[c(3,6),]`  
But I always get a range returned:

```auto
> unique(tmp$cell_index)
[1] 3 4 5 6

```

I have tried to use the solution described here: [https://docs.tiledb.com/main/how-to/arrays/reading-arrays/multi-range-subarrays](https://docs.tiledb.com/main/how-to/arrays/reading-arrays/multi-range-subarrays)

```auto
tiledb_query_add_range(qry, sch, "rows", 3L, 4L)
tiledb_query_add_range(qry, sch, "rows", 7L, 9L)

```

But whenever I do that I always get returned the last specified range excluding the previous one. In that example, I would get indexes 7 to 9 only, not 3 to 4.

Any hint would be appreciated.  
Cheers,

---

<div class="post-metadata">

**Author:** ![julio514](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/julio514/32/362_2.png) [@julio514](https://forum.tiledb.com/u/julio514)\
**Post date:** [January 16, 2025, 10:47pm UTC](https://forum.tiledb.com/t/multi-range-query-with-r/764/2 "2025-01-16T22:47:25Z")

</div>

Okay I managed to figure it out. I need to specify a matrix as range. For instance if I want indexes 3 and 6 in my example, I would need to build a query matrix like this one:

```auto
> query_mat <- cbind(matrix(c(3,6)), matrix(c(3,6)))
> query_mat
     [,1] [,2]
[1,] 3 3
[2,] 6 6

```

Then,` tmp = matrix_tdb[query_mat,]`

But using R for querying my tiledb is really much slower than when using python.
