R:dplyr提取TIBLE中以特定元素为中心的n个元素

R:dplyr提取TIBLE中以特定元素为中心的n个元素,r,dplyr,tibble,R,Dplyr,Tibble,我有一个包含两列基因和tpm的TIBLE,我想提取以特定基因为中心的n个元素。TIBLE在tpm列上排序。我现在分两步做。在本例中,我想要一个以ELF2基因为中心的10元素窗口: index <- e %>% rownames_to_column() %>% filter(gene=="ELF2") %>% select(rowname) %>% as.numeric() e %>% dplyr::slice((index-10):(index+10)) 我

我有一个包含两列基因和tpm的TIBLE,我想提取以特定基因为中心的n个元素。TIBLE在tpm列上排序。我现在分两步做。在本例中,我想要一个以ELF2基因为中心的10元素窗口:

index <- e %>% rownames_to_column() %>% filter(gene=="ELF2") %>% select(rowname) %>% as.numeric()
e %>% dplyr::slice((index-10):(index+10))

我们也可以在一个过程中做到这一点

e %>%
     slice( {tmp <- which(gene == 'ELF2')
              (tmp-10):(tmp+10)})

我们也可以在一个过程中做到这一点

e %>%
     slice( {tmp <- which(gene == 'ELF2')
              (tmp-10):(tmp+10)})

以下是一句话:

e[(which(e$gene == "ELF2")-10):(which(e$gene == "ELF2")+10), ]
或者,使用%>%管道操作符:

"ELF2" %>%
  { which(e$gene == .) } %>%
  { e[seq(.-10, .+10), ] }

以下是一句话:

e[(which(e$gene == "ELF2")-10):(which(e$gene == "ELF2")+10), ]
或者,使用%>%管道操作符:

"ELF2" %>%
  { which(e$gene == .) } %>%
  { e[seq(.-10, .+10), ] }