I am trying to subset multiple dataframes that are contained in a list based on strings that are contained in another dataframe.
list.df <- list(
 df.1 = data.frame(LM = c(1:10), LS = c(1:10), PL = c(1:10)), 
 df.2 = data.frame(XY = c(1:10), FE = c(4:13), OI = c(1:10)), 
 df.3 = data.frame(IL = c(1:10), KU = c(9:18), TS = c(1:10)))
df.4 <- data.frame(df.1 = c("LM", "PL", NA), df.2 = c("FE", NA, NA), 
 df.3 = c("IL", "KU", "TS"))
I want all my dataframes to look like this in the end:
df.1_sub <- subset(list.df[["df.1"]], select = 
   colnames(list.df[["df.1"]]) %in% df.4$df.1)
I will have to do this for around 50 datasets and was wondering whether there was a way of writing a loop to do this for all the datasets at once.
I have tried using lapply and for loops but was so far unsuccessful. I am new to using lists in R and would appreciate any help! This is my first time posting on stack overflow so please let me know if my post isn't appropriate,
df.2_subit would just be theFEcolumn, correct? Anddf.3_subwould be a 10x3 dataframe consisting of columnsIL,KU, andTS?