Here's some mock data that resembles the data I'm working with:
df1 <- data.frame(Date.Time..GMT.04.00 = c(1, 2, 3, 4, 5),
var1 = c('a', 'b', 'c', 'd', 'e'),
var2 = c(1, 1, 0, 0, 1))
df2 <- data.frame(Date.Time..GMT.05.00 = c(1, 2, 3, 4, 5),
var1 = c('a', 'b', 'c', 'd', 'e'),
var2 = c(1, 1, 0, 0, 1))
I put the data frames into a list so that I'll be able to apply a function to each of the data frames:
df.list <- list(df1, df2)
Here's the function that I source in:
change_date_name <- function(df){
colnames(df) <- sub("^Date.Time..GMT.0\\d.00", "Date_Time", colnames(df))
}
Now, I apply the function to the data frame list:
df.list <- lapply(df.list, change_date_name)
When I apply the function to the list, it successfully changes the name in each data frame, but it creates a new column in each data frame called "V1", which is a character column that contains the previous columns' names, causing the data to disappear. How can I apply the function to change the column name in each data frame without losing the data?
Using R version 3.5.1, Mac OS X 10.13.6