[英]Reordering columns in an R dataframe according to average values
I would like to reorder a data frame's columns according to arithmetic mean of each column. 我想根据每列的算术平均值对数据框的列重新排序。
For: 对于:
S1 S2 S3
1 1 1
2 1 1
3 3 1
the expected output is: 预期的产出是:
S3 S2 S1
1 1 1
1 1 2
1 3 3
In above case, the averages were: S1 = 2
, S2 = 1.6666
and S3 = 1
, inverting S1 and S3 columns positions in a data frame. 在上述情况下,平均值为:
S1 = 2
, S2 = 1.6666
, S3 = 1
,反转数据帧中的S1和S3列位置。
Additionally, my real data have NA´s values too. 另外,我的真实数据也有NA的值。
Use the order()
function. 使用
order()
函数。
An exemplary data frame: 示例性数据框:
df <- data.frame(s1=runif(5), s2=runif(5), s3=runif(5))
df[1,2] <- NA # some NAs
df
## s1 s2 s3
## 1 0.74473576 NA 0.71547379
## 2 0.66997782 0.6474405 0.62320795
## 3 0.05361586 0.5370381 0.03298139
## 4 0.06209263 0.9409920 0.46096984
## 5 0.42432948 0.9983042 0.38503196
Calculate column means, with NAs omitted: 计算列平均值,省略NA:
(mns <- colMeans(df, na.rm=TRUE))
## s1 s2 s3
## 0.3909503 0.7809437 0.4435330
The desired column ordering is: 所需的列顺序是:
order(mns)
## [1] 1 3 2
( s1
goes first, s2
goes last, and s3
should become the 2nd column) (
s1
首先, s2
最后, s3
应该成为第2列)
Now you may reorder the columns: 现在您可以重新排序列:
(df <- df[,order(mns)])
## s1 s3 s2
## 1 0.74473576 0.71547379 NA
## 2 0.66997782 0.62320795 0.6474405
## 3 0.05361586 0.03298139 0.5370381
## 4 0.06209263 0.46096984 0.9409920
## 5 0.42432948 0.38503196 0.9983042
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.