R: Rename Data Frame Columns
You can change the variable names of a data frame by using the colnames() function or by directly assigning values to the names attribute of the data frame.
For example, consider a data frame df with variables named var1 and var2.
df <- data.frame(var1 = c(1, 2, 3), var2 = c("A", "B", "C"))
To change the variable names var1 and var2 to new_var1 and new_var2 respectively, you can use the following method:
# 使用colnames()函数
colnames(df) <- c("new_var1", "new_var2")
# 或直接对names属性进行赋值操作
names(df) <- c("new_var1", "new_var2")
This will successfully change the variable names of the data frame.