Statistics for Corpus Linguists
  • Overview
  • Introduction to R
    • 1.1 First steps
    • 1.2 Exploring R Studio
    • 1.3 Vectors
    • 1.4 Data frames
    • 1.5 Libraries
    • 1.6 Importing/Exporting
  • NLP
    • 2.1 Concordancing
    • 2.2 Regular expressions
    • 2.3 The CQP interface
    • 2.4 Data annotation
  • Statistics
    • 3.1 Data, variables, samples
    • 3.2 Probability theory
    • 3.3 Descriptive statistics
    • 3.4 Hypothesis testing
    • 3.5 Chi-squared test
    • 3.6 t-test
    • 3.7 ANOVA
    • 3.8 Case study
  • Models
    • 4.1 Linear regression
    • 4.2 Logistic regression
    • 4.3 Poisson regression
  • Machine Learning
    • 5.1 Tree-based methods
    • 5.2 Gradient boosting
  1. 1. Introduction to R
  2. 1.2 Exploring R Studio
  • 1. Introduction to R
    • 1.1 First steps
    • 1.2 Exploring R Studio
    • 1.3 Vectors
    • 1.4 Data frames
    • 1.5 Libraries
    • 1.6 Importing/Exporting

On this page

  • The RStudio interface
    • Console
    • Working environment
    • R scripts

Other Formats

  • PDF
  1. 1. Introduction to R
  2. 1.2 Exploring R Studio

1.2 Exploring RStudio

Author
Affiliation

Vladimir Buskin

Catholic University of Eichstätt-Ingolstadt

Abstract
This unit introduces the RStudio interface and illustrates how to interact with R via the console, store and reuse values in variables, and preserve your work in R scripts.

The RStudio interface

Once you’ve opened RStudio, you will see several empty panes similar to the ones shown here:

Console

Let’s focus on the console window on the left. This is where we can communicate with R directly by entering ‘code’. The way this code is written follows certain arbitrary conventions – just like natural languages such as English or German. Here is an example in the R programming language:

print("Hello world!")
[1] "Hello world!"

Entering this command into your console and pressing Enter will display the sentence "Hello world!" on a new line.

As we can see, anything that R should understand as a simple sequence of letters or words must be enclosed in quotation marks "...". Anything inside them will be interpreted as a so-called string. Their counterparts are numbers, as illustrated here:

2 + 2 - 1
[1] 3

Naturally, you can use R for more sophisticated computations:

  • \(2 + (3 \times 3)\)

    2 + (3 * 3)
    [1] 11
  • \(\sqrt{9}\)

    sqrt(9)
    [1] 3
  • \(\frac{16}{2^3}\)

    16 / 2 ^ 3
    [1] 2

Working environment

While it is certainly useful to print text or numbers to your console, it may sometimes make more sense to store them somewhere (at least temporarily) so that you can reuse them later. R gives us a way of storing data in virtual, container-like objects: variables. We can assign strings or numbers to a variable by using the assignment operator <-.

When you run the commands below, you will (hopefully) notice two items popping up in your Environment tab in the top-right corner.

greeting <- "Hello world!"

quick_math <- 2 + 2 - 1

If we now want to display their contents in the console, we can simply apply the print() function to the variables:

print(greeting)
[1] "Hello world!"
print(quick_math)
[1] 3

We can also embed variables in other statements. For example, let’s take the content of quick_math and multiply it by 2:

hard_math <- quick_math * 2

print(hard_math)
[1] 6

R scripts

Working with the console has a simple but important disadvantage: Once we close RStudio, the console is wiped clean, erasing everything we’ve typed into it during our precious R session.

The remedy for this nuisance is the script. Essentially, a script is the programmer’s equivalent of a Word document: It allows you to save all the code you’ve written to a file, which you can seamlessly continue working on the next time you open it.

Note

To create a new R script, you can …

  • go to File > New File > R Script,

  • click on the icon with the + sign and select R Script,

  • or simply press Ctrl+Shift+N (macOS: Cmd+Shift+N).

Don’t forget to save your script with Ctrl+S (macOS: Cmd+S)! It is good practice to save your files regularly.

1.1 First steps
1.3 Vectors