R is a powerful, open-source programming language and software environment specifically designed for statistical computing, data analysis, and graphical visualization.
Originally created by Ross Ihaka and Robert Gentleman at the University of Auckland in 1993, it has since become the "gold standard" for statisticians, data scientists, and researchers worldwide.
Domain-Specific: Unlike Python (which is general-purpose), R was built from the ground up for data.
Interpreted: You can run code line-by-line in a console, making it excellent for exploratory work.
Vector-Based: R operates on vectors (sequences of data) by default, meaning you can perform operations on entire datasets without writing complex loops.
Open Source: It is free to use and supported by a massive community that constantly contributes new features.
R is arguably the best language for creating publication-quality graphics. The library ggplot2 allows you to build complex charts layer-by-layer, providing immense control over every pixel.
CRAN is a repository of over 18,000+ packages. Whether you are doing genomic sequencing, financial modeling, or social media sentiment analysis, there is likely a package already built for it.
With the Tidyverse (a collection of packages like dplyr and tidyr), cleaning and transforming messy data becomes intuitive and readable.
R handles everything from basic descriptive statistics to advanced machine learning, linear regression, and time-series analysis with built-in functions.
To work effectively in R, most users utilize these two tools:
Tool Purpose
R Language The "engine" that processes the code and math.
RStudio (Posit) The "cockpit" or IDE where you write code, view plots, and manage files.
Download R: Get the base language from CRAN.
Download RStudio: Get the free Desktop version from Posit.
Learn the "Tidyverse": This is the modern way to write R and makes the learning curve much smoother.
Here is what a basic linear regression and plot looks like in R:
# Create a simple dataset
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
# Perform linear regression
model <- lm(y ~ x)
# Plot the data
plot(x, y, main="Simple Regression", col="blue", pch=19)
abline(model, col="red") # Add the regression line
multiply_and_report <- function() {
# 1. Accept first numeric input
num1 <- as.numeric(readline(prompt = "Enter the first number: "))
# 2. Accept second numeric input
num2 <- as.numeric(readline(prompt = "Enter the second number: "))
# 3. Calculate the result
result <- num1 * num2
output_text <- paste("The result of", num1, "times", num2, "is:", result)
# 4. Output to Display Screen
cat("\n--- Display Output ---\n")
print(output_text)
# 5. Output to 'Printer' (Saves to a text file for physical printing)
writeLines(output_text, "print_job.txt")
cat("\nSuccess! Result sent to 'print_job.txt' for your printer.\n")
}
# Execute the routine
multiply_and_report()