Find a window of 2 rows either side of specific value and return information from a different column in R

StackOverflow https://stackoverflow.com/questions/19507339

  •  01-07-2022
  •  | 
  •  

Вопрос

Hi I'm looking to find a window of 4 rows, 2 rows either side of pvalues less than a specific threshold, and then create a new data.table containing information regarding a specific character of these rows.

SNP pval  GO
1   0.9   A
2   0.8   C
3   0.4   A
4   0.3   D
5   0.9   L
6   0.02  L
7   0.6   L
8   0.1   G
9   0.6   P
10  0.8   E

From the example data above, looking at the 2 rows either side of a pval < 0.05 the desired output would be.

GO
D
L
L
G

Ultimately I would like to extend this to a large data.frame and look at 1000 row windows for multiple pval < 0.05

Thankyou for any help

Это было полезно?

Решение

Getting the set of indices where pval<0.05 is easy, the question is how to get the indices within 2 of these. One possibility is this:

> indices<-c(5,9,13,17,25)
> indices2<-sort(unique(as.vector(outer(indices,c(-2,-1,1,2),`+`))))
> indices2
 [1]  3  4  6  7  8 10 11 12 14 15 16 18 19 23 24 26 27

So putting it all together in your case would look like this:

> dt[sort(unique(as.vector(outer(which(dt$pval<0.05),c(-2,-1,1,2),`+`)))),]
  SNP pval GO
4   4  0.3  D
5   5  0.9  L
7   7  0.6  L
8   8  0.1  G
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top