Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do a lot of basic manipulation of data, so here are two built-in functions ( <strong>transform</strong> , <strong>subset</strong> ) and one library ( <strong>sqldf</strong> ) that I use daily.</p> <h2>create sample sales data</h2> <pre><code>sales &lt;- expand.grid(country = c('USA', 'UK', 'FR'), product = c(1, 2, 3)) sales$revenue &lt;- rnorm(dim(sales)[1], mean=100, sd=10) &gt; sales country product revenue 1 USA 1 108.45965 2 UK 1 97.07981 3 FR 1 99.66225 4 USA 2 100.34754 5 UK 2 87.12262 6 FR 2 112.86084 7 USA 3 95.87880 8 UK 3 96.43581 9 FR 3 94.59259 </code></pre> <h2>use transform() to add a column</h2> <pre><code>## transform currency to euros usd2eur &lt;- 1.434 transform(sales, euro = revenue * usd2eur) &gt; country product revenue euro 1 USA 1 108.45965 155.5311 2 UK 1 97.07981 139.2125 3 FR 1 99.66225 142.9157 ... </code></pre> <h2>use subset() to slice the data</h2> <pre><code>subset(sales, country == 'USA' &amp; product %in% c(1, 2), select = c('product', 'revenue')) &gt; product revenue 1 1 108.4597 4 2 100.3475 </code></pre> <h2>use sqldf() to slice and aggregate with SQL</h2> <p>The <a href="http://code.google.com/p/sqldf/" rel="nofollow noreferrer">sqldf package</a> provides an SQL interface to R data frames</p> <pre><code>## recast the previous subset() expression in SQL sqldf('SELECT product, revenue FROM sales \ WHERE country = "USA" \ AND product IN (1,2)') &gt; product revenue 1 1 108.4597 2 2 100.3475 </code></pre> <p>Perform an aggregation or GROUP BY</p> <pre><code>sqldf('select country, sum(revenue) revenue \ FROM sales \ GROUP BY country') &gt; country revenue 1 FR 307.1157 2 UK 280.6382 3 USA 304.6860 </code></pre> <p>For more sophisticated map-reduce-like functionality on data frames, check out the <a href="http://crantastic.org/packages/plyr" rel="nofollow noreferrer">plyr</a> package. And if find yourself wanting to pull your hair out, I recommend checking out <a href="http://rads.stackoverflow.com/amzn/click/0387747303" rel="nofollow noreferrer">Data Manipulation with R</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload