简体   繁体   中英

Subsetting data frame in R based on range of values in a column

I have a data frame (df) with multiple columns and rows such as:

    A     B   C 

    0.6   a.  b

    0.9   c.  d

    1.1.  e.  f

    1.2   g.  h

    1.4   I   l

    1.5.  m.  n

    5.0   o.  p

    5.3   q.  r

    5.6.  s.  t

    6.1.  u  v

    6.5.  w. z

    6.9.  y  a

    7.0.  b. c

The code I am looking for should calculate the difference between each consecutive values in column A ( 0.9-0.3 = 0.3, 1.1-0.9=0.2 and so on) and if the difference is larger then a certain threshold (here we set is as 3 but can be different) it will subset a certain number of rows (let's say 3 in this case, but it can be different too) before and after that gap where the difference is greater then the threshold set. So, in this case 5.0 - 1.5 = 3.5 which is larger then 3, 3 rows before 1.5 and 3 rows after 5.0 will be kept, the rest removed. Any idea of how to write such code?

Output:

    A     B   C 

    1.1.  e.  f

    1.2   g.  h

    1.4   I   l

    1.5.  m.  n

    5.0   o.  p

    5.3   q.  r

    5.6.  s.  t

    6.1.  u  v

I have multiple data frames so the values in column A are different,the code should look into each data frame one by one and find where is the gap in the column A based on the threshold set.

Data in dput format.

Input: data.frame df1 .

df1 <-
structure(list(A = c(0.6, 0.9, 1.1, 1.2, 1.4, 
1.5, 4, 4.3, 4.6, 5.1, 5.5, 5.9, 6), 
B = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 2L), .Label = c("a.", 
"b.", "c.", "e.", "g.", "I", "m.", "o.", 
"q.", "s.", "u", "w.", "y"), class = "factor"), 
C = structure(c(2L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 1L, 3L), .Label = c("a", 
"b", "c", "d", "f", "h", "l", "n", "p", 
"r", "t", "v", "z"), class = "factor")), 
row.names = c(NA, -13L), class = "data.frame")

Output: data.frame out .

out <-
structure(list(A = c(1.1, 1.2, 1.4, 1.5, 4, 
4.3, 4.6, 5.1), B = structure(1:8, 
.Label = c("e.", "g.", "I", "m.", "o.", 
"q.", "s.", "u"), class = "factor"), 
C = structure(1:8, .Label = c("f", "h", "l", 
"n", "p", "r", "t", "v"), class = "factor")), 
row.names = c(NA, -8L), class = "data.frame")

This is my df :

structure(list(POS = c(207687374L, 207689227L, 207690871L, 207691563L, 
207693563L, 207694165L, 207694357L, 207738077L, 207739127L, 207740272L, 
207740868L, 207747296L, 207747984L, 207748107L), SNP = c("rs12130494", 
"rs4844601", "rs10863358", "rs77357299", "rs12043913", "rs61822967", 
"rs11117991", "rs7515905", "rs3886100", "rs12038575", "rs34883952", 
"rs1752684", "rs17046851", "rs10127904"), Std_iHS = c(-1.52176, 
-1.51905, -1.50286, 0.656487, -1.45251, 0.84325, -1.06089, -1.41041, 
1.29513, 1.21325, 0.456717, -1.00933, -1.71468, 0.265969)), row.names = 
21:34, class = "data.frame")

Output:

structure(list(POS = c(207691563L, 
207693563L, 207694165L, 207694357L, 207738077L, 207739127L, 207740272L, 
207740868L, ), SNP = c( "rs77357299", "rs12043913", "rs61822967", 
"rs11117991", "rs7515905", "rs3886100", "rs12038575", "rs34883952", 
), Std_iHS = c( 0.656487, -1.45251, 0.84325, -1.06089, -1.41041, 
1.29513, 1.21325, 0.456717, )), row.names = 21:34, class = "data.frame")

Using base R you could do something like:

limit = 2
df1[match(unique(c(sapply(which(diff(df1$A)>limit),function(x)(x-3):(x+4)))),1:nrow(df1)),]
     A  B C
3  1.1 e. f
4  1.2 g. h
5  1.4  I l
6  1.5 m. n
7  4.0 o. p
8  4.3 q. r
9  4.6 s. t
10 5.1  u v

It looks like your example dataframe doesn't have any jumps over 3.0, but this code should work:

limit <- 2.0

structure(list(A = c(0.6, 0.9, 1.1, 1.2, 1.4, 
                 1.5, 4, 4.3, 4.6, 5.1, 5.5, 5.9, 6), 
           B = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 
                           9L, 10L, 11L, 12L, 13L, 2L), .Label = c("a.", 
                                                                   "b.", "c.", "e.", "g.", "I", "m.", "o.", 
                                                                   "q.", "s.", "u", "w.", "y"), class = "factor"), 
           C = structure(c(2L, 4L, 5L, 6L, 7L, 8L, 9L, 
                           10L, 11L, 12L, 13L, 1L, 3L), .Label = c("a", 
                                                                   "b", "c", "d", "f", "h", "l", "n", "p", 
                                                                   "r", "t", "v", "z"), class = "factor")), 
      row.names = c(NA, -13L), class = "data.frame") %>%
mutate(diffA = A - lag(A, 1)) %>%
  mutate(over_limit = diffA > limit) %>%
  mutate(before_limit = lag(over_limit, 1) | lag(over_limit, 2),
     after_limit = lead(over_limit, 1) | lead(over_limit, 2)) %>%
  rowwise() %>%
  mutate(subset_filter = any(over_limit, after_limit, before_limit)) %>%
  ungroup() %>%
  filter(subset_filter) %>%
  select(-c(subset_filter, diffA, over_limit, before_limit, after_limit))

output in dput() format:

structure(list(A = c(1.4, 1.5, 4, 4.3, 4.6),
B = structure(6:10, .Label = c("a.", "b.", "c.", "e.", "g.", "I", "m.", "o.", "q.", "s.", "u", "w.", "y"), class = "factor"), 
C = structure(7:11, .Label = c("a", "b", "c", "d", "f", "h", "l", "n", "p", "r", "t", "v", "z"), class = "factor")), 
class = c("tbl_df",  "tbl", "data.frame"), 
row.names = c(NA, -5L), .Names = c("A", "B", "C"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM