在使用R语言时,想对一个矩阵matrix使用order函数按照某一列排序,结果报错
> print(rep_motion)
[,1] [,2] [,3]
[1,] NaN NaN NaN
[2,] 0.0434575 0.01098376 0.9512894
[3,] 0.001588562 0.001588562 0.99722
[4,] 0.004219409 0 0.9978903
[5,] 0.004721435 0.0006295247 0.9952786
[6,] 0.008078995 0.000598444 0.9931179
[7,] 0.01246883 0.009975062 0.9806733
[8,] 0 0 1
[9,] 0.03324361 0.02113055 0.9510094
[10,] 0.02582149 0.01563 0.9611535
[11,] 0.006769826 0.005802708 0.9883946
[12,] 0.001959248 0.0003918495 0.9980408
[13,] 0.002842524 0.0005685048 0.9971575
[14,] 0.00748183 0.001710133 0.9910218
[15,] 0.02713675 0.01901709 0.9626068
[16,] 0.03195067 0.009529148 0.9646861
[17,] 0.01386159 0.01799938 0.9778628
[18,] 0.02140673 0.1569827 0.9006116
[19,] 0.06902854 0.01588641 0.9190215
[20,] 0.006300257 0.003150128 0.9912957
[21,] 0.01619586 0.006779661 0.9777778
[22,] 0.006226313 0.00351922 0.9910666
[23,] 0.01170929 0.005552047 0.9836333
> rep_motion[order(rep_motion[,3])]
Error in order(rep_motion[, 3]) : unimplemented type 'list' in 'orderVector1'
查看了一下rep_motion的类型:
> typeof(rep_motion)
[1] "list"
> mode(rep_motion)
[1] "list"
> class(rep_motion)
[1] "matrix"
原因是创建这个矩阵的时候,把每一行都作为一个list组合到一起的,这样在order的时候,相当于对list进行order,而不是对数字进行order。数字是有大小的所以才能排序,而list不可以,因此会报错。
当你的matrix变为如下类型时,就不会报错了。
> typeof(evaluated)
[1] "double"
> mode(evaluated)
[1] "numeric"
> class(evaluated)
[1] "matrix"
具体类型转换方式要看你是怎么定义的了。若仍未解决,欢迎评论区交流!