Search code examples
rexportcrosstab

how to export table/crosstab from r to excel while keeping the format as it is


I need to export a high dimension frequency table generated by crosstab() (Package: descr) on 2 rows and 2 column variables from R to excel without changing the pattern/format.

The crosstab() function is running with combination of other packages. Got from this tutorial: http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html

Note: The page obtained the crosstab function with: source("http://pcwww.liv.ac.uk/~william/R/crosstab.r"). Credit was given to the ctab() function in the "catspec" package. One would be expected to also obtain the associated print.crosstab function, because the result is given a class of "crosstab".

Specifically, I want the same out-put in excel as the high dimension table generated using crosstab() is displayed on R console.

install.packages("descr")
library(descr)
ID <- seq(1:177)
Age <- sample(c("0-15", "16-29", "30-44", "45-64", "65+"), 177, replace = TRUE)
Sex <- sample(c("Male", "Female"), 177, replace = TRUE)
Country <- sample(c("England", "Wales", "Scotland", "N. Ireland"), 177, replace = TRUE)
Health <- sample(c("Poor", "Average", "Good"), 177, replace = TRUE)
Survey <- data.frame(Age, Sex, Country, Health)
head(Survey)
crosstab_1 <- crosstab(Survey, row.vars = c("Age", "Sex"), col.vars = c("Health", "Country"), 
     type = "f", addmargins = FALSE)
print(crosstab_1)

I need to export to excel the crosstab format as it is comes out in console through print(crosstab_1) function.


Solution

  • I get an error with that code:

    Error in crosstab(Survey, row.vars = c("Age", "Sex"), col.vars = c("Health",  : 
      The 'indep' (independent variable) is missing. Please, consider using either CrossTable() or freq().
    

    I'm not a user of that package so decided against figuring out what caused the error. So I decided to show what I would have done to achieve what I think is your purpose. The table function in base-R is like the "crosstabs" function in SPSS. It generates a contingency table, a matrix-like object:

    with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )
    #-----
    
                   Average.England Good.England Poor.England
      0-15.Female                0            0            2
      16-29.Female               1            3            4
      30-44.Female               2            3            0
      45-64.Female               1            1            0
      65+.Female                 2            0            4
      0-15.Male                  1            3            1
      16-29.Male                 0            2            1
      30-44.Male                 2            3            1
      45-64.Male                 2            2            0
      65+.Male                   3            3            1
    
                   Average.N. Ireland Good.N. Ireland Poor.N. Ireland
      0-15.Female                   1               2               0
      16-29.Female                  1               0               1
      30-44.Female                  2               0               1
      45-64.Female                  1               1               2
    

    snipped the rest of the output since it will clearly not be amenable to easy processing with Excel (or in my case OpenOffice)

    Now expand the viewing console and repeat:

    options( width=300)
    with(Survey, table( interaction(Age, Sex), interaction(Health, Country) ) )
    

    Most of the data is not visible on hte right side of the display but that doesn't matter. Now select it with your cursor-mousing, copy-, then paste into a blank region of your spreadsheet. What should appear is a dialog that allows specifying that this is fixed format data and allows you to adjust the column separators easily. Fiddle with the columns and hit OK and you're done.

    You could also have used capture.output or sink to send this output to a file.

    Addendum: The output of the print.crosstabs function from the site listed below in the comments is:

    print(crosstab_1)
    #----------------------------
                 Health  Average                              Good                              Poor                          
                 Country England N. Ireland Scotland Wales England N. Ireland Scotland Wales England N. Ireland Scotland Wales
    Age   Sex                                                                                                                 
    0-15  Female               2          1        4     1       3          2        1     3       0          3        1     1
          Male                 1          0        1     0       0          2        1     1       1          1        1     3
    16-29 Female               1          1        1     3       1          1        2     2       0          2        0     1
          Male                 4          0        2     1       1          5        0     1       2          2        2     0
    30-44 Female               2          1        2     3       2          2        1     2       1          2        1     3
          Male                 1          2        3     1       1          3        3     3       2          0        3     1
    45-64 Female               1          0        1     3       3          0        4     1       0          0        1     0
          Male                 2          0        1     3       1          2        2     0       3          2        0     0
    65+   Female               1          1        3     0       1          1        1     3       3          6        1     0
          Male                 0          1        1     0       3          0        0     1       1          2        1     2
    

    Since is is text, you could import it into excel (after using copy-paste or the sink or capture.output functions) using the program's fixed width text import facilities. It's essentially the same format as R base ftable function would give you for a 3-way classification.