如何将R中的第一列指定为行名?[duplicate]

bxfogqkk  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(342)
    • 此问题在此处已有答案**:

Convert the values in a column into row names in an existing data frame(5个答案)
3天前关闭。
我想将第一列指定为行名称kirp.mut

rownames(kirp.mut) <- kirp.mut[,1]
kirp.mut[,1] <- NULL

追溯:

> rownames(kirp.mut) <- kirp.mut[,1]
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
In addition: Warning message:
Setting row names on a tibble is deprecated.

尺寸:

> dim(kirp.mut)
[1]  283 8654

类别:

> class(kirp.mut)
[1] "tbl_df"     "tbl"        "data.frame"

 typeof(kirp.mut)
[1] "list"

数据:

> dput(kirp.mut[1:10,1:10])
structure(list(sample_id = c("TCGA-2Z-A9J1-01A-11D-A382-10", 
"TCGA-B9-A5W9-01A-11D-A28G-10", "TCGA-GL-A59R-01A-11D-A26P-10", 
"TCGA-2Z-A9JM-01A-12D-A42J-10", "TCGA-A4-A57E-01A-11D-A26P-10", 
"TCGA-BQ-7044-01A-11D-1961-08", "TCGA-HE-7130-01A-11D-1961-08", 
"TCGA-UZ-A9Q0-01A-12D-A42J-10", "TCGA-HE-A5NI-01A-11D-A26P-10", 
"TCGA-WN-A9G9-01A-12D-A36X-10"), NBPF1 = c(1, 0, 0, 0, 0, 0, 
0, 0, 0, 0), CROCC = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), SF3A3 = c(1, 
0, 0, 0, 0, 0, 0, 0, 0, 0), GUCA2A = c(1, 0, 0, 0, 0, 0, 0, 0, 
0, 0), RAVER2 = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), ACADM = c(1, 
0, 0, 0, 0, 0, 0, 0, 0, 0), PDE4DIP = c(1, 0, 0, 0, 0, 0, 0, 
0, 0, 0), NUP210L = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), NCF2 = c(1, 
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))
rqqzpn5f

rqqzpn5f1#

tibble不能指定行名称。您可以将其转换为其他格式,如数据框,然后指定行名称。您也可以在tibble上使用column_to_rownames执行此tidyverse解决方案,而无需显式转换为其他格式,但它将在内部执行此操作并返回data.frame

library(tidyverse)
library(dplyr)

kirp.mut <- kirp.mut %>% 
  column_to_rownames(var = "sample_id")

有关行名称和tibble,请参见技术文档here

zbsbpyhn

zbsbpyhn2#

转换为矩阵(不包括第1列),然后指定行名称:

m <- as.matrix(kirp.mut[, -1])
rownames(m) <- kirp.mut$sample_id

或到 * Dataframe *

#convert tibble to data.frame, then add rownames
df <- as.data.frame(kirp.mut[, -1])
rownames(df) <- kirp.mut$sample_id

相关问题