Learn R Programming

⚠️There's a newer version (2026.1) of this package.Take me there.

bruceR

BRoadly Useful Convenient and Efficient R functions that BRing Users Concise and Elegant R data analyses.

This package includes easy-to-use functions for (1) basic R programming (e.g., set working directory to the path of currently opened file, import/export data from/to files with any format, print strings with rich formats and colors); (2) multivariate computation (e.g., compute scale sums/means/... with reverse scoring); (3) reliability analyses and factor analyses (PCA, EFA, CFA); (4) descriptive statistics and correlation analyses; (5) t-test, multi-factor analysis of variance (ANOVA), simple-effect analysis, and post-hoc multiple comparison; (6) tidy report of statistical models (to R Console and Microsoft Word); (7) mediation and moderation analyses (PROCESS); and (8) additional toolbox for statistics and graphics.

Author

Han-Wu-Shuang (Bruce) Bao 包寒吴霜

Email: baohws@foxmail.com

Homepage: psychbruce.github.io

Citation

User Guide

NEWS (GitHub version)

Chinese Documentation for bruceR: I. Overview

Chinese Documentation for bruceR: II. FAQ

Installation

## Method 1: Install from CRAN
install.packages("bruceR")
update.packages(ask=FALSE)

## Method 2: Install from GitHub
install.packages("devtools")
devtools::install_github("psychbruce/bruceR", force=TRUE)
update.packages(ask=FALSE)

Tips:

  1. Good practice: restart RStudio before installation.
  2. Good practice: update R to the latest version (v4.0+).
  3. Good practice: install Rtools.exe (it is not an R package) on Windows system.
  4. If you see "Do you want to restart R prior to install?", choose "Yes" at the first time and then choose "No".
  5. If you see "Do you want to install from sources the package which needs compilation?", just choose "No".
  6. If you fail to install, please carefully read the warning messages and find out the R package(s) causing the failure, manually uninstall and reinstall these R package(s), and then retry the main installation.

Package Dependency

bruceR depends on many important R packages.

Loading bruceR by library(bruceR) will also load these R packages for you:

  • [Data]:

    • dplyr: Data manipulation and processing.
    • tidyr: Data cleaning and reshaping.
    • stringr: Toolbox for string operation (with regular expressions).
    • forcats: Toolbox for factor manipulation (for categorical variables).
    • data.table: Advanced data.frame with higher efficiency.
  • [Stat]:

    • emmeans: Estimates of marginal means and multiple contrasts.
    • effectsize: Estimates of effect sizes and standardized parameters.
    • performance: Estimates of regression models performance.
    • lmerTest: Tests of linear mixed effects models (LMM, also known as HLM and multilevel models).
  • [Plot]:

    • ggplot2: Data visualization.
    • ggtext: Markdown/HTML rich text format for ggplot2 (geoms and themes).
    • cowplot: Advanced toolbox for ggplot2 (arrange multiple plots and add labels).
    • see: Advanced toolbox for ggplot2 (geoms, scales, themes, and color palettes).

Main Functions in bruceR

  1. Basic R Programming

    • cc()
    • set.wd() (alias: set_wd())
    • import(), export()
    • pkg_depend(), pkg_install_suggested()
    • formatF(), formatN()
    • print_table()
    • Print(), Glue(), Run()
    • %^%
    • %notin%
    • %allin%, %anyin%, %nonein%, %partin%
  2. Multivariate Computation

    • SUM(), MEAN(), STD(), MODE(), COUNT(), CONSEC()
    • RECODE(), RESCALE()
    • LOOKUP()
  3. Reliability and Factor Analyses

    • Alpha()
    • EFA() / PCA()
    • CFA()
  4. Descriptive Statistics and Correlation Analyses

    • Describe()
    • Freq()
    • Corr()
    • cor_diff()
  5. T-Test, Multi-Factor ANOVA, Simple-Effect Analysis, and Post-Hoc Multiple Comparison

    • TTEST()
    • MANOVA()
    • EMMEANS()
  6. Tidy Report of Regression Models

    • model_summary()
    • lavaan_summary()
    • GLM_summary()
    • HLM_summary()
    • HLM_ICC_rWG()
    • regress()
  7. Mediation and Moderation Analyses

    • PROCESS()
    • med_summary()
  8. Additional Toolbox for Statistics and Graphics

    • grand_mean_center()
    • group_mean_center()
    • ccf_plot()
    • granger_test()
    • granger_causality()
    • theme_bruce()
    • show_colors()

Function Output

For some functions, the results can be saved to Microsoft Word using the file argument.

