I am trying to write a loop in R that will select the same specific column names in multiple data frames. The below code seems to achieve the desired output but the dataframes are not re-assigned. How can I re-assign the dataframes df1 and df2 to the lapply output?
col1 <- c(1,2,3,4)
col2 <- c("A","B","C","D")
col3 <- c(4,15,"BLANK","ZZ")
df1 <- data.frame(col1,col2, col3)
col1 <- c(500,546,47,87)
col2 <- c("E","L","J","U")
col3 <- c(6,10,"F","R")
df2 <- data.frame(col1,col2, col3)
df_list <- list(df1,df2)
lapply(df_list,function(x) {x<- x %>% select("col1","col2")} )