SoFunction
Updated on 2025-03-01

R language - How to define column names of data frames

1. When defining a data frame, define the column name:

For example:

a<-c(2,23,45,6,7,1,6,7)                 
b<-c(4,6,1,2,5,66,10,2)                
df<-(a,b)

At this time, the column names in the data frame df are a and b respectively

It can also be as follows:

df<-(a1=a,b1=b)

The column names at this time are a1 and b1

2. Modify the name of the column in the data frame

If you want to modify the column name in the data frame, you can use the name function to modify it

For example:

names(df)<-c("a2","b2")

At this time the column name will become a2, b2

3. Modify the row name in the data frame

The rows in the data frame can also be defined and modified to the desired name, which can be passed (df)<-c("c","d")

Supplement: R language extracts row and column names of data frames

Many data in R language are in the form. Based on the data matrix, a row is added above to represent the column names of each column, and a row is added to the left to represent the row names of each row. If you want to obtain the contents of the row name and column name, you only need to use the following two functions, assuming that the data variable is data,

rownames(data)    # Return the line namecolnames(data)    # Return column name

The above two functions will return the row name and column name in the form of a list. I hope it can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.