title: Create a summary table for continuous variable by categorical variable
body: <p>I am a beginner in R, and have transitioned from Stata/SPSS to R. I used to run tabular command in Stata to generate summary of continuous variable by grouping variable. Is there any way I can do this?</p>
  
  <p>I searched on SO, and I found this thread: <a href="https://stackoverflow.com/questions/9847054/how-to-get-summary-statistics-by-group/26842218">How to get Summary statistics by group</a> </p>
  
  <p>While Hadley's map function did help me provide quartiles, mean and median, but I need more. Specifically, the number of elements in a particular quartile, the number of elements in a particular level of a factor.</p>
  
  <p>Here's dummy code:</p>
  
  <pre><code>data &lt;- c(62, 60, 63, 59, 63, 67, 71, 64, 65, 66, 68, 66, 
             71, 67, 68, 68, 56, 62, 60, 61, 63, 64, 63, 59)
   grp &lt;- factor(rep(LETTERS[1:4], c(4,6,6,8)))
   df &lt;- data.frame(group=grp, dt=data)
  
   df %&gt;% 
    data.table::as.data.table(.) %&gt;% 
    split(.,by=c("group"),drop = TRUE,sorted = TRUE) %&gt;% 
    purrr::map(~summary(.$dt))
  </code></pre>
  
  <p>And</p>
  
  <p><code>describe(df$group)</code> </p>
  
  <p>gives two different disjointed sets--one only provides descriptive statistics about categorical variable, while the other only provides basic six functions. I need to see what's going on within a quartile. </p>
  
  <p>I am using <code>Hmisc::describe</code> package above.</p>
  
  <p>How can I do this using R? I'd sincerely appreciate any help. </p>
  
  <hr>
  
  <p>Sample Output:</p>
  
  <p>My sample output would look something like this , but it would be grouped for each of the four levels of categorical variable. This way, I can analyze what's going on with continuous variable for each level of categorical variable.  Right now, the output is spread across three different commands, and it harder for me to understand what's happening. </p>
  
  <p>Here are the commands:</p>
  
  <pre><code> df %&gt;% data.table::as.data.table(.) %&gt;% split(.,by=c("group"),drop = TRUE,sorted = TRUE) %&gt;% purrr::map(~summary(.$dt))
   df %&gt;% data.table::as.data.table(.) %&gt;% split(.,by=c("group"),drop = TRUE,sorted = TRUE) %&gt;% purrr::map(~describe(.$dt))
   df %&gt;% group_by(group) %&gt;% count(quartile = ntile(dt, 4)) 
  </code></pre>
  
  <p>[The credit for the third command goes to one of the people who answered this questions.]</p>