简体   繁体   English

根据列中的值范围在R中子集数据帧

[英]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: 我有一个具有多个列和行的数据框(df),例如:

    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. 我正在寻找的代码应该计算A列中每个连续值之间的差异(0.9-0.3 = 0.3、1.1-0.9 = 0.2,依此类推),如果差异较大,则将某个阈值(此处设置为3)但可以不同)它将在该间隙之前和之后分配一定数量的行(在这种情况下,假设为3,但是也可以有所不同),在这种情况下,差异大于阈值设置。 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. 因此,在这种情况下,将保留5.0-1.5 = 3.5,该值大于3、1.5之前的3行和5.0之后的3行,其余部分将被删除。 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. 我有多个数据帧,因此A列中的值不同,代码应逐个查看每个数据帧,并根据阈值设置找出A列中的间隙在哪里。

Data in dput format. dput格式的数据。

Input: data.frame df1 . 输入: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 . 输出: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 : 这是我的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: 使用base R,您可以执行以下操作:

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: 看起来您的示例数据框没有超过3.0的任何跳转,但是此代码应该可以工作:

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: 以dput()格式输出:

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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