Abstract
This chapter is designed to provide a beginner user of R with key information to be ready to understand the code used in this book.
First, we will learn how to launch a project in RStudio in Section 1.3.1 and open an R Script file in Section 1.3.2.
Section 1.4 explains how to install and load packages in R and which packages the reader needs to install and load to replicate the code in this book. These sections contain several screen-shots of my computer so that the reader can follow all the steps visually.
In Section 1.5, we will learn how to read and replicate the code in this book.
Finally, the chapter ends with examples with R.
Section 1.6, presents eight key facts regarding the R grammar I think any beginner should be aware of.
Section 1.7.1 builds a step-by-step example applying the knowledge of the previous section.
Section 1.7.2 shows the main data management operations with two dummy data sets.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Do not write > because it is not part of the code—we will return to > in Sect. 1.5.1.
- 2.
If you have already created a directory, you can click Existing Directory.
- 3.
In parenthesis the package version used in this book. To retrieve the package version of ggplot2, for example, after you installed it: packageVersion("ggplot2"). Again, it should be fine to replicate this code even though you have a different version.
- 4.
Note that you do not need to type .R.
- 5.
We need to specify that this operation does not work in the R language. In fact, if you are a Python user you are aware that in Python this is a legit operation that replicates the string many times as determined by the numeric value.
- 6.
- 7.
You may think of a data frame as an Excel spreadsheet.
- 8.
$ works for lists and data frames.
- 9.
More on functions in Sect. 1.6.7.
- 10.
Refer to Table 1.3 for logical operators.
- 11.
Note that if you have the latest version of R you will not able to replicate the warning message since the if() function returns an error in the latest version with the same example.
- 12.
Note that sum(), min(), max() treat the collection of arguments as the vector. This is not the typical behaviour in R. In cumsum() and mean(), the c() function combines values into a vector (Burns, 2012, p. 8).
- 13.
Note that if you did not remove point or clear the objects from the workspace, you do not need to generate again point to make the loop work. However, we generate it again to make our work easy to understand. On the other hand, we do not really need to generate total_score out of the loop. We could remove it from the workspace with rm() and this would not affect the loop. However, when we want to store multiple results it is necessary to initialize it. We will talk again about the initialization of total_score in a few pages.
- 14.
Note that it is better to avoid space in the names of the variables.
- 15.
In the rest of the book I will not print the code to save the images. However, for ggplot2 plots I use the ggsave() function. For other plots, I save them as shown in Figs. 1.13 and 1.14. To save 3D plots, you may use the rgl.snapshot() function from the rgl package. However, we will not make any 3D plot in this book.
- 16.
In Sect. 2.4 we will work with the stringi package.
References
Blair, G., Cooper, J., Coppock, A., Humphreys, M., & Sonnet, L. (2021). estimatr: Fast estimators for design-based inference. R package version 0.30.4. https://CRAN.R-project.org/package=estimatr.
Burns, P. (2012). The R Inferno. https://www.burns-stat.com/documents/books/the-r-inferno/. ISBN 9781471046520
Chang, W., Cheng, J., Allaire, J., Sievert, C., Schloerke, B., **e, Y., Allen, J., McPherson, J., Dipert, A., & Borges, B. (2021). shiny: Web application framework for R. R package version 1.6.0. https://CRAN.R-project.org/package=shiny.
Croissant, Y., & Millo, G. (2008). Panel data econometrics in R: The plm package. Journal of Statistical Software, 27(2). http://www.jstatsoft.org/v27/i02/.
Dowle, M., & Srinivasan, A. (2017). data.table: Extension of ‘data.frame‘. R package version 1.10.4. https://CRAN.R-project.org/package=data.table.
Gagolewski, M. (2020). R package stringi: Character string processing facilities. http://www.gagolewski.com/software/stringi/.
Georgakopoulos, H. (2015). Quantitative trading with R. Palgrave Macmillan.
Harrell, F. E., Jr., with contributions from Charles Dupont & many others. (2021). Hmisc: Harrell Miscellaneous. R package version 4.5-0. https://CRAN.R-project.org/package=Hmisc.
Højsgaard, S., & Halekoh, U. (2023). doBy: Groupwise statistics, LSmeans, linear estimates, utilities. R package version 4.6.16. https://CRAN.R-project.org/package=doBy.
Hlavac, M. (2018). Stargazer: Well-formatted regression and summary statistics tables. Central European Labour Studies Institute (CELSI), Bratislava, Slovakia. R package version 5.2.2. https://CRAN.R-project.org/package=stargazer.
Kassambara, A. (2019). ggpubr: ’ggplot2’ based publication ready plots. R package version 0.2.1. https://CRAN.R-project.org/package=ggpubr.
Merlino, A., & Howard, P. (2021). shinyFeedback: Display user feedback in shiny apps. R package version 0.4.0. https://CRAN.R-project.org/package=shinyFeedback.
Ooms, J. (2018). gifski: Highest quality GIF encoder. R package version 0.8.6. https://CRAN.R-project.org/package=gifski.
Pedersen, T. L., & Robinson, D. (2020). gganimate: A grammar of animated graphics. R package version 1.0.5. https://CRAN.R-project.org/package=gganimate.
Porto, M. (2022). Introduction to mathematics for economics with R. Springer Nature.
R Core Team. (2020). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.
RStudio Team. (2020). RStudio: Integrated development environment for R. RStudio, PBC, Boston, MA. http://www.rstudio.com/.
Sievert, C. (2020). Interactive web-based data visualization with R, plotly, and shiny. Chapman and Hall/CRC. https://plotly-r.com.
Urbanek, S. (2013). png: Read and write PNG images. R package version 0.1-7. https://CRAN.R-project.org/package=png.
Wickham, H. (2009). ggplot2: Elegant graphics for data analysis. Springer-Verlag. http://ggplot2.org.
Wickham, H. (2018). scales: Scale functions for visualization. R package version 1.0.0. https://CRAN.R-project.org/package=scales.
Wickham, H. (2019a). Advanced R. The R Series (2nd ed.). CRC Press/Taylor & Francis Group.
Wickham, H. (2019b). stringr: Simple, consistent wrappers for common string operations. R package version 1.4.0. https://CRAN.R-project.org/package=stringr.
Wickham, H., & Henry, L. (2019). tidyr: Easily tidy data with ‘spread()’ and ‘gather()’ functions. R package version 0.8.3. https://CRAN.R-project.org/package=tidyr.
Wickham, H., & Miller, E. (2020). haven: Import and export ‘SPSS’, ‘Stata’ and ‘SAS’ files. R package version 2.3.1. https://CRAN.R-project.org/package=haven.
Wickham, H., François, R., Henry, L., & Müller, K. (2019). dplyr: A grammar of data manipulation. R package version 0.8.3. https://CRAN.R-project.org/package=dplyr.
Zeileis, A. (2004). Econometric computing with HC and HAC covariance matrix estimators. Journal of Statistical Software, 11(10), 1–17. http://www.jstatsoft.org/v11/i10/.
Zeileis, A., & Grothendieck, G. (2005). zoo: S3 infrastructure for regular and irregular time series. Journal of Statistical Software, 14(6), 1–27.
Zeileis, A., & Hothorn, T. (2002). Diagnostic checking in regression relationships. R News, 2(3), 7–10. https://CRAN.R-project.org/doc/Rnews/.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2023 The Author(s), under exclusive license to Springer Nature Switzerland AG
About this chapter
Cite this chapter
Porto, M. (2023). Introduction to R. In: Using R for Trade Policy Analysis. Springer, Cham. https://doi.org/10.1007/978-3-031-35044-3_1
Download citation
DOI: https://doi.org/10.1007/978-3-031-35044-3_1
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-031-35043-6
Online ISBN: 978-3-031-35044-3
eBook Packages: Economics and FinanceEconomics and Finance (R0)