英文:
Calculate min() and max() of each matrix column
问题
我使用以下矩阵:
dput(data)
=
结构(c(269.643848985434,284.856715574861,278.687093704939,
242.00628682971,258.754844814539,257.241126745939,250.868861973286,
267.881099879742,266.277047023177,264.643696397543,283.306375294924,
278.137760534883,251.230201125145,264.389782398939,260.087893307209,
263.933225497603,284.947050362825,279.185155779123,262.722251042724,
284.903103709221,279.329203143716,256.589251384139,274.861293360591,
270.730307921767,233.265785723925,249.657887518406,247.94396802783,
264.665669724345,284.124271348119,278.701742589474,0,0,0
),dim = c(3L,11L),dimnames = list(NULL,c(& quot; & quot;,& quot; & quot;,& quot; & quot;,& quot; & quot;,
& quot; & quot;,& quot; & quot;,& quot; & quot;,& quot; & quot;,
& quot; & quot;,& quot; & quot;,& quot; gt & quot;)))
并且,对于每一列,我想找到值的范围(min
和 max
?)并保存它们(例如在一个列表中)。
有什么简单的方法可以做到这一点?
谢谢!
英文:
I'm using the following matrix:
dput(data)
=
structure(c(269.643848985434, 284.856715574861, 278.687093704939,
242.00628682971, 258.754844814539, 257.241126745939, 250.868861973286,
267.881099879742, 266.277047023177, 264.643696397543, 283.306375294924,
278.137760534883, 251.230201125145, 264.389782398939, 260.087893307209,
263.933225497603, 284.947050362825, 279.185155779123, 262.722251042724,
284.903103709221, 279.329203143716, 256.589251384139, 274.861293360591,
270.730307921767, 233.265785723925, 249.657887518406, 247.94396802783,
264.665669724345, 284.124271348119, 278.701742589474, 0, 0, 0
), dim = c(3L, 11L), dimnames = list(NULL, c("", "", "", "",
"", "", "", "", "", "", "gt")))
and, for each column, I'd like to find the range of values (min
and max
?) and save them (f.e. in a list).
What would be an easy way to do this?
Thank you!
答案1
得分: 3
我来帮你翻译代码部分:
让我引起你的注意,**matrixStats** 包。它不仅仅是语法糖,还是用C++实现的,因此非常快速。
matrixStats::colRanges(m)
# [,1] [,2]
# 269.6438 284.8567
# 242.0063 258.7548
# 250.8689 267.8811
# 264.6437 283.3064
# 251.2302 264.3898
# 263.9332 284.9471
# 262.7223 284.9031
# 256.5893 274.8613
# 233.2658 249.6579
# 264.6657 284.1243
# gt 0.0000 0.0000
请注意,我只翻译了代码部分,没有包括其他内容。
英文:
Let me draw your attention to the matrixStats package. It's nut just syntactic sugar, it's also implemented in C++ and, thus, very fast.
matrixStats::colRanges(m)
# [,1] [,2]
# 269.6438 284.8567
# 242.0063 258.7548
# 250.8689 267.8811
# 264.6437 283.3064
# 251.2302 264.3898
# 263.9332 284.9471
# 262.7223 284.9031
# 256.5893 274.8613
# 233.2658 249.6579
# 264.6657 284.1243
# gt 0.0000 0.0000
答案2
得分: 2
你要找的函数是 range()
:
range
返回一个包含所有给定参数的最小值和最大值的向量。
使用 apply()
并设置 MARGIN = 2
来迭代每一列,应用 range()
函数。然后设置适当的行名称,例如 c("min", "max")
。由于你想要一个列表,可以使用 asplit()
来将结果矩阵的列拆分为向量列表。
假设你的矩阵叫做 m
:
apply(m, MARGIN = 2, range) |>
`rownames<-`(c("min", "max")) |>
asplit(MARGIN = 2)
# [[1]]
# min max
# 269.6438 284.8567
# [[2]]
# min max
# 242.0063 258.7548
# [[3]]
# min max
# 250.8689 267.8811
# [[4]]
# min max
# 264.6437 283.3064
# [[5]]
# min max
# 251.2302 264.3898
# [[6]]
# min max
# 263.9332 284.9471
# [[7]]
# min max
# 262.7223 284.9031
# [[8]]
# min max
# 256.5893 274.8613
# [[9]]
# min max
# 233.2658 249.6579
# [[10]]
# min max
# 264.6657 284.1243
# $gt
# min max
# 0 0
英文:
The function you're looking for is range()
:
> range
returns a vector containing the minimum and maximum of all the given arguments.
Use apply()
with MARGIN = 2
to iterate over each column, applying the range()
function. Then set appropriate row names, e.g. c("min", "max")
. As you want a list, use asplit()
to split the columns of the resulting matrix into a list of vectors.
Assuming your matrix is called m
:
apply(m, MARGIN = 2, range) |>
`rownames<-`(c("min", "max")) |>
asplit(MARGIN = 2)
# [[1]]
# min max
# 269.6438 284.8567
# [[2]]
# min max
# 242.0063 258.7548
# [[3]]
# min max
# 250.8689 267.8811
# [[4]]
# min max
# 264.6437 283.3064
# [[5]]
# min max
# 251.2302 264.3898
# [[6]]
# min max
# 263.9332 284.9471
# [[7]]
# min max
# 262.7223 284.9031
# [[8]]
# min max
# 256.5893 274.8613
# [[9]]
# min max
# 233.2658 249.6579
# [[10]]
# min max
# 264.6657 284.1243
# $gt
# min max
# 0 0
答案3
得分: 0
我们可以使用 tapply
+ col
+ range
来实现如下操作:
> tapply(data, col(data), range)
$`1`
[1] 269.6438 284.8567
$`2`
[1] 242.0063 258.7548
$`3`
[1] 250.8689 267.8811
$`4`
[1] 264.6437 283.3064
$`5`
[1] 251.2302 264.3898
$`6`
[1] 263.9332 284.9471
$`7`
[1] 262.7223 284.9031
$`8`
[1] 256.5893 274.8613
$`9`
[1] 233.2658 249.6579
$`10`
[1] 264.6657 284.1243
$`11`
[1] 0 0
英文:
We can use tapply
+ col
+ range
like below
> tapply(data, col(data), range)
$`1`
[1] 269.6438 284.8567
$`2`
[1] 242.0063 258.7548
$`3`
[1] 250.8689 267.8811
$`4`
[1] 264.6437 283.3064
$`5`
[1] 251.2302 264.3898
$`6`
[1] 263.9332 284.9471
$`7`
[1] 262.7223 284.9031
$`8`
[1] 256.5893 274.8613
$`9`
[1] 233.2658 249.6579
$`10`
[1] 264.6657 284.1243
$`11`
[1] 0 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论