Extract rows name in R


I have a data frame with several rows. I want to select the row's names of this data frame and put them into a new variable. The input example dataframe is as follows:
enter image description here

I want to extract the row's names of the first column (X) (ENSG000...). I tried to use the rownames() function:

genes_name <- as.character(rownames(dataframe))

enter image description here

It didn't extract the row names of X but the numbers.

Please could anyone help me?




That data.frame does not seem to have rownames. Your "rownames" are in fact the first column, named "X", accesible through dataframe$X, or dataframe[,1] or dataframe[,"X"]

I second the answer provided by @Papyrus above. Also, I have frequently run into this issue when reading external data in to R. If you want the first column of the table to be the row names of the data frame in R, you can set the row.names parameter to 1 when reading in the data. Hope you find this helpful!

You asked for rownames, R gave you numbers. Those are your rownames, not the contents of X. Usually, when you import into R, your rowname column should not have a column name too.

As mentioned already, you can specify row.names when importing the file, or you can always assign new rownames within R. Or, if you want the gene names, just get the contents of the column you want, row name or not.

before adding your answer.

Traffic: 2054 users visited in the last hour

Source link