<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-the-xlsx-package-to-create-an-excel-file/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/17/xlsx1/?utm_source=rss&utm_medium=rss&utm_campaign=xlsx1"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/screenshot.jpg" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/screenshot-300x196.jpg?resize=300%2C196" alt="" class="wp-image-6365" data-recalc-dims="1"></a></p>
<p>Microsoft Excel is perhaps the most popular data anlysis tool out there. While arguably convenient, spreadsheet software is error prone and Excel code can be <a rel="nofollow" href="http://lemire.me/blog/2014/05/23/you-shouldnt-use-a-spreadsheet-for-important-work-i-mean-it/" target="_blank">very hard to review and test</a>.</p>
<p>After successfully completing this exercise set, you will be able to prepare a basic Excel document using just R (no need to touch Excel yourself), leaving behind a reproducible R-script.</p>
<p>Solutions are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/17/using-the-xlsx-package-to-create-an-excel-file-solutions/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Install and load the <code>xlsx</code> package, using the <code>dependencies = TRUE</code> option.</p>
<p><strong>Exercise 2</strong><br>
Create an <code>xlsx</code> workbook object in your R workspace and call it <code>wb</code>.</p>
<p><strong>Exercise 3</strong><br>
Create a sheet object in <code>wb</code> named <code>iris</code> assign it the name <code>sheet1</code> in your workspace.</p>
<p><strong>Exercise 4</strong><br>
Write the built-in Iris <code>data.frame</code> to the iris sheet without row names. Hint: use the <code>addDataFrame()</code> function.</p>
<p>Now you can write your workbook anytime to your working directory using <code>saveWorkbook(wb, &quot;filename.xlsx&quot;)</code>.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/17/xlsx1/?utm_source=rss&utm_medium=rss&utm_campaign=xlsx1"></aside>
<div><strong>Learn more</strong> about working with excel and R in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/learn-by-example-statistics-and-data-science-in-r/" target="_blank">Learn By Example: Statistics and Data Science in R</a>. In this course you will learn how to:
<ul>
<li>Learn some of the differences between working in Excel with regression modelling and R</li>
<li>Learn about different statistical concepts</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
Apply ‘freeze pane’ on the top row.</p>
<p><strong>Exercise 6</strong><br>
Set width of columns 1 through 5 to 12, that is 84 pixels.</p>
<p><strong>Exercise 7</strong><br>
Use <code>Font</code>, <code>CellBlock</code> and <code>CB.setFont</code> to make the header in bold.</p>
<p><strong>Exercise 8</strong><br>
Using tapply generate a table with the mean of ‘petal width’ by species and write to a new sheet called <code>pw</code>, from row 2 down.</p>
<p><strong>Exercise 9</strong><br>
Add a title in cell <code>A1</code> above the table, merge the cells of the first three columns.</p>
<p><strong>Exercise 10</strong><br>
Save your workbook to your working directory and open using Excel. Go back to <code>R</code> and continue formatting and adding information to your workbook at will.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/" title="Building Shiny App exercises part 1" target="_blank">Building Shiny App exercises part 1 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/24/data-wrangling-part-1-io/" title="Data wrangling : I/O (Part-1)" target="_blank">Data wrangling : I/O (Part-1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/" title="Building Shiny App exercises part 4" target="_blank">Building Shiny App exercises part 4 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/17/xlsx1/?utm_source=rss&utm_medium=rss&utm_campaign=xlsx1"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/emSAIq9qz_Y" height="1"></a></div>