3 - Basics of R and Python
from PART ONE - GETTING STARTED
Published online by Cambridge University Press: 15 September 2017
Summary
In this chapter, you will become familiar with the R and Python languages. Here, R and Python will be used in parallel, but the rest of the book may not always follow that pattern; there may be a situation in which R would seem simpler to use than Python or a situation in which Python would seem like the wiser alternative. However, I need to be clear here: the choice of R or Python for a particular task does not mean that the task cannot be done in the option not chosen. It simply means that it would be harder, less elegant, or too confusing to use the other language, or it might require the use of techniques and concepts not covered in this book.
First Contact with R and Python
You will need first to locate the RStudio and Anaconda environments in your computer. The icons are shown in Figure 3.1.
Becoming Familiar with R
Let's start running RStudio first, as shown in Figure 3.2. In this case, I decided to change the position of the RStudio of the windows, so the source is to the left and the console is to the right (the other windows are not shown). The console is showing only the first message you see when you start RStudio. In your case, the source window should be empty, but mine is not. Please write the code as it is shown in Figure 3.2. Keep reading after you finish typing the code.
Let's use the numbers that RStudio offers in the source window to review the code you have typed:
• Lines 1 and 15 are very special in RStudio. They create a code section, several lines of code that run together. It is a good practice to organize your code in sections: you need to structure your work to improve it progressively and facilitate debugging. A section starts with the symbol #; then you write the name of the section, and finally you add at least four dashes, so that the section is recognized as such. You can quickly visit the sections you create using the drop-down list you find in the bottom corner to the left of the source window and selecting the label of the section you want. In Figure 3.2 you can see that the label “Calculator” appears in that area, close to a pound symbol.
- Type
- Chapter
- Information
- Introduction to Data Science for Social and Policy ResearchCollecting and Organizing Data with R and Python, pp. 24 - 82Publisher: Cambridge University PressPrint publication year: 2017