0

I have a data frame that looks something like this:

my_data <- structure(list(chr = c("chr14", "chr14", "chr14", "chr14", "chr14", "chr14", "chr14", "chr14", "chr14", "chr14"), start = c(1245841L, 4729880L, 5042400L, 6129542L, 6130563L, 6131756L, 6135211L, 6139073L, 6144706L, 6145560L), end = c(1245858L, 4729897L, 5042417L, 6129559L, 6130580L, 6131773L, 6135228L, 6139090L, 6144723L, 6145577L)), class = "data.frame", row.names = c(NA, -10L))

I now wish to make a new data_frame where only a certain picked combination of the row exists. I'd like to create a new data frame with only the rows in which start value >= 6129542 and end value <= 6145577.

I tried with this combination but it doesn't work

library(tidyverse)    
new_data <- my_data %>% filter(start >= 6129542 |
                               end <= 6145577)

Could Could you help me find a solution?

cucalorda
  • 35
  • 4
  • '|' is used for an OR condition. Since you want both conditions to be true you need to replace it with an AND condition '&'. – tivd Sep 24 '22 at 15:24
  • simply replace the `|` with a comma. – waywardteachR Sep 24 '22 at 15:42
  • Base R also has a subset function: `new_data <- subset(my_data, start >= 6129542 & end <= 6145577)` Some people use the Tidyverse by default. But whatever works for you. – SteveM Sep 24 '22 at 16:51

1 Answers1

0

base R option by subsetting on two columns like this:

my_data <- structure(list(chr = c("chr14", "chr14", "chr14", "chr14", "chr14",  "chr14", "chr14", "chr14", "chr14", "chr14"), start = c(1245841L,  4729880L, 5042400L, 6129542L, 6130563L, 6131756L, 6135211L, 6139073L,  6144706L, 6145560L), end = c(1245858L, 4729897L, 5042417L, 6129559L,  6130580L, 6131773L, 6135228L, 6139090L, 6144723L, 6145577L)), class = "data.frame", row.names = c(NA,  -10L))

my_data[with(my_data, start >= 6129542 & end <= 6145577), ]
#>      chr   start     end
#> 4  chr14 6129542 6129559
#> 5  chr14 6130563 6130580
#> 6  chr14 6131756 6131773
#> 7  chr14 6135211 6135228
#> 8  chr14 6139073 6139090
#> 9  chr14 6144706 6144723
#> 10 chr14 6145560 6145577

Created on 2022-09-24 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53