Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name the unnamed first column of a data.frame

Tags:

dataframe

r

I have a data frame that looks like this:

> mydf
                   val1     val2
hsa-let-7a         2.139890 -0.03477569
hsa-let-7b         2.102590  0.04108795
hsa-let-7c         2.061705  0.02375882
hsa-let-7d         1.938950 -0.04364545
hsa-let-7e         1.889000 -0.10575235
hsa-let-7f         2.264296  0.08465690

Note that from 3 columns only 2nd and 3rd are names. What I want to do is to name the first column (plus rename the 2nd and 3rd).

But why this command failed?

colnames(mydf) <- c("COL1","VAL1","VAL2");

What's the right way to do it?

It gave me:

Error in `colnames<-`(`*tmp*`, value = c("COL1", "VAL1", "VAL2" :
  'names' attribute [3] must be the same length as the vector [2]
like image 655
neversaint Avatar asked Apr 16 '13 07:04

neversaint


People also ask

How do I name the first column of a data frame?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.

How do you call unnamed columns in pandas?

There are situations when an Unnamed: 0 column in pandas comes when you are reading CSV file . The simplest solution would be to read the "Unnamed: 0" column as the index. So, what you have to do is to specify an index_col=[0] argument to read_csv() function, then it reads in the first column as the index.

How do I rename an unnamed zero?

The solution can be improved as data. rename( columns={0 :'new column name'}, inplace=True ) . There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' .


3 Answers

You could join the row names to the dataframe, like this:

mydf <- cbind(rownames(mydf), mydf)
rownames(mydf) <- NULL
colnames(mydf) <- c("COL1","VAL1","VAL2")

Or, in one step:

setNames(cbind(rownames(mydf), mydf, row.names = NULL), 
         c("COL1", "VAL1", "VAL2"))
#         COL1     VAL1        VAL2
# 1 hsa-let-7a 2.139890 -0.03477569
# 2 hsa-let-7b 2.102590  0.04108795
# 3 hsa-let-7c 2.061705  0.02375882
# 4 hsa-let-7d 1.938950 -0.04364545
# 5 hsa-let-7e 1.889000 -0.10575235
# 6 hsa-let-7f 2.264296  0.08465690
like image 90
Tom Avatar answered Sep 18 '22 12:09

Tom


this may also work in your case,

mydf <- cbind(rownames(mydf),mydf)
rownames(mydf) <- NULL
colnames(mydf) <- c(names(mydf)) #to not write all the column names

colnames(mydf)[1] <- "name.of.the.first.column" 
names(mydf)
like image 45
Seyma Kalay Avatar answered Sep 22 '22 12:09

Seyma Kalay


If one wants to use a tidyverse solution within his/her pipeline, this works

rownames_to_column(mydf, var = "var_name")

The function is contained in the tibble package

like image 28
Ric S Avatar answered Sep 18 '22 12:09

Ric S