【发布时间】:2021-08-27 03:08:02
【问题描述】:
我在数据框中有一个方阵,需要交换行和列,以便最高值位于主对角线上并向外变小。 我在编码时遇到了麻烦,因为我是编码新手。
我得到的一个示例矩阵:
input <- data.table(
c( 4, 4, 3, 5, 2),
c( 4, 2, 5, 3, 4),
c( 3, 5, 2, 4, 1),
c( 3, 1, 4, 2, 5),
c( 5, 3, 4, 4, 3)
)
output <- function(input)
output #would be nice if the output could also be a data frame
5 4 3 2 1
4 5 4 3 2
3 4 5 4 3
2 3 4 5 4
1 2 3 4 5
I would appreciate help very much :)
【问题讨论】:
-
那么到目前为止你尝试了什么?您的代码在哪里不起作用?请提供minimal reproducible example,以便我们帮助解决您的问题。我建议阅读How to Ask 并查看What is the proper way to approach Stack Overflow as someone totally new to programming?。因此,请通过edit提出问题并添加我们需要帮助您的详细信息来帮助我们。
-
结果是否总是一个完整的带状矩阵?为什么你想要一个 data.frame 作为结果而不是一个更合适的数据结构?
-
如果你在第二行第五列输入一个 20 怎么办?排序后会进入第五列/行还是进入第二列/行?生成的矩阵是否总是对称的?
标签: r sorting matrix data.table