简体   繁体   中英

Changing Column names of multiple data frames using a for loop with data frames loaded into a List

I am trying to change the variable names in all data frames in a for loop. Any example of the data is:

df1 <- data.frame(
  Number = c(45,62,27,34,37,55,40),
  Day = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
df2 <- data.frame(
  Number = c(15,20,32,21,17,18,13),
  Day = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
df3 <- data.frame(
  Number = c(12,32,22,14,16,21,30),
  Day = c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun")

L <- list(df1,df2,df3)

My current attempt is:

for(i in L){
colnames(L) <- c("NewName1", "NewName2")
}

Which is not working, I do not understand why it is not working. Please let me know if someone can guide me in the right direction.

L <- lapply(L, function(x){
  colnames(x) <- c("NewName1", "NewName2")
  x
} )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM