如何根据特定的标准过滤行? [英] How to filter rows based on certain criteria?

查看:174
本文介绍了如何根据特定的标准过滤行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的示例文件:

pre $ GENE Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
g1 0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344
g2 0.700 0.000 0.000 0.000 0.000 0.000 0.000
g3 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g4 0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345
g5 0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000
g6 0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000

如果2个或更多个样本的值为0.010或更多,我需要检索行(基因)的列表。所以我应该得到如下的结果列:

  GENES 
g1
g4
g5

任何人都可以帮助我吗?

解决方案

以下是一种可能的解决方法:

  DF < -  read.table(text = 
GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
g1 0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344
g2 0.700 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g3 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g4 0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345
g5 0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000
g6 0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000,header = T,sep ='')


rows < - sapply(1:nrow(DF),FUN = function(i) (DF [i,2:ncol(DF)]> = 0.01)> = 2})
subSet <-DF [行,]

> subSet
GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
1 g1 0.000 0.000 0.000 0.000 0.01 0 0.022 0.344
4 g4 0.322 0.782 0.000 0.023 0.00 0 0.000 0.345
5 g5 0.010 0.000 0.333 0.000 0.00 0 0.011 0.000

或类似的:
$ b $ (函数(x){sum(tail(x,-1)> = 0.01)> = 2})。 ,]

或者:

<$ p $ (行数(DF [,2:ncol(DF)]> = 0.01)> = 2,]
pre>

你可以看到有很多方法可以完成这个工作:)

I have an example file as follows:

GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
g1    0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344
g2    0.700 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g3    0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g4    0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345
g5    0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000
g6    0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000

I need to retrieve the list of rows (genes) if it has "2 or more samples" with the values "0.010 or more". So I should get the resulting column as follows.:

GENES
g1
g4
g5

Can anyone help me with this ?

解决方案

Here's one possible way:

DF <- read.table(text=
"GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
g1 0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344
g2 0.700 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g3 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
g4 0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345
g5 0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000
g6 0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000",header=T,sep=' ')


rows <- sapply(1:nrow(DF),FUN=function(i){sum(DF[i,2:ncol(DF)] >= 0.01) >= 2})
subSet <- DF[rows,]

> subSet
  GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8
1    g1 0.000 0.000 0.000 0.000  0.01     0 0.022 0.344
4    g4 0.322 0.782 0.000 0.023  0.00     0 0.000 0.345
5    g5 0.010 0.000 0.333 0.000  0.00     0 0.011 0.000

or similarly this:

subSet <- DF[apply(DF,1,function(x){sum(tail(x,-1) >= 0.01) >= 2}),]

or this:

subSet <- DF[rowSums(DF[,2:ncol(DF)] >= 0.01) >= 2,]

as you can see there are many ways to accomplish that :)

这篇关于如何根据特定的标准过滤行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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