Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is `[` better than `subset`?
    primarykey
    data
    text
    <p>When I need to filter a data.frame, i.e., extract rows that meet certain conditions, I prefer to use the <code>subset</code> function:</p> <pre><code>subset(airquality, Month == 8 &amp; Temp &gt; 90) </code></pre> <p>Rather than the <code>[</code> function:</p> <pre><code>airquality[airquality$Month == 8 &amp; airquality$Temp &gt; 90, ] </code></pre> <p>There are two main reasons for my preference:</p> <ol> <li><p>I find the code reads better, from left to right. Even people who know nothing about R could tell what the <code>subset</code> statement above is doing.</p></li> <li><p>Because columns can be referred to as variables in the <code>select</code> expression, I can save a few keystrokes. In my example above, I only had to type <code>airquality</code> once with <code>subset</code>, but three times with <code>[</code>.</p></li> </ol> <p>So I was living happy, using <code>subset</code> everywhere because it is shorter and reads better, even advocating its beauty to my fellow R coders. But yesterday my world broke apart. While reading the <code>subset</code> documentation, I notice this section:</p> <blockquote> <p>Warning</p> <p>This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.</p> </blockquote> <p>Could someone help clarify what the authors mean?</p> <p>First, what do they mean by "<em>for use interactively</em>"? I know what an interactive session is, as opposed to a script run in BATCH mode but I don't see what difference it should make.</p> <p>Then, could you please explain "<em>the non-standard evaluation of argument subset</em>" and why it is dangerous, maybe provide an example?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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