<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/mabbles-the-missing-piece-of-a-unified-theory-of-tidyness/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://4dpiecharts.com/2017/04/20/mabbles-the-missing-piece-of-a-unified-theory-of-tidyness/"> R – 4D Pie Charts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R programming has seen a big shift in the last couple of years. All those packages that RStudio have been creating to solve this or that problem suddenly started to cohere into a larger ecosystem of packages. Once it was given a name, the tidyverse, it became possible to start thinking about the structure of the ecosystem and how packages relate to each other and what new packages were needed. At this point, the tidyverse is already the dominant ecosystem on CRAN. Five of the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/trends">top ten downloaded packages</a> are tidyverse packages, and most of the packages in the ecosystem are in the top one hundred.</p>
<p>As the core tidyverse packages like dplyr mature, the most exciting developments are its expansion into new fields. Notably <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/tidytext">tidytext</a> is taking over text mining, <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/tidyquant">tidyquant</a> is poised to conquer financial time analyses, and <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/sf">sf</a> is getting the spatial stats community excited.</p>
<p>There is one area that remains stubbornly distinct from the tidyverse. Bioconductor dominates biological research, particularly ‘omics fields (genomics, transcriptomics, proteomics, and metabolomics). Thanks to the heavy curation of package by Bioconductor Core, the two and a half thousand packages in the Bioconductor repository also form a coherent ecosystem.</p>
<p>In the same way that the general theory of relativity and quantum mechanics are incredibly powerful by themselves but are currently irreconcilable when it come to <a target="_blank" rel="nofollow" href="https://www.britannica.com/science/unified-field-theory">thinking about gravity</a>, the tidyverse and Bioconductor are more or less mutually exclusive ecosystems of R packages for data analysis. The fundamental data structure of the tidyverse is the data frame, but for Bioconductor it is the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/Biobase/topics/ExpressionSet"> ExpressionSet</a>.</p>
<p>If you’ve not come across ExpressionSets before, they essentially consist of a data frame of feature data, a data frame of response data, and matrix of measurements. This data type is marvelously suited to dealing with data from ‘omics experiments and has served Bioconductor well for years.</p>
<p>However, over the last decade, biological datasets have been growing exponentially, and for many experiments it is now no longer practical to store them in RAM, which means that an ExpressionSet is impractical. There are some very clever workarounds, but it strikes me that what Bioconductor needs is a trick from the tidyverse.</p>
<p>My earlier statement that the data frame is the fundamental data structure in the tidyverse isn’t quite true. It’s actually the tibble, an abstraction of the data frame. From a user point of view, tibbles behave like data frames with a slightly nicer print method. From a technical point of view, they have one huge advantage: they don’t care where their data is. tibbles can store their data in a regular data.frame, a data.table, a database, or on Spark. The user gets to write the same dplyr code to manipulate them, but the analysis can scale beyond the limits of RAM.</p>
<p>If Bioconductor could have a similar abstracted ExpressionSet object, its users and developers could stop worrying about the rapidly expanding sizes of biological data.</p>
<p>Swapping out the data frame parts of an ExpressionSet is simple – you can just use tibbles already. The tricky part is what to do with the matrix. What is needed is an object that behaves like a matrix to the user, but acts like a tibble underneath.</p>
<p>I call such a theoretical object a <em>mabble</em>.</p>
<p>Unfortunately, right now, it doesn’t exist. This is where you come in. I think that there is plenty of fame and fortune for the person or team that can develop such an object, so I urge you to have a go.</p>
<p>The basic idea seems reasonably simple. You store the mabble as a tibble, with three columns for row, column, and value. Here’s a very simple implementation.</p>
<pre>mabble &lt;- function(x, nrow = NULL, ncol = NULL) {
  # Decide on dimensions
  n &lt;- length(x)
  if(is.null(nrow)) {
    if(is.null(ncol)) {
      # Default to column vector
      nrow &lt;- n
      ncol &lt;- 1
    } else { # only ncol known
      nrow &lt;- n / ncol
      assert_all_are_whole_numbers(nrow)
    }
  } else {
    if(is.null(ncol)) { # only nrow known
      nrow &lt;- n / ncol
      assert_all_are_whole_numbers(ncol)
    } else { # both known
      # Not allowing recycling for now; may change my mind later
      assert_all_are_equal_to(nrow * ncol, length(x))
    }
  }

  m &lt;- tibble(
    r = rep.int(seq_len(nrow), times = ncol),
    c = rep(seq_len(ncol), each = nrow),
    v = x
  )
  class(m) &lt;- c(&quot;mbl&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;)
  m
}
</pre>
<p>Then you need a print method so it displays like a matrix. Here’s a simple solution, though ideally only a limited number of rows and column would be displayed.</p>
<pre>as.matrix.mbl &lt;- function(x) {
  reshape2::acast(x, r ~ c, value.var = &quot;v&quot;)
}

print.mbl &lt;- function(x) {
  print(as.matrix(x))
}
(m &lt;- mabble(1:12, 3, 4))
##   1 2 3  4
## 1 1 4 7 10
## 2 2 5 8 11
## 3 3 6 9 12
</pre>
<p>The grunt work is to write methods for all the things that matrices can do. Transposing is easy – you just swap the r and c columns.</p>
<pre>t.mbl &lt;- function(x) {
  x %>% 
    dplyr::select(r = c, c = r, v)
}
t(m)
##    1  2  3
## 1  1  2  3
## 2  4  5  6
## 3  7  8  9
## 4 10 11 12
</pre>
<p>There are a lot of things that need to be worked out. Right now, I have no idea how you implement linear algebra with a mabble. I don’t have time to make this thing myself but I’d be happy to advise you if you are interested in creating something yourself.</p>
<p> Tagged: <a target="_blank" rel="nofollow" href="https://4dpiecharts.com/tag/bioconductor/">Bioconductor</a>, <a target="_blank" rel="nofollow" href="https://4dpiecharts.com/tag/programming/">programming</a>, <a target="_blank" rel="nofollow" href="https://4dpiecharts.com/tag/r/">r</a>, <a target="_blank" rel="nofollow" href="https://4dpiecharts.com/tag/tidyverse/">tidyverse</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/659/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/659/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://4dpiecharts.com/2017/04/20/mabbles-the-missing-piece-of-a-unified-theory-of-tidyness/"> R – 4D Pie Charts</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/aeUHObOdCLc"></a></div>