Useful Bits of R Code

There are some bits of code that I constantly forget, despite needing them all the time. I decided to keep a running list of them all, and thought I would make it public in case they are useful to anyone else. (I can’t guarantee these are the most efficient—or even the right—ways to do any of these things!)

Most of these use the “data.table” package. You can install it and load it with these commands:


Creating a Median Split

d[, MedianSplitVariable := ifelse(ContinuousVariableUsedForSplit > MedianValue, 1, 0)]

Scale a Range of Predictors

This would scale all of the predictors in a dataset called “d”, in columns 2 to 34.

preds <- colnames(d[, 2:34])
d <- d[, (preds) := lapply(.SD, scale), .SDcols=preds]

vlookup in R    

This does the equivalent of a vlookup from Excel in R

data$var <- data2[match(data$matchv, data2$matchv),]$var

Set a Reference Level for a Categorical Predictor

d <- within(d, Shape <- relevel(Shape, ref = "S"))

Label Levels of a Factor

Variable <- factor(variable, labels = c(l1 = blue, l2 = red))

Renaming Columns

This would rename column “Participant.Private.ID” to simply “PPT

setnames(d, "Participant.Private.ID", "PPT")

Reshaping Data

Long to wide.

dcast(data, identifier ~ grouping, value.var = "DV", fun.aggregate = mean)

Wide to long.

melt(data, id.vars = , measure.vars = , = , = )

Plotting Interaction Effects from lmer or glmer Models

I found it tricky to plot the interaction effects in a mixed effects model. The best solution I’ve found uses the “interaction” package.

cat_plot(model, pred = , modx = ) (for categorical predictors)

interact_plot(model, pred = , modx = ) (for at least one continuous predictor)

Effects Code a Variable

contrasts(d$Condition) <- c(-1, 1)

Get Rid of Scientific Notation

options(scipen=7) (gets rid of scientific notation unless the result is wider than 7 digits)

ggplot Commands

Change Size of All Text


Reorder Elements in an Axis by Some Column

aes(x = reorder(x1, ordercolumn))

Center the Main Title

+ theme(plot.title = element_text(hjust = 0.5))

Remove Legend

+ theme(legend.position = "none")

Combine Multiple Plots


manyplots <- ggarrange(p_1, p_2, p_3, nrow = 3, ncol = 1)