首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在r中排序后如何保持矩阵的类

在r中排序后如何保持矩阵的类
EN

Stack Overflow用户
提问于 2012-12-07 09:22:54
回答 1查看 208关注 0票数 3

可能重复: R and matrix with 1 row

我有数百个矩阵,在一个for循环中,我对它们做了一些修改,包括排序。问题是矩阵只有一行。因此,当我给他们排序时,他们的类会从矩阵变化到字符,如下所示:

代码语言:javascript
复制
> 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),]

代码语言:javascript
复制
> 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),])

然后会是这样:

代码语言:javascript
复制
> test1
    [,1]     
Gene ID     "g17035" 
Gene Name   "17035"  
Score(d)    "-29.1"  
Fold Change "0.877"  
q-value(%)  "303.826"

我怎么才能修好它?提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-07 09:29:54

您要寻找的是不降低子集的维度,而方法是使用drop参数到[。更多信息可在?"["中获得。

代码语言:javascript
复制
# 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"
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13760163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档