0 XP
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
Statistics
t.test(x, y)
Two-sample t-test
cor(x, y)
Correlation
chisq.test(table)
Chi-squared test
aov(y ~ group, data)
ANOVA
shapiro.test(x)
Normality test
prop.test(x, n)
Proportion test
Modelling
lm(y ~ x, data)
Linear regression
glm(y~x, family=binomial)
Logistic regression
summary(model)
Model summary
predict(model, newdata)
Make predictions
coef(model)
Coefficients
AIC(model)
Model fit criterion
tidymodels
initial_split(df, 0.8)
Train/test split
recipe(y~., data)
Preprocessing recipe
linear_reg()
Linear model spec
workflow() |> add_recipe() |> add_model()
Build workflow
vfold_cv(df, v=5)
5-fold CV
fit_resamples(wf, cv)
CV training
Shiny
shinyApp(ui, server)
Create & run app
sliderInput(id, ...)
Slider widget
selectInput(id, ...)
Dropdown widget
renderPlot({ })
Reactive plot
input$id
Read widget value
reactive({ })
Reactive expression
Great work!
Keyboard shortcuts
Run code+
Run code (Windows)Ctrl+
Clear output+L
Insert pipe %>%+Shift+M
Insert assignment <-Alt+-
Comment / uncomment line+/
Indent lineTab
Outdent lineShift+Tab
Select all+A
Duplicate line+D
Delete line+K
Move line upAlt+
Move line downAlt+
Show this panel?
Close this panelEsc
Switch to Editor tab+1
Switch to Lesson tab+2
Toggle dark mode+Shift+D
Next lesson+]
Previous lesson+[
%>% inserted