0

I have two lists and a dataframe. The columns in the dataframe have the same names as the entries in the list. The dataframe has other columns as well, other than the ones specified in the lists

category.list <- c('Reserve_Book','choicepriv_and_points','Latency_freeze_load','signin','gift_card','mystery_gift','credit_card','call_support','account')
crosstab.list <- c('browser','OS','Device','comment_cat','comment_focus','recommend')

Now, how do I iterate through the elements in the list and use them to access the dataframe columns? Below is the code, I am trying but I am getting errors while trying to access the dataframe column via the iterator variable.

for (i in category.list){
  for (j in crosstab.list){  
  ftable(dataframe[j]~dataframe[i])
  }
}
zx8754
  • 52,746
  • 12
  • 114
  • 209
tjt
  • 620
  • 2
  • 7
  • 17
  • An [example of your data](http://stackoverflow.com/a/5963610/2005219) would be helpful, as well as the text of your error – Edward R. Mazurek May 25 '16 at 19:40
  • 1
    @EdwardR.Mazurek agreed, but in this case it's a basic typo in the column selection – kdopen May 25 '16 at 19:44
  • 2
    Use `[[`, i.e. `dataframe[[j]]`. There may also be more R-idiomatic approaches than the nested for loop – talat May 25 '16 at 19:47
  • Sorry, [[ is not giving an error , not showing any output either – tjt May 25 '16 at 21:11
  • Output is automatically hidden in a for loop. If you want it to print, then `print()` it. See `for (i in 1:4) i` vs `for (i in 1:4) print(i)`. – Gregor Thomas Oct 12 '16 at 21:18

1 Answers1

0

Specifically to your question, your dataframe references need to specify both which columns are desired and which rows.

ftable(dataframe[j]~dataframe[i])

needs to be

ftable(dataframe[,j]~dataframe[,i])

Note the addition of commas

kdopen
  • 8,032
  • 7
  • 44
  • 52
  • I added the commas, but it is resulting in nothing. No error as well as no output – tjt May 25 '16 at 19:46
  • 1
    Yep, doesn't do much for me either :) But that wasn't what your question was about. My answer just addresses the R errors you were getting – kdopen May 25 '16 at 19:47
  • 1
    What result are you expecting? your are not saving results in your `for` loop anywhere, neither you print them. – Bulat May 25 '16 at 21:40