Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The "database approach" is to keep a separate table (a data.frame) for your product keys definitions. It makes even more sense since you say your product keys translate into not only a brand, but also a size:</p> <pre><code>product.keys &lt;- read.table(textConnection(" product brand size 1 Tylenol small 2 Tylenol medium 3 Tylenol large 4 Advil small 5 Advil medium 6 Advil large 7 Bayer small 8 Bayer medium 9 Bayer large 10 Generic small 11 Generic medium 12 Generic large "), header = TRUE) </code></pre> <p>Then, you can join your data using <code>merge</code>:</p> <pre><code>merge(dat, product.keys, by = "product") # product brand size # 1 1 Tylenol small # 2 3 Tylenol large # 3 4 Advil small # 4 5 Advil medium # 5 5 Advil medium # 6 5 Advil medium # 7 6 Advil large # 8 7 Bayer small # 9 7 Bayer small # 10 8 Bayer medium # 11 9 Bayer large # 12 9 Bayer large # 13 9 Bayer large # 14 10 Generic small # 15 10 Generic small # 16 11 Generic medium # 17 11 Generic medium # 18 11 Generic medium # 19 11 Generic medium # 20 11 Generic medium </code></pre> <p>As you notice, the order of the rows is not preserved by <code>merge</code>. If this is a problem, the <code>plyr</code> package has a <code>join</code> function that does preserve the order:</p> <pre><code>library(plyr) join(dat, product.keys, by = "product") # product brand size # 1 11 Generic medium # 2 11 Generic medium # 3 9 Bayer large # 4 9 Bayer large # 5 6 Advil large # 6 1 Tylenol small # 7 11 Generic medium # 8 5 Advil medium # 9 7 Bayer small # 10 11 Generic medium # 11 5 Advil medium # 12 11 Generic medium # 13 4 Advil small # 14 3 Tylenol large # 15 10 Generic small # 16 7 Bayer small # 17 10 Generic small # 18 5 Advil medium # 19 9 Bayer large # 20 8 Bayer medium </code></pre> <p>Finally, if your tables are large and speed is an issue, consider using data.tables (from the <code>data.table</code> package) instead of data.frames.</p>
 

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