R Commands Quick Reference
Essential R commands for daily data science work
R
reference
cheatsheet
Quick lookup table of commonly used R commands for data manipulation, visualization, and analysis.
1 Data Import/Export
| Task | Command | Example |
|---|---|---|
| Read CSV | read.csv() |
read.csv("data.csv") |
| Read Excel | readxl::read_excel() |
read_excel("data.xlsx") |
| Write CSV | write.csv() |
write.csv(df, "output.csv") |
| Save RDS | saveRDS() |
saveRDS(data, "data.rds") |
| Load RDS | readRDS() |
readRDS("data.rds") |
2 Data Manipulation (dplyr)
| Task | Command | Example |
|---|---|---|
| Filter rows | filter() |
filter(df, age > 18) |
| Select columns | select() |
select(df, name, age) |
| Create columns | mutate() |
mutate(df, age_months = age * 12) |
| Group data | group_by() |
group_by(df, category) |
| Summarize | summarise() |
summarise(df, mean_age = mean(age)) |
| Sort | arrange() |
arrange(df, desc(age)) |
3 Data Visualization (ggplot2)
| Task | Command | Example |
|---|---|---|
| Scatter plot | geom_point() |
ggplot(df, aes(x, y)) + geom_point() |
| Line plot | geom_line() |
ggplot(df, aes(x, y)) + geom_line() |
| Bar plot | geom_bar() |
ggplot(df, aes(x)) + geom_bar() |
| Histogram | geom_histogram() |
ggplot(df, aes(x)) + geom_histogram() |
| Box plot | geom_boxplot() |
ggplot(df, aes(x, y)) + geom_boxplot() |
4 Statistical Functions
| Task | Command | Example |
|---|---|---|
| Mean | mean() |
mean(x, na.rm = TRUE) |
| Median | median() |
median(x, na.rm = TRUE) |
| Standard deviation | sd() |
sd(x, na.rm = TRUE) |
| Correlation | cor() |
cor(x, y, use = "complete.obs") |
| Linear model | lm() |
lm(y ~ x, data = df) |
| ANOVA | aov() |
aov(y ~ group, data = df) |
5 String Operations
| Task | Command | Example |
|---|---|---|
| Concatenate | paste() |
paste("Hello", "World") |
| Split string | strsplit() |
strsplit("a,b,c", ",") |
| Find pattern | grep() |
grep("pattern", x) |
| Replace pattern | gsub() |
gsub("old", "new", x) |
| String length | nchar() |
nchar("hello") |
6 Package Management
| Task | Command | Example |
|---|---|---|
| Install package | install.packages() |
install.packages("dplyr") |
| Load package | library() |
library(dplyr) |
| Update packages | update.packages() |
update.packages() |
| List packages | installed.packages() |
installed.packages() |
7 Workspace Management
| Task | Command | Example |
|---|---|---|
| List objects | ls() |
ls() |
| Remove objects | rm() |
rm(x, y) |
| Clear workspace | rm(list = ls()) |
rm(list = ls()) |
| Working directory | getwd() |
getwd() |
| Set directory | setwd() |
setwd("/path/to/dir") |
8 Data Types & Structure
| Task | Command | Example |
|---|---|---|
| Data type | class() |
class(x) |
| Structure | str() |
str(df) |
| Dimensions | dim() |
dim(df) |
| Column names | names() |
names(df) |
| Summary | summary() |
summary(df) |
| First rows | head() |
head(df, 10) |
| Last rows | tail() |
tail(df, 10) |
9 Missing Values
| Task | Command | Example |
|---|---|---|
| Check for NA | is.na() |
is.na(x) |
| Remove NA | na.omit() |
na.omit(df) |
| Complete cases | complete.cases() |
complete.cases(df) |
10 Quick Tips
- Use
?function_nameto get help - Use
Tabfor auto-completion in RStudio - Use
Ctrl+Shift+Mfor pipe operator%>% - Use
Ctrl+Shift+Cto comment/uncomment code