📘 Unit 1.1: Introduction to R and RStudio

Estimated duration: 1 week (4–6 hours)
Level: Beginner (basic programming knowledge assumed)
Keywords: R, RStudio, installation, environment, scripts, console, projects, data types


🎯 LEARNING OBJECTIVES

By the end of this unit, the student will be able to:

  1. Correctly install R and RStudio on their operating system.
  2. Navigate the RStudio interface and understand the function of each panel.
  3. Create, save, and execute R scripts.
  4. Use the R console for quick tests and exploration.
  5. Understand basic data types in R.
  6. Assign values to variables and know style conventions (<- vs =).
  7. Use basic functions such as print(), class(), str(), summary() to inspect objects.
  8. Create and manage projects in RStudio to maintain an organized and reproducible workflow.

📚 1. WHAT IS R?

1.1. Definition and origin

R is a free and open-source programming language and software environment specifically designed for statistical analysis, data visualization, and data science. It was created in 1993 by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, as an implementation of the S language.

Today, R is maintained by the R Foundation for Statistical Computing and boasts a massive global community that develops packages, shares knowledge, and organizes conferences (such as useR! and R-Ladies).

💡 Why learn R?

  • It is the standard in statistics, bioinformatics, social sciences, and finance.
  • It has the most powerful data visualization ecosystem (ggplot2).
  • It has over 19,000 packages on CRAN (as of 2025).
  • It is ideal for exploratory analysis, reproducible reports, and rapid prototyping.

💻 2. INSTALLING R AND RSTUDIO

2.1. Install R (base)

R is the engine that executes the code. Without R, RStudio will not work.

🔗 Download R:

  • Official site: https://cran.r-project.org/
  • Select your operating system (Windows, macOS, Linux)
  • Download the base version (you don’t need the “tools” unless you plan to compile packages)

✅ Recommendation: Install the latest stable version (e.g., R 4.4.x)

2.2. Install RStudio Desktop

RStudio is an Integrated Development Environment (IDE) that makes working with R easier. It is not mandatory, but highly recommended.

🔗 Download RStudio:

✅ Install RStudio after installing R.


🖥️ 3. RSTUDIO INTERFACE

When you open RStudio, you will see four main panels:

Panel 1 (Top left): Script Editor

  • This is where you write and save your code in .R or .Rmd files.
  • You can have multiple tabs open.
  • To run a line: Ctrl + Enter (Windows/Linux) or Cmd + Enter (macOS).

Panel 2 (Top right): Environment / History

  • Environment: Displays all variables and functions you have created in the current session.
  • History: Saves all commands you have executed in the console.

Panel 3 (Bottom left): R Console

  • This is where R “responds.” You can type commands directly and see results.
  • Ideal for quick tests, but not for permanent code.
  • Use and to navigate through previous commands.

Panel 4 (Bottom right): Files, Plots, Packages, Help, Viewer

  • Files: Navigate your file system.
  • Plots: Displays generated graphics.
  • Packages: List of installed and loaded packages.
  • Help: Function documentation (type ?function_name in the console).
  • Viewer: For viewing Shiny apps or embedded web content.

🧭 Pro tip:
Never write important code directly in the console. Always use a script (.R) or a notebook (.Rmd).


📄 4. SCRIPTS, NOTEBOOKS, AND CONSOLE

4.1. Scripts (.R)

  • Plain text files where you save your code.
  • Extension: .R
  • Advantage: You can reuse, share, and version your code.
  • To create one: File > New File > R Script
# Example script: saludo.R
mensaje <- "¡Hola, mundo en R!"
print(mensaje)

4.2. R Notebooks / R Markdown (.Rmd)

  • Allow you to combine code + text + results + graphics in a single document.
  • Ideal for reports, tutorials, and reproducible projects.
  • Can be compiled into HTML, PDF, or Word.
---
title: "My first notebook"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

Hello from R Markdown!

x <- 5 + 3
x

> ✨ We will cover R Markdown in depth in Module 3.

### 4.3. Console

- For quick tests, exploration, and debugging.
- Not saved automatically (unless you manually save the history).
- Example:

