Search code examples
rexportdata-analysiscrosstabsummary

How to export crosstab to csv


I have a dataset, where I run this code:

source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")
crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")

And get this result:

               s2_nautos_nu      0      1     2+    Sum
s2_nautos_voor                                         
0                            19.89   3.31   0.05  23.25
1                             0.92  51.71   1.78  54.41
2+                            0.31   2.45  19.58  22.34
Sum                          21.11  57.47  21.42 100.00

However, when I try to save it in a csv using this code:

crosstabs_nautos <- crosstab(nautos, row.vars = "s2_nautos_voor", col.vars = "s2_nautos_nu", type = "t")
write.csv(crosstabs_nautos$table,"crosstabs_nautos.csv")

It gets saved with the type = f instead of t. In other words, it doesn't save the function, and I get this:

               s2_nautos_nu    0    1   2+  Sum
s2_nautos_voor                                 
0                            390   65    1  456
1                             18 1014   35 1067
2+                             6   48  384  438
Sum                          414 1127  420 1961 

...no matter which crosstab function I apply.

How can I get around this?


Solution

  • I'm guessing that the crosstabs function only stores the table as frequencies in an object of class crosstabs and then "prints" with desired type to the console with an associated S3 print.crosstabs function that checks for the type before "deciding" whether to print raw frequencies or percentages. If you only send the "table" leaf of the list structure to write.csv, then there is no opportunity to check the type. Perhaps you are expected to figure this out by experimentation or by reading the code. I cannot find any documentation of that set of functions at that academic website. If you have any links to documentation, then you should include them in your question.

    Also, most of the checks in the code are of thesort:

    if ( type="frequency") {   do something
    

    So they are written to be exact checks and the spelling would need to be exact rather than abbreviated. You could use capture.output to send the results of print.crosstabs to a file. It won't be csv delimited. However that might not be a problem since there don't appear to be commas in the output you are concerned about. Try

     capture.output( print(crosstabs_nautos, type = "frequencies") , 
                   file="crosstabs_nautos.txt")
    

    Or you could perhaps write.csv using the object crosstab_nautos$table/1961

    Edit: There's some code near the end of the crosstabs function that's making me think my guesses were not completely correct:

     #(b) tabular output [3 variants]
        result$table <- tbl  #Stores original cross-tab frequency counts without margins [class: table]
        result$crosstab <- crosstab #Stores cross-tab in table format using requested style(frequency/pct) and table margins (on/off)
                                    #[class: table]  
        result$crosstab.nosub <- t1  #crosstab with subtotals suppressed [class: dataframe; or NULL if no subtotals suppressed]  
        class(result) <- "crosstab"