I am trying to select column dynamically from a dataframe. Say I have a vector of column names and need to pass that to the dataframe
df_test$c1$c2$c3
Here c2 is what I am trying to pass dynamically. I found a similar question in stackoverflow Dynamically select data frame columns using $ and a vector of column names , but this only talks about df_test$c1 and passing c1 dynamically.
Adding reproducible code
dfList <- split(mtcars, mtcars$cyl)
# the following list the columns mpg,
dfList$`8`$mpg
dfList$`6`$mpg
dfList$`4`$mpg
#Here I am trying to add 8 ,6 and 4 to a list
cols <- c("`8`", "`6`","`8`")
#and then pass it to
dfList$cols[1]$mpg
Is there any way to achieve this?