R dplyr过滤值大于+ N且小于-N的数据:abs()函数? [英] R dplyr filtering data with values greater than +N and lesser than -N : abs() function?

查看:163
本文介绍了R dplyr过滤值大于+ N且小于-N的数据:abs()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中使用 dplyr 软件包来过滤我的基因表达数据。我已经计算出倍数变化,并希望过滤至少一个样本(列)的值大于+0.584963或小于-0.584963的基因(行)。示例数据:

I am using the dplyr package in R for filtering my data of gene expressions. I have calculated fold changes and would like to filter the genes (rows) in which at least one sample (columns) has a value greater than +0.584963 OR less than -0.584963.An example data:

       X SAMPLE_1_FC SAMPLE_2_FC SAMPLE_3_FC SAMPLE_4_FC SAMPLE_5_FC
GENE_1      0.6780      0.4050      0.8870      0.3300      0.2230
GENE_2      0.2340     -0.6670      0.0020      0.1240      0.3560
GENE_3      0.0170      0.1560      0.1120      0.0080     -0.1230
GENE_4     -0.0944     -0.1372     -0.1800     -0.2228     -0.2656
GENE_5     -0.8080     -0.7800     -0.5560      0.0340      0.4450
GENE_6      0.2091      0.1106      0.0121     -0.0864     -0.1849
GENE_7      0.5980      0.7680      0.9970      0.4670     -0.7760

我当前正在使用以下脚本

I am currently using the following script

det.cols<- colnames(my.data)[which(grepl("fc",tolower(colnames(my.data))))]
filt <- gsub(","," | ",toString(paste("`",det.cols,"`",">abs(0.584963)", sep = "")))
my.datasub<- my.data %>% filter_(filt)

但这仅返回大于+0.584963的基因,而不返回负基因。在该示例的情况下,我想要的是具有基因1、2、5和7的子集列表。但是相反,它仅给我基因1和7。我该如何更改?

but this returns only the genes greater than +0.584963 and not the negative ones. In the case of the example, what I want is a subsetted list with Genes 1, 2, 5 and 7. But instead it gives me only Genes 1 and 7. How can I change this?

我希望答案采用以下格式:

I am expecting the answer to be in this format:

 X SAMPLE_1_FC SAMPLE_2_FC SAMPLE_3_FC SAMPLE_4_FC SAMPLE_5_FC
GENE_1      0.6780      0.4050      0.8870      0.3300      0.2230
GENE_2      0.2340     -0.6670      0.0020      0.1240      0.3560
GENE_5     -0.8080     -0.7800     -0.5560      0.0340      0.4450
GENE_7      0.5980      0.7680      0.9970      0.4670     -0.7760

谢谢。

推荐答案

长话短说,您在代码中的错误位置放置了 abs()

Long story short, you had the abs() in the wrong place in your code.

我在这里将其修复:

det.cols<- colnames(my.data)[which(grepl("fc",tolower(colnames(my.data))))]
filt <- gsub(","," | ",toString(paste("abs(`",det.cols,"`)",">0.584963", sep = "")))
my.datasub<- my.data %>% filter_(filt)

为了进一步提高灵活性,@ ha_pu提供了一个很棒的 filter_at 解决方案以先前的解决方案为基础(在我识别出您的代码中的错误之前)。

For further flexibility, @ha_pu provided a great filter_at solution building off my previous solution (before I identified the error in your code).

这篇关于R dplyr过滤值大于+ N且小于-N的数据:abs()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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