I have multiple dataframes saved in a list object. They share the same two column names. I'd like to rename the second column to the name of the dataframe.
Example Data:
df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40)
df3 <- data.frame(A = 31:40, B= 41:50)
df4 <- data.frame(A = 51:80, B = 61:70)
listDF <- list(df1, df2,df3, df4)
I'm trying to use lapply to rename the second column to match the name of the dataframe.
# trying to rename second column after the element of the list they're located in
listDF_2 <- lapply(names(listDF), function(x) setNames(listDF[[x]], x) )