Skip to content

Crosstab from sql dump

3 messages · Alan Gibson, Dieter Menne

#
Hello all,,

Im looking for a simple function to produce a crosstab from a dumped
sql query result. Its very hard to produce crosstabs with most
databases (Access being the exception), so with the vast array of R
packages, Im sure this has to have already been implemented somewhere.

Examples are always good:
Take a csv dump like

name code
user1 100
user2 100
user1 200
user2 210
user1 300
user2 300

and produce

user1 user2
100 100
200 210
300 300

Thanks in advance for any suggestions,

Alan
#
Alan Gibson <alan.gibson <at> gmail.com> writes:
xtabs in stats is a simple solution, but package reshape is much more flexible
and comes with a good introduction.pdf.

Dieter
#
thanks for all the tips.

using 'unstack(read.table('/tmp/codes', header=TRUE))' gets me

  user1 user2
1   100   100
2   200   200
3   300   300

where /tmp/codes contains

code name
100 user1
200 user1
300 user1
100 user2
200 user2
300 user2

so unstack is exactly what i was looking for. for the record, it is
probably a good idea to specify which column is values and which is
group indicator like 'unstack(read.table('/tmp/crap', header=TRUE),
"code ~ name")' in case your columns are in a different order.

alan
On 12/30/06, Dieter Menne <dieter.menne at menne-biomed.de> wrote: