| name | R |
| description | Statistical programming language and environment for statistical computing, data analysis, and graphical representation. |
| license | GPL v2 |
| compatibility | R 4.0+ |
| audience | Data scientists, statisticians, bioinformaticians, researchers |
| category | Programming Languages |
R
What I do
I am a language and environment for statistical computing and graphics, developed by Ross Ihaka and Robert Gentleman in 1993. I was designed by statisticians for statisticians, excelling at data manipulation, calculation, and graphical display. I offer a vast ecosystem of packages for statistical analysis, machine learning, data visualization, and bioinformatics. I am widely used in academic research, clinical trials, finance, and any domain requiring sophisticated statistical analysis and visualization.
When to use me
Use R when performing statistical analysis and hypothesis testing, data visualization and exploratory data analysis, machine learning and predictive modeling, bioinformatics and genomic analysis, reporting with R Markdown, or when you need specialized statistical packages not available elsewhere.
Core Concepts
- Vectors as Basic Unit: Most operations are vectorized, operating on entire vectors at once.
- Data Frames: Tabular data structure with rows and columns, similar to spreadsheets or database tables.
- Factors: Categorical data with ordered or unordered levels for statistical modeling.
- S3/S4/OOP Systems: Multiple object-oriented systems with S3 being most common and informal.
- Tidyverse: Collection of packages (dplyr, tidyr, ggplot2, readr) for consistent data science workflows.
- Tibbles: Modern reimagining of data frames with better printing and subsetting behavior.
- Pipes ( %>% / |> ): Chain operations together for readable data transformation pipelines.
- Statistical Modeling: Built-in lm(), glm(), and extensive packages for statistical inference.
- Functional Programming: First-class functions, lapply/sapply/map family, and closure-based abstractions.
- Lazy Evaluation: Arguments are evaluated only when needed, enabling efficient computation.
Code Examples
Data Manipulation with Tidyverse:
library(tidyverse)
people <- tibble(
id = 1:5,
name = c("Alice", "Bob", "Charlie", "Diana", "Eve"),
age = c(25, 30, 35, 28, 32),
department = c("Engineering", "Sales", "Engineering", "Marketing", "Sales"),
salary = c(75000, 60000, 85000
engineers people
filterdepartment
name_salary people
selectname salary
people_with_bonus people
mutatebonus salary
total_comp salary bonus
avg_salary_by_dept people
group_bydepartment
summariseavg_salary meansalary
count n
.groups
sorted_by_age people
arrangedescage
departments tibble
department
budget
joined people
left_joindepartments by
long_data people
pivot_longercols age salary
names_to
values_to
wide_data long_data
pivot_widernames_from metric
values_from value
Statistical Analysis:
set.seed(42)
control <- rnorm(50, mean = 100, sd = 15)
treatment <- rnorm(50, mean = 110, sd = 15)
summary(control)
sd(control)
mean(control)
median(control)
t_result <- t.test(control, treatment, var.equal = TRUE)
print(t_result)
x <- 1:100
y <- 2 * x + rnorm(100, 0, 20)
corx y
model lmy x
summarymodel
group1
group2
group3
df data.frame
value group1 group2 group3
group each
anova_result aovvalue group data df
summaryanova_result
sample rnorm mean sd
t.testsample conf.level
observed matrix nrow
chisq.testobserved
Data Visualization with ggplot2:
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) +
geom_point(size = 3) +
labs(title = "MPG vs Horsepower",
x = "Miles per Gallon",
y = "Horsepower",
color = "Cylinders") +
theme_minimal()
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
geom_boxplot(fill = "lightblue") +
labs(title =
x
y
ggplotmtcars aesx mpg
geom_histogrambins fill color
labstitle
x
y
ggplotmtcars aesx wt y mpg
geom_point
geom_smoothmethod
facet_wrap factorcyl
labstitle
correlation_matrix cormtcars
heatmapcorrelation_matrix
col heat.colors
main
Functions and Control Flow:
calculate_stats <- function(data, na.rm = TRUE) {
if (!is.numeric(data)) {
stop("Data must be numeric")
}
mean_val <- mean(data, na.rm = na.rm)
sd_val <- sd(data, na.rm = na.rm)
median_val <- median(data, na.rm = na.rm)
list(
mean = mean_val,
sd = sd_val,
median = median_val,
n = sum(!is.na(data))
)
}
data
a rnorm
b rnorm
rnorm
results lapplydata calculate_stats
strresults
librarypurrr
results_map mapdata calculate_stats.x
map_dbldata mean.x na.rm
grade_student score
score
score
score
score
scores
grades ifelsescores
ifelsescores
ifelsescores
ifelsescores
Best Practices
- Use Tidyverse Packages: Prefer tidyverse (dplyr, tidyr, ggplot2) over base R for data manipulation and visualization.
- Use Tibbles Over Data Frames: Tibbles have better printing and subsetting behavior.
- Use Pipes for Readability: Chain operations with |> or %>% for readable data transformation pipelines.
- Use Vectorization: Avoid loops; use vectorized operations for better performance and readability.
- Use Functional Programming: Use map functions from purrr instead of for loops and lapply.
- Document with Roxygen2: Document functions with roxygen2 comments for automatic help pages.
- Use testthat for Testing: Write unit tests with testthat package for reproducible code.
- Use R Projects: Use RStudio projects for self-contained, reproducible analyses.
- Use renv for Dependency Management: Use renv for project-specific package versions.
- Follow Style Guide: Use consistent naming (snake_case), spacing, and code organization.