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:
install.package("data.table")
library(data.table)
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 = , variable.name = , value.name = )
Plotting Interaction Effects from lmer or glmer Models
The best solution I’ve found uses the “interactions” 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(-.5, .5)
Get Rid of Scientific Notation
options(scipen=7)
(gets rid of scientific notation unless the result is wider than 7 digits)
Switch Optimizer/Increase Iterations for MEM
m <- lmer(DV ~ IV + (1 + IV | Subject), data = d, control=lmerControl(optimizer="bobyqa",optCtrl=list(maxfun=100000)))
Load Copied Excel Data into R
Copy the Excel data you want to load. Then run the following.
my_data <- read.table(pipe("pbpaste"), sep="\t", header = TRUE)
ggplot Commands
Change Size of All Text
+theme(text=element_text(size=21))
Reorder Elements in an Axis by Some Column
aes(x = reorder(x1, ordercolumn))
Reorder Elements in an Axis Manually
ggplot(d, aes(x = factor(Emotion, level = c("Negative", "Neutral", "Positive"))
Center the Main Title
+ theme(plot.title = element_text(hjust = 0.5))
Remove Legend
+ theme(legend.position = "none")
Combine Multiple Plots
library(ggpubr)
manyplots <- ggarrange(p_1, p_2, p_3, nrow = 3, ncol = 1)
Plot Words on a Scatterplot
Plot a set of words by their length and frequency, and add labels for the words at the extremes.
library(ggrepel)
ggplot(d, aes(x = Length, y = Frequency, label = Word)) + geom_point() + geom_label_repel(data = subset(d,(abs(Length) >1 & abs(Frequency) > 1)))
Other Helpful Resources
A list of online resources that I’ve found helpful and want to remember.
On interpreting logistic regression coefficients, and converting them to odds and probabilities.