可能重复: R and matrix with 1 row
我有数百个矩阵,在一个for循环中,我对它们做了一些修改,包括排序。问题是矩阵只有一行。因此,当我给他们排序时,他们的类会从矩阵变化到字符,如下所示:
> test1
Gene ID Gene Name Score(d) Fold Change q-value(%)
[1,] "g17035" "17035" "-29.1" "0.877" "303.826"
> class(test1)
[1] "matrix"当应用顺序时,它变成了字符类:
test1 <- test1[顺序(test1,5),]
> test1
Gene ID Gene Name Score(d) Fold Change q-value(%)
"g17035" "17035" "-29.1" "0.877" "303.826"
> class(test1)
[1] "character"我甚至使用了as.matrix,但它按不需要的顺序更改了矩阵:
test1 <-as.matrix矩阵( test1[order(test1,5),])
然后会是这样:
> test1
[,1]
Gene ID "g17035"
Gene Name "17035"
Score(d) "-29.1"
Fold Change "0.877"
q-value(%) "303.826"我怎么才能修好它?提前谢谢你
发布于 2012-12-07 09:29:54
您要寻找的是不降低子集的维度,而方法是使用drop参数到[。更多信息可在?"["中获得。
# Demo matrix
> a <- matrix(1:9, 3, 3)
> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# With reduction
> a[1,]
[1] 1 4 7
> class(a[1,])
[1] "integer"
# Without reduction
> a[1,,drop=FALSE]
[,1] [,2] [,3]
[1,] 1 4 7
> class(a[1,,drop=FALSE])
[1] "matrix"https://stackoverflow.com/questions/13760163
复制相似问题