考虑以下汇总表:
m <- structure(c(0.00165720273925865, 0.000414300684814661, 0.00126727268296249,
0.0105768527770331, 0.00126727268296249, 0.00155972022518461,
0.00046304194185168, 0.001291643311481, 0.0107961884336997, 0.00158409085370312,
0.00114541954036995, 0.000438671313333171, 0.00141349645407355,
0.0107230765481442, 0.00182779713888821, 0.00131601393999951,
0.00046304194185168, 0.00124290205444398, 0.00984573392147784,
0.00175468525333268, 0.00158409085370312, 0.000536153827407209,
0.00173031462481417, 0.0100894402066629, 0.00119416079740697), .Dim = c(5L,
5L), .Dimnames = list(c("M001_0.6", "M002_0.6", "M004_0.6", "M012_0.6",
"M013_0.6"), NULL))看起来是这样的:
[,1] [,2] [,3] [,4] [,5]
M001_0.6 0.0016572027 0.0015597202 0.0011454195 0.0013160139 0.0015840909
M002_0.6 0.0004143007 0.0004630419 0.0004386713 0.0004630419 0.0005361538
M004_0.6 0.0012672727 0.0012916433 0.0014134965 0.0012429021 0.0017303146
M012_0.6 0.0105768528 0.0107961884 0.0107230765 0.0098457339 0.0100894402
M013_0.6 0.0012672727 0.0015840909 0.0018277971 0.0017546853 0.0011941608我希望找到每一行的前3个最大值,并保留每个行的最大3值出现的位置的列索引。使用此代码,可以获得前3名,但列索引将被丢弃。
apply(m, 1,function(x) sort(x,decreasing = TRUE))发布于 2022-04-06 17:01:58
您可以使用order获取索引,使用head(x, 3)获取前3个值。
head(apply(m, 1, function(x) order(x, decreasing = TRUE)), 3)
M001_0.6 M002_0.6 M004_0.6 M012_0.6 M013_0.6
[1,] 1 5 5 2 3
[2,] 5 2 3 3 4
[3,] 2 4 2 1 2然后你就可以得到这样的东西:
list(index = t(head(apply(m, 1, function(x) order(x, decreasing = TRUE)), 3)),
values = t(head(apply(m, 1,function(x) sort(x, decreasing = TRUE)), 3)))
$index
[,1] [,2] [,3]
M001_0.6 1 5 2
M002_0.6 5 2 4
M004_0.6 5 3 2
M012_0.6 2 3 1
M013_0.6 3 4 2
$values
[,1] [,2] [,3]
M001_0.6 0.0016572027 0.0015840909 0.0015597202
M002_0.6 0.0005361538 0.0004630419 0.0004630419
M004_0.6 0.0017303146 0.0014134965 0.0012916433
M012_0.6 0.0107961884 0.0107230765 0.0105768528
M013_0.6 0.0018277971 0.0017546853 0.0015840909https://stackoverflow.com/questions/71770742
复制相似问题