bruceR FunctionOutput: R ConsoleOutput: MS Word
print_table()√ (basic usage)
Describe()
Freq()
Corr()√ (suggested)
Alpha()(unnecessary)
EFA() / PCA()
CFA()
TTEST()
MANOVA()
EMMEANS()
PROCESS()√ (partial)
model_summary()√ (suggested)
med_summary()
lavaan_summary()
GLM_summary()
HLM_summary()
HLM_ICC_rWG()(unnecessary)
granger_test()
granger_causality()

Examples:

## Correlation analysis (and descriptive statistics)
Corr(airquality, file="cor.doc")

## Regression analysis
lm1 = lm(Temp ~ Month + Day, data=airquality)
lm2 = lm(Temp ~ Month + Day + Wind + Solar.R, data=airquality)
model_summary(list(lm1, lm2), file="reg.doc")
model_summary(list(lm1, lm2), std=TRUE, file="reg_std.doc")

Learn More From Help Pages

library(bruceR)

## Overview
help("bruceR")
help(bruceR)
?bruceR

## See help pages of functions
## (use `?function` or `help(function)`)
?set.wd
?import
?export
?Describe
?Freq
?Corr
?Alpha
?MEAN
?RECODE
?TTEST
?MANOVA
?EMMEANS
?PROCESS
?model_summary
?lavaan_summary
?GLM_summary
?HLM_summary
...

Copy Link

Version

Install

install.packages('bruceR')

Monthly Downloads

2,712

Version

0.8.6

License

GPL-3

Maintainer

Han-Wu-Shuang Bao

Last Published

April 13th, 2022

Functions in bruceR (0.8.6)

Freq

Frequency statistics.
Describe

Descriptive statistics.
CFA

Confirmatory Factor Analysis (CFA).
GLM_summary

Tidy report of GLM (lm and glm models).
Corr

Correlation analysis.
Alpha

Reliability analysis (Cronbach's \(\alpha\) and McDonald's \(\omega\)).
EFA

Principal Component Analysis (PCA) and Exploratory Factor analysis (EFA).
HLM_ICC_rWG

Tidy report of HLM indices: ICC(1), ICC(2), and rWG/rWG(J).
EMMEANS

Simple-effect analysis and post-hoc multiple comparison.
HLM_summary

Tidy report of HLM (lmer and glmer models).
PROCESS

PROCESS for mediation and/or moderation analyses.
Print

Print strings with rich formats and colors.
MANOVA

Multi-factor ANOVA.
LOOKUP

Search, match, and look up values (like Excel's functions INDEX + MATCH).
ccf_plot

Cross-correlation analysis.
cor_diff

Test the difference between two correlations.
%notin%

The opposite of %in%.
RESCALE

Rescale a variable (e.g., from 5-point to 7-point).
export

Export data to a file (TXT, CSV, Excel, SPSS, Stata, ...) or clipboard.
RECODE

Recode a variable.
dtime

Timer (compute time difference).
%nonein%

A simple extension of %in%.
cc

Split up a string (with separators) into a character vector.
bruceR-package

bruceR: BRoadly Useful Convenient and Efficient R functions
grand_mean_center

Grand-mean centering.
TTEST

One-sample, independent-samples, and paired-samples t-test.
%anyin%

A simple extension of %in%.
granger_causality

Granger causality test (multivariate).
show_colors

Show colors.
med_summary

Tidy report of mediation analysis.
set.wd

Set working directory to the path of currently opened file.
lavaan_summary

Tidy report of lavaan model.
formatN

Format "1234" to "1,234".
%allin%

A simple extension of %in%.
bruceR-demodata

Demo data.
granger_test

Granger causality test (bivariate).
formatF

Format numeric values.
print_table

Print a three-line table (to R Console and Microsoft Word).
%%COMPUTE%%

Multivariate computation.
regress

Regression analysis.
pkg_depend

Check dependencies of R packages.
pkg_install_suggested

Install suggested R packages.
RGB

A simple extension of rgb().
p

Compute p value.
model_summary

Tidy report of regression models.
theme_bruce

A nice ggplot2 theme that enables Markdown/HTML rich text.
scaler

Min-max scaling (min-max normalization).
rep_char

Repeat a character string for many times and paste them up.
import

Import data from a file (TXT, CSV, Excel, SPSS, Stata, ...) or clipboard.
group_mean_center

Group-mean centering.
formula_paste

Paste a formula into a string.
Run

Run code parsed from text.
formula_expand

Expand all interaction terms in a formula.
%partin%

A simple extension of %in%.
%^%

Paste strings together.