How can the column names of a list be changed in R?

In R language, you can use the colnames() function to change the column names of a list (or data frame).

Here is an example:

# 创建一个列表
my_list <- list(a = 1:5, b = 6:10, c = 11:15)

# 查看列表的列名称
colnames(my_list)

# 将列名称改为新的名称
colnames(my_list) <- c("col1", "col2", "col3")

# 再次查看列表的列名称
colnames(my_list)

Running the above code will produce the following output:

NULL
[1] "col1" "col2" "col3"

In this way, you can use the colnames() function to change the column names of the list to new ones.

bannerAds