R Editor
R-bae AI
Console Output
⚡
Press Run Code to see output
Basics
print(x)
Print to console
x <- 5
Assign variable
class(x)
Check data type
c(1, 2, 3)
Create vector
1:10
Sequence 1 to 10
length(x)
Number of elements
dplyr verbs
filter(df, cond)
Filter rows
select(df, cols)
Choose columns
mutate(df, col=...)
Add/change columns
arrange(df, col)
Sort rows
group_by(df, col)
Group data
summarise(df, ...)
Aggregate groups
rename(df, new=old)
Rename columns
distinct(df, col)
Unique rows
Joins
left_join(x, y)
All rows from x
inner_join(x, y)
Matching rows only
full_join(x, y)
All rows both sides
anti_join(x, y)
Rows not in y
tidyr (reshaping)
pivot_longer()
Wide → long
pivot_wider()
Long → wide
drop_na(df)
Remove NA rows
replace_na(x, val)
Fill NA values
stringr
str_to_upper(x)
Uppercase
str_length(x)
String length
str_detect(x, pat)
Pattern match → TRUE/FALSE
str_replace(x,p,r)
Find and replace
str_split(x, pat)
Split string
str_c(x, y)
Concatenate strings
lubridate (dates)
ymd("2024-01-15")
Parse date
year(date)
Extract year
month(date)
Extract month
today()
Current date
ggplot2 — geoms
geom_point()
Scatter plot
geom_line()
Line chart
geom_bar()
Bar chart (counts)
geom_col()
Bar chart (values)
geom_histogram()
Histogram
geom_boxplot()
Box plot
geom_violin()
Violin plot
geom_smooth()
Trend line
geom_density()
Density curve
geom_tile()
Heatmap tiles
ggplot2 — scales & themes
scale_color_manual()
Custom colours
scale_fill_brewer()
ColorBrewer palette
scale_x_log10()
Log scale on x
facet_wrap(~var)
Small multiples
facet_grid(r~c)
Grid of plots
theme_minimal()
Clean theme
theme_classic()
Classic theme
labs(title,x,y)
Set labels
annotations & layout
geom_text()
Text on plot
geom_label()
Labelled text box
annotate()
Manual annotation
ggsave("file.png")
Save plot to file
plot + plot (patchwork)
Side by side
ggplotly(p)
Make interactive
map(x, fn)
Apply fn to each element
map_dbl(x, fn)
Returns numeric vector
map_chr(x, fn)
Returns character vector
map2(x, y, fn)
Map over two inputs