Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用if语句筛选最小/最大值R_R_If Statement_Dplyr - Fatal编程技术网

使用if语句筛选最小/最大值R

使用if语句筛选最小/最大值R,r,if-statement,dplyr,R,If Statement,Dplyr,下面是我试图筛选的df的一部分示例 Gene Chr Start End V5 Strand ENSMUSG00000028364 chr4 64012669 64020725 . - ENSMUSG00000028364 chr4 63959785 64047015 . - ENSMUSG00000018387 chr11 53457249 53467501

下面是我试图筛选的df的一部分示例

Gene                Chr     Start       End         V5  Strand
ENSMUSG00000028364  chr4    64012669    64020725    .   -   
ENSMUSG00000028364  chr4    63959785    64047015    .   -
ENSMUSG00000018387  chr11   53457249    53467501    .   +   
ENSMUSG00000018387  chr11   53457205    53467766    .   +
对于每个基因,我想保留cols3和cols4中第一个(或最后一个)值的行,这取决于基因所在的链。我尝试了一些不同的方法,但都出现了错误,无法工作

df <- df %>%
   group_by(Gene) %>%
   ifelse(df$Strand == "+", (filter(Start==first(Start), End ==first(End))), (filter(End==last(End), Start ==last(Start))))

Error in ifelse(., df$Strand == "+", (filter(Start == first(Start),  : unused argument ((filter(End == last(End), Start == last(Start))))


df <- df %>%
   group_by(Gene) %>%
   {if (df$Strand == "+") filter(Start==first(Start), End ==first(End)) else filter(End==last(End), Start ==last(Start))}

Error in filter(Start == first(Start), End == first(End)) : object 'Start' not found
In addition: Warning message:
In if (df$Strand == "+") filter(Start == first(Start),  :

 Error in filter(Start == first(Start), End == first(End)) : object 'Start' not found 

有什么建议得到这项工作请?谢谢

我认为您正在使用
过滤器寻找类似的内容

library(tidyverse)
df %>%
  group_by(Gene) %>%
  # Get first row for "+" and last for "-"
  filter(Strand == "+" & row_number() == 1 | Strand == "-" & row_number() == n())

Strand='+'
时,保持
Start
End
的第一个值是否有问题

下面的代码在分组前按
进行过滤,以去除不必要的数据。然后进行分组和总结

如果您不需要所有原始列,而只想保留分组列
Gene
和摘要列
Start
End
,请删除
left\u join
,这样就可以获得其余的输入数据集列

library(tidyverse)

df %>%
  filter(Strand == '+') %>%
  group_by(Gene) %>%
  summarise(Start = first(Start), End = first(End)) %>%
  left_join(df)
#Joining, by = c("Gene", "Start", "End")
## A tibble: 1 x 6
#  Gene                  Start      End Chr   V5    Strand
#  <chr>                 <int>    <int> <chr> <chr> <chr> 
#1 ENSMUSG00000018387 53457249 53467501 chr11 .     +     
库(tidyverse)
df%>%
过滤器(串=='+')%>%
分组依据(基因)%>%
总结(开始=第一(开始),结束=第一(结束))%>%
左联合(df)
#连接,通过=c(“基因”、“开始”、“结束”)
##一个tibble:1 x 6
#基因起始端Chr V5链
#                           
#1 ENSMUG00000018387 53457249 53467501 chr11+

我认为您需要单独使用
过滤器,而不是
ifelse
。您的预期输出是什么(我有点不清楚)?似乎您需要这个,但不确定:
df%>%groupby(Gene)%%>%filter(ifelse(strind=“+”,first(Start)&first(End),last(Start)&last(End))
library(tidyverse)

df %>%
  filter(Strand == '+') %>%
  group_by(Gene) %>%
  summarise(Start = first(Start), End = first(End)) %>%
  left_join(df)
#Joining, by = c("Gene", "Start", "End")
## A tibble: 1 x 6
#  Gene                  Start      End Chr   V5    Strand
#  <chr>                 <int>    <int> <chr> <chr> <chr> 
#1 ENSMUSG00000018387 53457249 53467501 chr11 .     +