```r
> 2 + 2
[1] 4

> sqrt(16)
[1] 4

➡️ 5. VARIABLE ASSIGNMENT

In R, you can assign values to names (variables) using:

Option 1: <- (recommended by the R community)

nombre <- "Ana"
edad <- 28
pi <- 3.1416
es_estudiante <- TRUE

Option 2: = (valid, but less used in scripts)

nombre = "Ana"
edad = 28

🚫 Avoid this in scripts:
assign("nombre", "Ana") — it’s valid, but unnecessarily complex.

💡 Cultural convention in R:
The community prefers <- because it is more explicit and avoids confusion with function arguments.


🔢 6. BASIC DATA TYPES

R has 6 atomic data types (the most common):

6.1. Numeric (decimal number)

altura <- 1.75
class(altura)  # [1] "numeric"

6.2. Integer (whole number)

edad <- 25L  # The "L" forces it to be integer
class(edad)  # [1] "integer"

6.3. Character (text string)

nombre <- "Carlos"
class(nombre)  # [1] "character"

6.4. Logical (boolean)

es_mayor <- TRUE
tiene_hijos <- FALSE
class(es_mayor)  # [1] "logical"

6.5. Complex (complex — rare in data science)

z <- 3 + 2i
class(z)  # [1] "complex"

6.6. Factor (categorical — very important in data analysis)

sexo <- factor(c("M", "F", "F", "M"))
class(sexo)  # [1] "factor"

⚠️ Warning! R automatically converts strings to factors in data.frame() by default.
Use stringsAsFactors = FALSE or, even better, use tibble().


🔍 7. BASIC FUNCTIONS TO INSPECT OBJECTS

print()

Displays the value of an object.

mensaje <- "Bienvenido a R"
print(mensaje)
# [1] "Bienvenido a R"

class()

Returns the data type.

class(3.14)     # "numeric"
class("hola")   # "character"
class(TRUE)     # "logical"

typeof()

Returns the internal type (more technical).

typeof(3.14)    # "double"
typeof(3L)      # "integer"

str()

Displays the structure of an object (very useful!).

datos <- data.frame(nombre = c("Ana", "Luis"), edad = c(25, 30))
str(datos)
# 'data.frame': 2 obs. of  2 variables:
#  $ nombre: chr  "Ana" "Luis"
#  $ edad  : num  25 30

summary()

Provides a statistical summary (ideal for vectors and data frames).

edades <- c(22, 25, 30, 35, 40)
summary(edades)
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#   22.0    25.0    30.0    30.4    35.0    40.0 

📁 8. CREATING AND MANAGING PROJECTS IN RSTUDIO

Working with projects is the best practice to keep your work organized and reproducible.

What is a Project in RStudio?

A project is a folder containing:

  • Your scripts and notebooks
  • Your data (folder /data)
  • Your graphs and results (folder /output)
  • Your work environment (.Rproj file)
  • Your history and workspace

How to create a project:

  1. File > New Project
  2. Choose “New Directory” > “New Project”
  3. Name the folder (e.g., curso_r_basico)
  4. Check “Create a git repository” (optional but recommended)
  5. Done! RStudio will change the working directory to that folder.

🌟 Advantages:

  • RStudio remembers your environment (variables, console, open scripts).
  • File paths are relative to the project (avoids absolute paths like C:/...).
  • Facilitates teamwork and reproducibility.

🧪 9. PRACTICAL EXERCISES

Exercise 1: Installation and first script

  1. Install R and RStudio if you haven’t already.
  2. Open RStudio and create a new script (File > New File > R Script).
  3. Write the following code:
# My first R script
mi_nombre <- "TuNombre"
mi_edad <- 25
mensaje <- paste("Hola, me llamo", mi_nombre, "y tengo", mi_edad, "años.")
print(mensaje)
  1. Execute line by line using Ctrl + Enter.
  2. Save the script as ejercicio1.R inside a new project named modulo1.

Exercise 2: Exploring data types

Create a new script and write:

# Exploring data types
x <- 42
y <- 42L
z <- "42"
w <- TRUE

print(class(x))  # What type is it?
print(class(y))
print(class(z))
print(class(w))

# What happens if you add x + z? Try it and explain the error.

Exercise 3: Create a project and folder structure

  1. Create a new project named practica_unidad1.
  2. Inside it, create the folders:
    • /data
    • /scripts
    • /output
  3. Save your script from Exercise 1 inside /scripts.
  4. Use getwd() to check your working directory.
  5. Use list.files() to see what’s in your project.

📚 10. ADDITIONAL RESOURCES

Recommended readings:

Useful videos:

  • “RStudio IDE Basics” (RStudio Team, YouTube)
  • “What is R?” (DataCamp, YouTube)

Cheatsheets:


🧑‍🏫 INSTRUCTOR NOTES

  • Focus on user experience: Many students get frustrated with installation. Prepare a visual guide or short support video.
  • Avoid the console for permanent code: Reinforce from day one the importance of scripts.
  • Demonstrate the value of projects: Show them how to open a project and how RStudio remembers everything.
  • Data types: Spend time explaining the difference between numeric and integer, and why factors are important.
  • Common mistakes:
    • Forgetting to save the script.
    • Using absolute paths instead of relative ones.
    • Confusing = with <- in function contexts.

✅ UNIT CHECKLIST

☐ I installed R and RStudio correctly.
☐ I can identify the 4 panels in RStudio.
☐ I know how to create, save, and run an .R script.
☐ I understand the difference between script, notebook, and console.
☐ I can assign variables using <-.
☐ I recognize basic data types.
☐ I know how to use class(), str(), summary().
☐ I created and manage a project in RStudio.
☐ I completed the 3 practical exercises.
☐ I explored at least one additional resource.


Congratulations! You have completed Unit 1.1.
You are ready to dive into R’s data structures in the next unit.

Course Info

Course: R-zero-to-hero

Language: EN

Lesson: Module01