read.table中的错误(文件=文件,标头=标头,sep = sep,引用=引用,:不允许重复的“ row.names”

时间:2019-04-23 16:17:51

标签: r csv matrix rstudio

我的矩阵像这样-  13367*13367长矩阵-

    NBAS    DNAH9   NRAS    NRAS    TP53    TP53    TP53    SCYL2   RNF19A
NBAS    1   0   0   0   0   0   0   0   0
DNAH9   0   1   0   0   0   0   0   0   0
NRAS    0   0   1   0   0   0   0   0   0
NRAS    0   0   0   1   0   0   0   0   0
TP53    0   0   0   0   1   0   0   0   0
TP53    0   0   0   0   0   1   0   0   0
TP53    0   0   0   0   0   0   1   0   0
SCYL2   0   0   0   0   0   0   0   1   0
RNF19A  0   0   0   0   0   0   0   0   1

我需要提取所有值等于1的行和列标题对。我正在使用以下R脚本-

Pmatrix = read.csv ("file.csv", header= TRUE, row.names = 1)
sig_values <- which(Pmatrix==1, arr.in=TRUE)
cbind.data.frame(colIDs = colnames(Pmatrix)[ sig_values[, 1] ],rowIDs = rownames(Pmatrix)[ sig_values[, 2] ])

但出现错误-

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  duplicate 'row.names' are not allowed

如果我输入row.names = False,R将不假定行名,而是添加编号。但是我需要行名和列名而不是数字。

1 个答案:

答案 0 :(得分:0)

只需将名称定义为第一列...

Pmatrix<- read.csv(text = ",NBAS,DNAH9,NRAS,NRAS,TP53,TP53,TP53,SCYL2,RNF19A
                       NBAS,1,0,0,0,0,0,0,0,0
                       DNAH9,0,1,0,0,0,0,0,0,0
                       NRAS,0,0,1,0,0,0,0,0,0
                       NRAS,0,0,0,1,0,0,0,0,0
                       TP53,0,0,0,0,1,0,0,0,0
                       TP53,0,0,0,0,0,1,0,0,0
                       TP53,0,0,0,0,0,0,1,0,0
                       SCYL2,0,0,0,0,0,0,0,1,0
                       RNF19A,0,0,0,0,0,0,0,0,1")

    sig_values <- which(Pmatrix==1, arr.in=TRUE)

    cbind.data.frame(colIDs = colnames(Pmatrix)[ sig_values[, 2] ],rowIDs = Pmatrix[,1][ sig_values[, 1] ])
#>   colIDs                        rowIDs
#> 1   NBAS                          NBAS
#> 2  DNAH9                         DNAH9
#> 3   NRAS                          NRAS
#> 4 NRAS.1                          NRAS
#> 5   TP53                          TP53
#> 6 TP53.1                          TP53
#> 7 TP53.2                          TP53
#> 8  SCYL2                         SCYL2
#> 9 RNF19A                        RNF19A