给定一行,一列或一个单元格的矩阵,我需要在保持矩阵结构的同时重新排序行.我尝试添加,drop=F但它不起作用!我做了什么?
test = matrix(letters[1:5]) # is a matrix
test[5:1,,drop=F] # not a matrix
test2 = matrix(letters[1:5],nrow=1) # is a matrix
test2[1:1,,drop=F] # not a matrix
test3 = matrix(1) # is a matrix
test3[1:1,,drop=F] # not a matrix
Run Code Online (Sandbox Code Playgroud)
我猜它被覆盖了F; F可以设置为变量,在这种情况下它不再是假的.始终FALSE完全写出来,不能将其设置为变量.
另外,- [R地狱,部分32年8月1日,是一个很好的基准.
> F <- 1
> test = matrix(letters[1:5]) # is a matrix
> test[5:1,,drop=F] # not a matrix
[1] "e" "d" "c" "b" "a"
> test[5:1,,drop=FALSE] # but this is a matrix
[,1]
[1,] "e"
[2,] "d"
[3,] "c"
[4,] "b"
[5,] "a"
> rm(F)
> test[5:1,,drop=F] # now a matrix again
[,1]
[1,] "e"
[2,] "d"
[3,] "c"
[4,] "b"
[5,] "a"
Run Code Online (Sandbox Code Playgroud)