2

I want to assign the first column as rownames of kirp.mut.

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

Traceback:

> 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. 

Dimensions:

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

Class:

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

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

Data:

> 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"))
melolilili
  • 239
  • 1
  • 8

2 Answers2

2

A tibble cannot have row names assigned. You could convert it to another format, such as a data frame, then assign row names. You can also do this tidyverse solution using column_to_rownames on your tibble without explicitly converting to another form, but it will do so internally and return a data.frame:

library(tidyverse)
library(dplyr)

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

See the technical documentation here on row names and tibbles

jpsmith
  • 11,023
  • 5
  • 15
  • 36
0

Convert to matrix, excluding 1st column, then assign rownames:

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

Or to a dataframe

#convert tibble to data.frame, then add rownames
df <- as.data.frame(kirp.mut[, -1])
rownames(df) <- kirp.mut$sample_id
zx8754
  • 52,746
  • 12
  • 114
  • 209