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
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),]
This worked like magic for me.
celltype_hesc_bool = expr['cell_type'] == 'hesc'
expr_celltype_hesc = expr[celltype_hesc]
Check this blog post
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