Matías de Felipe Matías de Felipe - 7 months ago 24
R Question

How to name a column in a dataframe when it does not has name?

I have a dataframe conformed for two columns, the first (column = 0) and the second (column = 1). I am trying to name zero column but I am into some difficulties. When I apply the

function, its output gives me only one variable (column 1) instead of two (column 0 and 1).

For more comprehension column 1 is "Variable" and column 0 must be "Identification" but I can not reach to perform it.

Below is the
and the srcipt that I am using but with non positive results (see below):

A.1 1178
A.2 1187
A.3 1190
B.1 983
B.2 1002
B.3 977
C.1 1056
C.2 1027
C.3 1043
D.1 974
D.2 1003
D.3 986


For more information, both scripts run effectively but only the second assigns the name to the column. In addition, this table is stemmed from function
sum ()
with the use of "Identification" as a splitting criterion.


In R, the indexing starts with 1. The column we were talking about is just row.names. If we want to have 2 columns, we can create a new column 'Identification' from the rownames using base R.

df1<-  `row.names<-`(transform(dataexample, Identification = row.names(df1))[2:1], NULL)

Of course, it can be done in two steps like the one showed in the comments under OP's post. But, I posted a one-liner in case it is useful.

If we are using packages, one option with data.table would be to change the default setting of keep.rownames from FALSE to TRUE.

setnames(setDT(dataexample, keep.rownames = TRUE), 1, "Identification")