My data looks as follows:
DT1 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,
2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,
2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,
2001, 2002, 2002, 2002), Municipality = c("Something", "Anything",
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything",
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything",
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything",
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything",
"Nothing"), Values = c(0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86,
0.85, 0.99, 0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99,
0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA,
-27L), class = c("tbl_df", "tbl", "data.frame"))
DT1_list <- DT1%>%
group_split(Province, Year)
I want to rename all the columns, using a vector as follows:
colnames <- c("newname1","newname2","newname3","newname4")
for (i in DT1_list) {
names(DT1_list)[[i]] <- colnames
}
The problem is that names(DT1_list)[[i]]
does not give column names but NULL
.
What is the correct way to do this?
EDIT:
I noticed that my question, was not a good enough representation of my actual problem (my apologies, I did not foresee the the issue). My actual problem is that I want to rename 3 out of 4 columns:
colnames <- c("newname1","newname2","newname3")
If I use the answers provided, the fourth column becomes NA
. Is there anyway to keep the other columns intact?