0

I have a vector of column names that I pulled from a data frame:

x = names(myDataSet)

I want to build tables for data exploration by passing each column name into a for loop and generate each table against a specific named column 'verdict' separately. I have tried the following:

for (i in x){
  table(myDataSet$i, myDataSet$verdict)
}

and

for (i in x){
  table(myDataSet[[i]], myDataSet$verdict)
}

But nothing has worked. Any suggestions?

user2722670
  • 39
  • 1
  • 1
  • 7
  • you have to use eval function - or basically instead of addressing to DF like that, find the index and use index instead – Areza May 26 '21 at 13:10
  • Does this answer your question? [Dynamically select data frame columns using $ and a character value](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value) – Areza May 26 '21 at 13:11
  • 1
    What did not work? It seems to me that the second solution should work , but you need to use `print` to actually print the result when you are inside a for-loop – J.C.Wahl May 26 '21 at 13:12
  • @J.C.Wahl, that was it! Thank you. – user2722670 May 26 '21 at 13:16

0 Answers0