按元素总和/平均值过滤行 [英] Filter rows by sum/average of their elements

查看:54
本文介绍了按元素总和/平均值过滤行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R脚本中,我有一个数据帧,格式为:

In a R script I have a DataFrame in the format:

 V1           V2           V3           V4
1  HIAT1 3.917271e-05 4.278916e-05 3.793761e-05
2  SASS6 2.008972e-06 1.890391e-06 2.168946e-06
3 TRMT13 4.397712e-06 4.724036e-06 4.009512e-06

其中第一列是字符串名称,以下是数字值。
值的总数不是先验的,但取决于提供给脚本输入的数据集。

Where the first column is a String name and the following are numeric values. The total number of values is not known a priori, but depends on the dataset provided as input to the script.

我只想提取以下行:从第二个元素到最后一个元素的总和(或平均值)大于某个阈值。

I want to extract only the rows for which the sum(or average) of elements from the second to the last is greater than a certain threshold.

如何用R风格做到这一点?

How to do that in R-style?

推荐答案

我们可以使用数字列的 rowSums / rowMeans ,使用获取逻辑向量> 并对行进行子集化。

We can use rowSums/rowMeans of the numeric columns, get a logical vector using > and subset the rows.

 thresh <- 0.0001
 df1[rowSums(df1[,-1])> thresh,]

这篇关于按元素总和/平均值过滤行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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