set.seed(0) knitr::opts_chunk$set(echo = TRUE) suppressMessages(library("frab")) suppressMessages(library("sqldf"))
knitr::include_graphics(system.file("help/figures/frab.png", package = "frab"))
One unexpectedly difficult challenge for me developing the frab
package is search engine discoverability. Searching for phrases such
as "add two tables" or "combine two tables" returns a whole bunch of
SQL hits, not the frab
package. Part of the problem is that the
word "table" means different things to the SQL and R communities.
Here I demonstrate that frab
functionality [at least for
one-dimensional tables] can be replicated using SQL constructs.
Consider two simple frab
objects and their sum:
(a <- rfrab()) (b <- rfrab()) a+b
We may coerce a
and b
to a dataframe and combine with rbind()
:
(ab_df <- rbind(as.data.frame(a),as.data.frame(b)))
However, object ab_df
contains repeated key
entries. To frabify
this, we need to use the group by
SQL construct:
aplusb <- sqldf(' SELECT key, sum(value) AS value FROM ab_df GROUP BY key ') aplusb
(Note that the above construction would require a little polishing for
production use as zeros are not removed automatically). However, the
resulting data frame can be coerced back to frab
form:
as.frab(aplusb)
It is straightforward to verify that the two methods give identical results:
as.frab(aplusb) == a+b
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.