通过逻辑条件过滤data.frame行 [英] Filter data.frame rows by a logical condition

查看:96
本文介绍了通过逻辑条件过滤data.frame行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于逻辑条件从data.frame过滤行.假设我有像这样的数据框

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    5.345618 bj fibroblast
2    5.195871 bj fibroblast
3    5.247274 bj fibroblast
4    5.929771          hesc
5    5.873096          hesc
6    5.665857          hesc
7    6.791656          hips
8    7.133673          hips
9    7.574058          hips
10   7.208041          hips
11   7.402100          hips
12   7.167792          hips
13   7.156971          hips
14   7.197543          hips
15   7.035404          hips
16   7.269474          hips
17   6.715059          hips
18   7.434339          hips
19   6.997586          hips
20   7.619770          hips
21   7.490749          hips

我想要的是获取一个看起来相同但只包含一个cell_type数据的新数据框.例如.子集/选择包含单元格类型"hesc"的行:

What I want to is to get a new data frame which looks the same but only has the data for one cell_type. E.g. subset / select rows which contains the cell type "hesc":

   expr_value     cell_type
1    5.929771          hesc
2    5.873096          hesc
3    5.665857          hesc

或"bj成纤维细胞"或"hesc"细胞类型:

Or either cell type "bj fibroblast" or "hesc":

   expr_value     cell_type
1    5.345618 bj fibroblast
2    5.195871 bj fibroblast
3    5.247274 bj fibroblast
4    5.929771          hesc
5    5.873096          hesc
6    5.665857          hesc

有没有简单的方法可以做到这一点?

Is there any easy way to do this?

我尝试过:

expr[expr[2] == 'hesc']
# [1] "5.929771" "5.873096" "5.665857" "hesc"     "hesc"     "hesc"    

如果原始数据帧被称为"expr",但是如您所见,它会以错误的格式给出结果.

if the original data frame is called "expr", but it gives the results in wrong format as you can see.

推荐答案

要根据 one 'cell_type'(例如'hesc')选择行,请使用==:

To select rows according to one 'cell_type' (e.g. 'hesc'), use ==:

expr[expr$cell_type == "hesc", ]

要根据两个或多个不同的"cell_type"(例如"hesc" "bj成纤维细胞")选择行,请使用%in%:

To select rows according to two or more different 'cell_type', (e.g. either 'hesc' or 'bj fibroblast'), use %in%:

expr[expr$cell_type %in% c("hesc", "bj fibroblast"), ]

这篇关于通过逻辑条件过滤data.frame行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