'Filter rows in a specific range values containing character and number in R

I'm using RStudio to perform some analysis.

I have this data frame:

Residue Energy Model
R-A-40 -3.45 DELTA
R-A-350 -1.89 DELTA
R-B-468 -0.25 DELTA
R-C-490 -2.67 DELTA
R-A-610 -1.98 DELTA

I would like to filter the first column ("Residue") based on the numeric values (between 300 to 500) and create a new data frame. The new data frame would be like this:

Residue Energy Model
R-A-350 -1.89 DELTA
R-B-468 -0.25 DELTA
R-C-490 -2.67 DELTA

Note that it does not matter if starts with "R-A-", "R-B-" or "R-C-". However, I have different patterns (not only these three). I have to ignore the non-numeric characters or the first four characters from "Residue" column.

I did not find any similar question. I appreciate any help!

Thanks an advance



Solution 1:[1]

# convert character to integer
df$x <- as.integer(substr(df$Residue, 5, nchar(df$Residue)))

# subset
df[df$x %between% c(300,500), ]

Solution 2:[2]

Similar to one of the answers already given, but a little more readable:

library(tidyverse)

df <- tribble(~Residue, ~Energy,    ~Model,
              "R-A-40", -3.45,  "DELTA",
              "R-A-350",    -1.89, "DELTA",
              "R-B-468",    -0.25,  "DELTA")
df %>% 
  mutate(number = str_extract(Residue, "[:DIGIT:]+")) %>% 
  mutate(number = as.numeric(number)) %>% 
  filter(number >= 300 & number <= 500)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sweepy Dodo
Solution 2 Ben G