如何对整体上大于等于0.2的列进行排序? [英] How to sort the columns with values greater than equal to 0.2 on a whole?

查看:134
本文介绍了如何对整体上大于等于0.2的列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下数据框:

df:

Sample  Region4 Region1 Region5 Region3 Region2 Type
T1       0       0.289    0.378   0       1      K
T2       0       0.167     0     0.875  0.389    K
T3    0.186     0.12345    0      0     0.187    K
T4   0.11234     0.1789    0     0.457  0.786    L
T5    0.2347     0.2567    0      0       0      L
T6   0.28769       0     0.123   0.1987 0.1565   L
T7    0.142        0     0.1987   0       0      M
T8       0       0.1256  0.123   0.129   0.111   M
T9    0.187      0.987     0     0.237   0.783   M

如何对值> = 0.2的列进行明智排序,不应对值<0.2进行排序.

How to sort the column wise with values >=0.2, sorting should not be applied on values <0.2.

我以这种方式尝试过.但是没用

I tried in this way. But didn't work

sort(apply(df[grepl("Region",length(colnames(df)], 2, function(x) 
 any(x >= 0.2)),decreasing=TRUE)

推荐答案

我们可以在区域"(Region)列上使用lapply循环,创建逻辑向量('i1'),使用该逻辑子集对值order进行子集化并更新

We can loop with lapply on the 'Region' columns, create logical vector ('i1'), use that to subset the values, order and update it

nm1 <- grepl("Region",names(df1))
df1[nm1] <- lapply(df1[nm1], function(x) {
         i1 <- x >= 0.2 
         x[i1] <- x[i1][order(x[i1], decreasing = TRUE)]
         x})

这篇关于如何对整体上大于等于0.2的列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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