3

I need to save a number of tables in a single CSV file and am having difficulty seeing how to retain dimension names. I searched SO and the closest I found was: How to get dimnames in xtable.table output? The problem he has with xtable is the problem I've got with write.table – dimnames exist in the table (and prop.table and ftable as well if I use that) but get dropped by write.table. I'm using write.table not write.csv for append=T.

The dataset is from a survey and the aim is to create the complete set of crosstabs, with labelled axes. In this case, actual row/column labels are not important, only dimension labels. I'm new to R, so hope I haven't missed something obvious.

d<-read.csv('dataset.csv')  # dataset with column headings, no row labels
cat('BEGIN\n',file='xtabs.csv')
for (i in 1:ncol(d)) {
    for (j in 1:ncol(d)) {
        cat(paste('\ni=',i,' j=',j,'\n'),file='xtabs.csv',append=T)
        t<-table(d[,i],d[,j],dnn=c(names(d[i]),names(d[j])))
        pt<-prop.table(t,1)
        write.table(pt,'xtabs.csv',sep=',',dec='.',row.names=F,col.names=F,append=T)
        print(pt)   # shows dimnames in the console as expected
    }
}
Community
  • 1
  • 1
Graham Jones
  • 251
  • 2
  • 9
  • 7
    Use the arguments you've supplied as false. `col.names=TRUE, row.names=TRUE`... – Justin Jan 10 '13 at 15:46
  • Did you read `?write.table` before you started searching SO? – Joshua Ulrich Jan 10 '13 at 15:51
  • 1
    I spent quite a while on ?write.table and others, and have tried every combination for row.names, col.names. It's not the row/column names I want though, but the dimension names, by which I mean the column headings. E.g. in the crosstab of "Q5:age" against "Q8:height" I want the label "Q5:age" to appear alongside the rows and "Q8:height" to appear above the columns. Axis labels in other words. This is what print(pt) shows, but not write.table(pt...). Could be something simple that I'm overlooking in my newness, but I have searched a fair bit so far. – Graham Jones Jan 10 '13 at 16:04
  • 3
    That's not what `write.table` does. This is potentially confusing because `write.table` really has nothing to do with `table` objects directly. – joran Jan 10 '13 at 16:10
  • To be clear, the above is just how the code was at the time I posted. Setting row.names=T, col.names=T does not produce the desired behaviour, but I should have said originally that it does produce the warnings: "appending column names to file". I'll investigate that further. – Graham Jones Jan 10 '13 at 16:20
  • To be perfectly honest, my first reaction to this is that a text file isn't what I'd consider a convenient format for tabular data like this, particularly if you want complicated formatting. – joran Jan 10 '13 at 16:24
  • I think a large part of the problem here is Graham's improper understanding of what a dataframe is and how it is different than an R "table" (which is a matrix). The code is a complete hash of those two distinct concepts. It makes no sense to use the function `table` when only two length-1 arguments are given. – IRTFM Jan 10 '13 at 16:52

1 Answers1

4

Try this:

tbl <- with(warpbreaks, table(wool, tension))
pt <- prop.table(tbl)
write.ftable(ftable(pt),file = "~/Desktop/table.csv", sep = ",",
             quote = FALSE)

I'm possibly abusing ftables here, which are intended for multi-dimensional tabular data (i.e. more than two variables). But it's the only thing I've found that will write the table to a text file with (seemingly) the formatting you want.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    +1 Nice answer, esp now the OP has made it clear what is wanted. Wasn't aware of `ftable()`. The edit tried to make the example a bit closer to what the OP had originally and output a CSV. – Gavin Simpson Jan 10 '13 at 16:59
  • Thanks joran, that's just what I was looking for. unfortunately write.ftable doesn't support the sep argument so it doesn't do CSV, but I'll find a workaround. It seems this question wasn't quite as straightforward as I originally assumed. – Graham Jones Jan 12 '13 at 00:56