You can also use setNames if you want to replace all columns
df1 <- data.frame(A = 1:10, B= 11:20) df2 <- data.frame(A = 21:30, B = 31:40) listDF <- list(df1, df2) new_col_name <- c("C", "D") lapply(listDF, setNames, nm = new_col_name)
If you need to replace only a subset of column names, you can use @Jogo's solution
lapply(listDF, function(df) { names(df)[-1] <- new_col_name[-ncol(df)] df })
Last point, in R there is a difference between a: b - 1 and a: (b - 1)
1:10 - 1 ## [1] 0 1 2 3 4 5 6 7 8 9 1:(10 - 1) ## [1] 1 2 3 4 5 6 7 8 9
EDIT
If you want to change the column names of data.frame in a global environment from a list, you can use list2env , but I'm not sure if this is the best way to achieve what you want. You also need to change your list and use a named list, the name must match the name data.frame , which you need to replace.
listDF <- list(df1 = df1, df2 = df2) new_col_name <- c("C", "D") listDF <- lapply(listDF, function(df) { names(df)[-1] <- new_col_name[-ncol(df)] df }) list2env(listDF, envir = .GlobalEnv) str(df1)
source share