Skip to main content Accessibility help
×
Hostname: page-component-7479d7b7d-t6hkb Total loading time: 0 Render date: 2024-07-12T01:00:04.766Z Has data issue: false hasContentIssue false

3 - Basics of R and Python

from PART ONE - GETTING STARTED

Published online by Cambridge University Press:  15 September 2017

Jose Manuel Magallanes Reyes
Affiliation:
University of Washington and Pontificia Universidad Católica del Perú
Get access

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:

  1. • 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 Research
Collecting and Organizing Data with R and Python
, pp. 24 - 82
Publisher: Cambridge University Press
Print publication year: 2017

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×