Estimated duration: 1 week (4–6 hours)
Level: Beginner (basic programming knowledge assumed)
Keywords: R, RStudio, installation, environment, scripts, console, projects, data types
By the end of this unit, the student will be able to:
<- vs =).print(), class(), str(), summary() to inspect objects.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.
R is the engine that executes the code. Without R, RStudio will not work.
🔗 Download R:
✅ Recommendation: Install the latest stable version (e.g., R 4.4.x)
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.
When you open RStudio, you will see four main panels:
.R or .Rmd files.Ctrl + Enter (Windows/Linux) or Cmd + Enter (macOS).↑ and ↓ to navigate through previous commands.?function_name in the console).🧭 Pro tip:
Never write important code directly in the console. Always use a script (.R) or a notebook (.Rmd).
.RFile > New File > R Script# Example script: saludo.R
mensaje <- "¡Hola, mundo en R!"
print(mensaje)
---
title: "My first notebook"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
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
In R, you can assign values to names (variables) using:
<- (recommended by the R community)nombre <- "Ana"
edad <- 28
pi <- 3.1416
es_estudiante <- TRUE
= (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.
R has 6 atomic data types (the most common):
altura <- 1.75
class(altura) # [1] "numeric"
edad <- 25L # The "L" forces it to be integer
class(edad) # [1] "integer"
nombre <- "Carlos"
class(nombre) # [1] "character"
es_mayor <- TRUE
tiene_hijos <- FALSE
class(es_mayor) # [1] "logical"
z <- 3 + 2i
class(z) # [1] "complex"
sexo <- factor(c("M", "F", "F", "M"))
class(sexo) # [1] "factor"
⚠️ Warning! R automatically converts strings to factors in
data.frame()by default.
UsestringsAsFactors = FALSEor, even better, usetibble().
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
Working with projects is the best practice to keep your work organized and reproducible.
A project is a folder containing:
/data)/output).Rproj file)File > New Projectcurso_r_basico)🌟 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.
File > New File > R Script).# 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)
Ctrl + Enter.ejercicio1.R inside a new project named modulo1.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.
practica_unidad1./data/scripts/output/scripts.getwd() to check your working directory.list.files() to see what’s in your project.numeric and integer, and why factors are important.= with <- in function contexts.☐ 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.