title: R - create object from table within console whilst retaining format
body: <p>I have some data from which I can create a table in the RStudio console using</p>
  <pre><code>#Open latest file
  #get a vector of all filenames 
  files &lt;- list.files(path=&quot;MYFILEPATH&quot;,pattern=&quot;files.xls&quot;,full.names = TRUE,recursive = TRUE)
  
  #get the directory names of these (for grouping)
  dirs &lt;- dirname(files)
  
  #find the last file in each directory (i.e. latest modified time)
  lastfiles &lt;- tapply(files,dirs,function(v) v[which.max(file.mtime(v))])
  
  File_List &lt;- file.info(list.files(&quot;//MYFILEPATH&quot;, full.names = T))
  
  Path_Of_Interest &lt;- rownames(File_List)[which.max(File_List$mtime)]
  
  library(readxl)
  MyData &lt;- read_excel(Path_Of_Interest,
      col_types = c(&quot;numeric&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;numeric&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;text&quot;, &quot;text&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;date&quot;, 
          &quot;date&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, 
          &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;, &quot;skip&quot;))
  
  #Rename columns
  MyData &lt;- MyData %&gt;%
  rename(Age = &quot;Age (Years)&quot;, Area = &quot;Preferred Area&quot;, SeniorArea = &quot;Preferred Senior Area&quot;, SaleDate = &quot;Sale Date&quot;, ReturnDate = &quot;Return Date&quot;)
  
  
  #Group Age Column
  
  MyData[&quot;Age&quot;] = cut(MyData$Age, c(0, 17, Inf), c(&quot;0-17&quot;, &quot;18+&quot;), include.lowest=TRUE)
  
  #Filter to most recent data only
  MyData %&gt;%
  select(Age, Area, SaleDate) %&gt;%
  filter(SaleDate &gt;= as.Date(&quot;2021-10-25&quot;) &amp; SaleDate &lt;= as.Date(&quot;2021-10-31&quot;))
  
  #Create table
  table(MyData$Area, MyData$Age)
  </code></pre>
  <p>Raw data example here:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: center;">Area</th>
  <th style="text-align: right;">Age</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">1</td>
  <td style="text-align: center;">AreaA</td>
  <td style="text-align: right;">0-17</td>
  </tr>
  <tr>
  <td style="text-align: left;">2</td>
  <td style="text-align: center;">AreaA</td>
  <td style="text-align: right;">0-17</td>
  </tr>
  <tr>
  <td style="text-align: left;">3</td>
  <td style="text-align: center;">AreaB</td>
  <td style="text-align: right;">18+</td>
  </tr>
  <tr>
  <td style="text-align: left;">4</td>
  <td style="text-align: center;">AreaB</td>
  <td style="text-align: right;">18+</td>
  </tr>
  <tr>
  <td style="text-align: left;">5</td>
  <td style="text-align: center;">AreaB</td>
  <td style="text-align: right;">0-17</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The output in the console looks like how I would like it to look, similar to below:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Area</th>
  <th style="text-align: center;">0-17</th>
  <th style="text-align: right;">18+</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">AreaA</td>
  <td style="text-align: center;">310</td>
  <td style="text-align: right;">405</td>
  </tr>
  <tr>
  <td style="text-align: left;">AreaB</td>
  <td style="text-align: center;">210</td>
  <td style="text-align: right;">401</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>However when I try to create an object out of this table (in order to merge it with another later) and then view the object I get a table like the below (not all rows included but you can see the column names)</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Var1</th>
  <th style="text-align: center;">Var2</th>
  <th style="text-align: right;">Freq</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">AreaA</td>
  <td style="text-align: center;">0-17</td>
  <td style="text-align: right;">310</td>
  </tr>
  <tr>
  <td style="text-align: left;">AreaB</td>
  <td style="text-align: center;">0-17</td>
  <td style="text-align: right;">210</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Can anyone please advise how to create the object to match format similar to the first table above for use in other things?</p>
  <p>Basically I just need to be able to create an object which is the equivalent of an Excel pivot table with 'Area' for columns and count of 'Age' for rows.</p>
  <p>Thank you!</p>