Filter data.frame rows by a logical condition

前端 未结 9 1367
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 05:43

I want to filter rows from a data.frame based on a logical condition. Let\'s suppose that I have data frame like

   expr_value     cell_type
1           


        
相关标签:
9条回答
  • 2020-11-21 06:02

    I was working on a dataframe and having no luck with the provided answers, it always returned 0 rows, so I found and used grepl:

    df = df[grepl("downlink",df$Transmit.direction),]
    

    Which basically trimmed my dataframe to only the rows that contained "downlink" in the Transmit direction column. P.S. If anyone can guess as to why I'm not seeing the expected behavior, please leave a comment.

    Specifically to the original question:

    expr[grepl("hesc",expr$cell_type),]
    
    expr[grepl("bj fibroblast|hesc",expr$cell_type),]
    
    0 讨论(0)
  • 2020-11-21 06:04

    This worked like magic for me.

    celltype_hesc_bool = expr['cell_type'] == 'hesc'

    expr_celltype_hesc = expr[celltype_hesc]

    Check this blog post

    0 讨论(0)
  • 2020-11-21 06:10

    The reason expr[expr[2] == 'hesc'] doesn't work is that for a data frame, x[y] selects columns, not rows. If you want to select rows, change to the syntax x[y,] instead:

    > expr[expr[2] == 'hesc',]
      expr_value cell_type
    4   5.929771      hesc
    5   5.873096      hesc
    6   5.665857      hesc
    
    0 讨论(0)
提交回复
热议问题