Introduction to R

  • Chapter
  • First Online:
Using R for Trade Policy Analysis
  • 103 Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
EUR 32.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or Ebook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Hardcover Book
USD 99.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free ship** worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Do not write > because it is not part of the code—we will return to > in Sect. 1.5.1.

  2. 2.

    If you have already created a directory, you can click Existing Directory.

  3. 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. 4.

    Note that you do not need to type .R.

  5. 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. 6.

    NA stands for Not Available. We will return to NA in Sect. 1.7.2 and Warning message in Sect. 1.6.8.

  7. 7.

    You may think of a data frame as an Excel spreadsheet.

  8. 8.

    $ works for lists and data frames.

  9. 9.

    More on functions in Sect. 1.6.7.

  10. 10.

    Refer to Table 1.3 for logical operators.

  11. 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. 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. 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. 14.

    Note that it is better to avoid space in the names of the variables.

  15. 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. 16.

    In Sect. 2.4 we will work with the stringi package.

References

Download references

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2023 The Author(s), under exclusive license to Springer Nature Switzerland AG

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

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

Publish with us

Policies and ethics

Navigation