title: Conditional filtering and summarizing in R
body: <p>I have recently transitioned from STATA + Excel to R. So, I would appreciate if someone could help me in writing efficient code. I have tried my best to research the answer before posting on SO.</p>
  
  <p>Here's how my data looks like:</p>
  
  <pre><code>mydata&lt;-data.frame(sassign$buyer,sassign$purch,sassign$total_)
  str(mydata)
  'data.frame':   50000 obs. of  3 variables:
   $ sassign.buyer : Factor w/ 2 levels "no","yes": 1 1 1 1 1 2 1 1 2 1 ...
   $ sassign.purch : num  10 3 2 1 1 1 1 11 11 1 ...
   $ sassign.total_: num  357 138 172 272 149 113 15 238 418 123 ...
  head(mydata)
    sassign.buyer sassign.purch sassign.total_
  1            no            10            357
  2            no             3            138
  3            no             2            172
  4            no             1            272
  5            no             1            149
  6           yes             1            113
  </code></pre>
  
  <p>My objective is to find average number of buyers with # of purchases > 1. </p>
  
  <p>So, here's what I did:</p>
  
  <p><strong>Method 1: Long method</strong> </p>
  
  <pre><code>library(psych)
  check&lt;-as.numeric(mydata$sassign.buyer)-1
  myd&lt;-cbind(mydata,check)
  abcd&lt;-psych::describe(myd[myd$sassign.purch&gt;1,])
  abcd$mean[4]
  </code></pre>
  
  <p>The output I got is:0.1031536697, which is correct.</p>
  
  <p>@Sathish: Here's how check looks like:</p>
  
  <pre><code>head(check)
  0 0 0 0 0 1
  </code></pre>
  
  <p>This did solve my purpose.</p>
  
  <p>Pros of this method: It's easy and typically a beginner level.
  Cons: Too many-- I need an extra variable (check). Plus, I don't like this method--it's too clunky.</p>
  
  <p><strong>Side Question :</strong> I realized that by default, functions don't show higher precision although options (digits=10) is set. For instance, here's what I got from running : </p>
  
  <pre><code>psych::describe(myd[myd$sassign.purch&gt;1,])
  
  
                 vars     n   mean     sd median trimmed    mad min max range skew
  sassign.buyer*    1 34880   1.10   0.30      1    1.00   0.00   1   2     1 2.61
  sassign.purch     2 34880   5.14   3.48      4    4.73   2.97   2  12    10 0.65
  sassign.total_    3 34880 227.40 101.12    228  226.13 112.68  30 479   449 0.09
  check             4 34880   0.10   0.30      0    0.00   0.00   0   1     1 2.61
                 kurtosis   se
  sassign.buyer*     4.81 0.00
  sassign.purch     -1.05 0.02
  sassign.total_    -0.72 0.54
  check              4.81 0.00
  </code></pre>
  
  <p>It's only when I ran </p>
  
  <pre><code>abcd$mean[4]
  </code></pre>
  
  <p>I got 0.1031536697</p>
  
  <p><strong>Method 2: Using dplyr</strong>
  I tried pipes and function call, but I finally gave up.</p>
  
  <p><strong>Method 2 | Try1:</strong>
  psych::describe(dplyr::filter(mydata,mydata$sassign.purch>1)[,dplyr::mutate(as.numeric(mydata$sassign.buyer)-1)])</p>
  
  <p>Output:</p>
  
  <pre><code>Error in UseMethod("mutate_") : 
    no applicable method for 'mutate_' applied to an object of class "c('double', 'numeric')"
  </code></pre>
  
  <p><strong>Method 2 | Try2: Using pipes:</strong></p>
  
  <pre><code>mydata %&gt;% mutate(newcol = as.numeric(sassign.buyer)-1) %&gt;% dplyr::filter(sassign.purch&gt;1) %&gt;% summarise(meanpurch = mean(newcol))
  </code></pre>
  
  <p>This did work, and I got  meanpurch= 0.1031537. However, I am still not sure about Try 1.</p>
  
  <p>Any thoughts why this isn't working?</p>