Labels
25 June 2009
What my violin really sound (and look) like
19 June 2009
Lattice magic - top-to-bottom plotting order in xyplot
From R-help: Hi, Using library(lattice), is there any way to tell xyplot to plot panels top to bottom, then left to right (i.e. panels are appended vertically, then horizontally). as.table changes the plot direction from left-to-right then top-to-bottom, to right-to-left then bottom- to-top, but that's not quite what I want to do. Thanks Yan
Deepayan says:
> tmp.tr3 <- xyplot(y ~ x | a, data=tmp, as.table=TRUE)Another high level option is to change the rule determining how packets are chosen for a given panel in the layout.
print(tmp.tr3, packet.panel = function(layout, row, column, ...) { layout <- layout[c(2, 1, 3)] packet.panel.default(layout = layout, row = column, column = row, ...) })This effectively transposes the layout, which (along with as.table=TRUE) is what you want.
-Deepayan
15 June 2009
Obfuscated SQL
--here's the magic: v_sourcedat --matches source files to datums create or replace view v_sourcedat as select unit_id||well_num as well, datum_m, gw.sn, source from v_loggerdat wd, ( SELECT tmp.sn, min(tmp."time")::date AS start, tmp.source FROM tmp_gwdata tmp GROUP BY tmp.source, tmp.sn) gw where gw.sn = wd.sn::int and gw.source not in (select source from v_sourcetowell) union select unit_id||well_num as well, datum_m, gwo.sn, source from v_loggerdat wd, (select * from v_sourcetowell) gwo where gwo.well = wd.unit_id||well_num order by well; -- using v_sourcedat, as above -- the final data create or replace view v_gwdata_4hr as SELECT datum.well, datum.sn, gw."time", gw.dtw::numeric(10,4) - datum.datum_m::numeric(10,4) AS dtw, gw.dtwsd, gw.temp, gw.tempsd FROM v_sourcedat datum JOIN gwdata_4hr gw using (source) WHERE gw.temp <> 3::numeric AND gw.dtw > (-0.5) AND gw.dtw < 4.5 AND gw."time" < '2010-01-01 00:00:00'::timestamp without time zone ORDER BY datum.well, datum.sn, gw."time";
12 June 2009
flying standby
A brief list of miraculous events proceed:
1. Megan graciously and unexpectedly offers to drop me off at the god-aweful hour of 5am.
2. My alarm fails to wake me at the even more god-aweful hour of 4:30, but a gate-change triggers a call from Southwest at 5:20am. I'm obviously late.
3. There's leftovers in BAM's fridge, and megan spots me a V8.
4. At security, i'm shuttled into the pilot/express line. I fly through security despite an irrationally long 5:40am "normal" line.
5. I don't feel like a total zombie...
6. Pressure-induced condensation vortex from turbelence in the streamline off the wing during descent braking.
7. belgian waffle with real butter
8. getting on the plane from denver to baltimore.
it's shaping up to be areally nice day. i'm sure i'll keel over eventually, in a safe place...
06 June 2009
Postgresql Poetry? Aggregate median with PL/R
CREATE OR REPLACE FUNCTION median(vals numeric[]) RETURNS float AS ' median(vals) ' LANGUAGE 'plr' STRICT; CREATE AGGREGATE median (numeric) ( sfunc = array_append, stype = numeric[], initcond = '{}', finalfunc = median ); --And usage select sn, median(dtwm), count(dtwm), date_trunc('day', timestamp) as day from gwdata where timestamp < '2003-01-01' group by sn, day limit 5; -- sn | median | count | day -----+--------+-------+------------------------ -- 5526 | 1.59 | 24 | 2002-12-26 00:00:00-07 -- 5539 | 1.7605 | 24 | 2002-02-27 00:00:00-07 -- 5522 | 0.737 | 24 | 2001-10-03 00:00:00-07 -- 5517 | 0.96 | 24 | 2001-11-05 00:00:00-07 -- 5513 | 1.3855 | 24 | 2001-09-07 00:00:00-07 -- Time: 247.126 ms -- Here's the PL/Perl median() function. -- Much less straightforwards, included for historical interest BEGIN { strict->import(); } my ($tname,$cname) = @_; my $SQL = "SELECT count($cname) AS t FROM $tname"; my $rc = spi_exec_query($SQL); my $total = $rc->{rows}[0]{'t'}; $total < offset =" ($total-1)/2;" sql = "SELECT $cname AS median FROM $tname ORDER BY $cname OFFSET $offset LIMIT 1" sql = "SELECT avg($cname) AS median FROM (SELECT $cname FROM $tname ORDER BY $cname OFFSET $offset LIMIT 2 ) AS foo" rc =" spi_exec_query($SQL);">{rows}[0]{median};