I have 18 uniform dataframes and would like to change the column names of each of these dataframes to the same column names. Here is a working example for just 2 but I would like this to scale.
df1 <- data.frame("value1" = 1:10, "value2" = 1)
df2 <- data.frame("value1" = 1:10, "value2" = 2)
column.names <- c("new1", "new2")
df.list <- as.list(df1, df2)
So far I have tried both lapply and a for loop
df.list <- lapply(df.list, function(x) {
colnames(x) <- column.names
x
})
for (i in seq_along(df.list)) {
colnames(df.list[[i]]) <- column.names
}
but neither work and both produce the following error
Error in `colnames<-`(`*tmp*`, value = column.names) :
attempt to set 'colnames' on an object with less than two dimensions
Any help would be greatly appreciated.