How can we change the column names of a data frame in R?

In R language, you can use the colnames() function to change the column names of a data frame. The specific procedure is as follows:

  1. column names()
  2. Modify the obtained column names.
  3. names of columns

Here is a simple example:

# 创建一个数据框
df <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6))

# 输出数据框的列名
print(colnames(df))

# 修改列名
new_colnames <- c("Column1", "Column2")
colnames(df) <- new_colnames

# 输出修改后的列名
print(colnames(df))

The output is:

[1] "A" "B"
[1] "Column1" "Column2"

It can be seen that the original column names “A” and “B” have been successfully changed to “Column1” and “Column2”.

bannerAds