AI Generate R docs instantly

R Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Variables & Data Types

7 snippets

Variable

name <- "John"

Numeric

age <- 25

Vector

nums <- c(1, 2, 3, 4, 5)

List

person <- list(name = "John", age = 30)

Matrix

mat <- matrix(1:9, nrow = 3, ncol = 3)

Data Frame

df <- data.frame(name = c("A", "B"), age = c(25, 30))

Factor

status <- factor(c("low", "high", "medium"))

Control Flow

5 snippets

If/Else

if (x > 0) {
  print("positive")
} else if (x < 0) {
  print("negative")
} else {
  print("zero")
}

For Loop

for (i in 1:5) {
  print(i)
}

While Loop

while (count < 10) {
  count <- count + 1
}

Apply

sapply(1:5, function(x) x^2)

Ifelse

result <- ifelse(x > 0, "positive", "non-positive")

Functions

4 snippets

Function

greet <- function(name) {
  paste("Hello,", name, "!")
}

Default Args

greet <- function(name = "World") {
  paste("Hello,", name, "!")
}

Return

add <- function(a, b) {
  return(a + b)
}

Anonymous

function(x) x^2

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Vector Operations

6 snippets

Create

v <- c(1, 2, 3, 4, 5)

Sequence

seq(1, 10, by = 2)  # 1, 3, 5, 7, 9

Repeat

rep(1, 5)  # 1, 1, 1, 1, 1

Index

v[1]  # First element
v[2:4]  # Elements 2 to 4

Filter

v[v > 2]  # Elements greater than 2

Operations

sum(v)
mean(v)
max(v)
length(v)

Data Frames

6 snippets

Create

df <- data.frame(
  name = c("Alice", "Bob"),
  age = c(25, 30)
)

Access Column

df$name
df[["name"]]
df[, "name"]

Access Row

df[1, ]  # First row

Filter

df[df$age > 25, ]

Add Column

df$city <- c("NYC", "LA")

Merge

merge(df1, df2, by = "id")

Packages

6 snippets

Install

install.packages("dplyr")

Load

library(dplyr)

dplyr Filter

df %>% filter(age > 25)

dplyr Select

df %>% select(name, age)

dplyr Mutate

df %>% mutate(age_plus = age + 1)

dplyr Group

df %>% group_by(city) %>% summarise(avg = mean(age))

More Cheat Sheets

FAQ

Frequently asked questions

What is a R cheat sheet?

A R cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in R. It helps developers quickly look up syntax without searching through documentation.

How do I learn R quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important R concepts?

Key R concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my R code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your R code using AI.

Code Conversion Tools

Convert R to Other Languages

Easily translate your R code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate R Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week