0

First of all a disclaimer: I am a Stata user trying to fully make the "jump" to R (slowly, though).

I was making some easy crosstabs when I saw that one of them simply does not fit my console size. In Stata, using the same data and the same variables, the crosstab fits perfectly fine. However, in R everything becomes very messy.

The table is 10x10. A reproducible example below:

library(gmodels)
df  <- data.frame(id = 1:100, x1=sample(1:7, size=100,replace=TRUE), 
                  x2 = sample(0:10, size=100, replace=TRUE))

CrossTable(df$x1, df$x2, prop.r=TRUE, prop.t=TRUE, prop.c=TRUE)

The above table does not fit in my screen. I am not even interested in putting it in a LaTeX document yet; I just wanted to explore that crosstab. I am interested in using CrossTable as it produces what, to me, is a good-looking table with percentages (rows and columns) and totals in the margins. I do not know if this is a problem with CrossTable or if it is due to how my R installation displays/limits output. I tried doing the same in a Rmarkdown specifying with "options(widht=huge_number)" a bigger space, but this was to no avail.

Some other threads did not help me very much. I could not find anything directly related to my issue. Examples of threads:

How do I fit a very wide grid.table or tableGrob to fit on a pdf page?

Automatically adjust LaTeX table width to fit pdf using knitr and Rstudio

R-LaTeX Wide table

Any help will be greatly appreciated. I am also open to learning other packages that result in similar outputs. Thanks!

Community
  • 1
  • 1
YSC
  • 75
  • 2
  • 10
  • It only happened to me with the tiny window of RStudio. I just had to enlarge it and the table is OK. Did you try running the code with a fullscreen terminal? – Paulo MiraMor Jan 17 '17 at 17:57
  • Yeah, that was the first thing I tried before posting. This morning, when I got back to work, producing the table with an enlarged console screen did the trick... Might it be some sort of "cache" that prevented the table from "re-sizing"? – YSC Jan 18 '17 at 10:32

3 Answers3

0

Check out this threads: This for Latex pdf creation. It may help you. In order to get in the terminal, for checking columns, use str(columns), or you may use data.table package to view dataframe inside terminal window. Otherwise you can slice it step by step to see the dataframe. Let me know if this helps.

Community
  • 1
  • 1
BoyInDaBox89
  • 417
  • 5
  • 16
  • Thanks @Amit for your comment. The link you suggest is one of the listed in OP. It is somehow insightful but it really does not answer my question. I just browsed through the package `data.table` help page and could not see how it would help me. – YSC Jan 17 '17 at 17:40
0

The CrossTable call actually creates a list object with all the parts. It may be useful for inspection to examine those list objects.

x<-CrossTable(df$x1, df$x2, prop.r=TRUE, prop.t=TRUE, prop.c=TRUE)
x$t
   y
x   0 1 2 3 4 5 6 7 8 9 10
  1 2 1 2 0 1 3 1 1 2 1  1
  2 2 1 1 2 1 2 1 2 1 2  0
  3 2 2 2 2 0 1 3 2 2 1  2
  4 1 2 1 1 1 2 2 1 2 0  0
  5 0 0 0 1 4 2 1 0 1 1  0
  6 0 1 2 0 0 1 0 1 0 1  5
  7 0 2 0 3 1 1 1 1 2 5  1

round(x$prop.row,3)
   y
x       0     1     2     3     4     5     6     7     8     9    10
  1 0.133 0.067 0.133 0.000 0.067 0.200 0.067 0.067 0.133 0.067 0.067
  2 0.133 0.067 0.067 0.133 0.067 0.133 0.067 0.133 0.067 0.133 0.000
  3 0.105 0.105 0.105 0.105 0.000 0.053 0.158 0.105 0.105 0.053 0.105
  4 0.077 0.154 0.077 0.077 0.077 0.154 0.154 0.077 0.154 0.000 0.000
  5 0.000 0.000 0.000 0.100 0.400 0.200 0.100 0.000 0.100 0.100 0.000
  6 0.000 0.091 0.182 0.000 0.000 0.091 0.000 0.091 0.000 0.091 0.455
  7 0.000 0.118 0.000 0.176 0.059 0.059 0.059 0.059 0.118 0.294 0.059

etc... for the prop.col and prop.tbl variables.

akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
0

This is a work around, but it will give you the ability to see the the cross table as you stated.

  • First, in RStudio IDE, open up a new R Notebook.
  • Second, run the code below. The '''{r} ''' runs R code in the document (note that these are 3 back ticks NOT single quotes, it doesn't appear correctly if i code it the right way).

    '''{r}

    library(gmodels) df <- data.frame(id = 1:100, x1=sample(1:7, size=100,replace=TRUE), x2 = sample(0:10, size=100, replace=TRUE))

    CT <- CrossTable(df$x1, df$x2, prop.r=TRUE, prop.t=TRUE, prop.c=TRUE)

    View(CT)

    '''

  • Third: when you get/see the output of your crosstab, it will be as crazy looking as before. In the "top-right" corner of the output cell, you find 3 options "x" out, "<<" hide/unhide output, and "window with arrow" which is open in separate window. Click to pop out the window, then expand the window to full size. There is your cross tab.

Enjoy :)

  • I think this is the same output as {as.data.frame(CrossTable(df$x1, df$x2, prop.r=TRUE, prop.t=TRUE, prop.c=TRUE)} – akaDrHouse Jan 17 '17 at 21:24