1

What is the recommended way to read a csv file and convert the content of a two-way table with one predictable category variable for rows and one category variable for columns into a dataframe?

In the example below the row category variable has values R1 and R2, and the column category variable has values C1 and C2.

> system('cat twt.csv')
,C1,C2
R1,1,2
R2,3,4

> data.frame(as.table(as.matrix(read.csv('twt.csv',row.names=1))))
  Var1 Var2 Freq
1   R1   C1    1
2   R2   C1    3
3   R1   C2    2
4   R2   C2    4

I believe there are better ways than this.

Heikki
  • 2,214
  • 19
  • 34
  • 2
    Actually using the `as.table.data.frame` function is considered pretty good code. Converting "wide" data to "long" data has many solutions and you may find that the `reshape2` pkg or one of the newer tidyverse packages aremore to your taste. – IRTFM Dec 18 '17 at 19:36
  • It's really just [a wide-to-long transformation](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format), so you can use `tidyr::gather` or whatnot, e.g. `library(tidyr); read.csv(text = ',C1,C2\n R1,1,2\n R2,3,4') %>% gather(C, Freq, -X)` – alistaire Dec 18 '17 at 19:36
  • better ways? Not really. You're dealing with some pretty efficient code. You could consider `library(broom); tidy(as.table(as.matrix(read.csv(text = x, row.names = 1))))` but I think just about anything else you try will probably be slower and no more clear. – Benjamin Dec 18 '17 at 19:42
  • Do s search: `[r] as.data.frame.table` and read examples. Here's one from the Archives: https://stackoverflow.com/questions/13791657/generate-data-frame-from-array-for-logistic-regression/35747749#35747749 – IRTFM Dec 18 '17 at 19:44

0 Answers0