Top packages for R programming

A quick and easy guide with examples


1 dplyr

`dplyr` is a popular R package for data manipulation, known for its intuitive syntax and verbs that make it easy to transform datasets.

Useful functions

  • filter(): Select rows that meet specific conditions.

  • select(): Choose specific columns from a data frame.

  • mutate(): Create new columns or modify existing ones.

  • arrange(): Order rows based on column values.

  • summarise(): Reduce multiple rows to a single summary row.

  • group_by(): Group data by one or more columns for aggregation.

Examples


2 ggplot2

ggplot2 is a widely-used package for data visualization, providing a powerful system to create complex plots.
  • ggplot(): Initialize a plot object.

  • geom_point(): Create scatter plots.

  • geom_line(): Draw lines to show trends.

  • geom_bar(): Create bar charts.

  • facet_wrap() / facet_grid(): Create subplots based on factors.

  • labs(): Customize labels and titles.

3 forcats

forcats provides tools for working with categorical data (factors), making it easier to reorder, create, and modify factor levels.

Useful functions

  • fct_relevel(): Manually change the order of factor levels.

  • fct_reorder(): Reorder factor levels based on another variable.

  • fct_infreq(): Reorder levels by their frequency.

  • fct_rev(): Reverse the order of factor levels.

Example code

4 stringr

stringr provides functions to work efficiently with strings, including pattern matching, replacement, and splitting.

Useful functions

  • str_c(): Concatenate strings together.

  • str_detect(): Detect the presence of a pattern.

  • str_sub(): Extract substrings from a string.

  • str_to_upper: Changes lowercase text to uppercase text.

  • str_replace(): Replace matching patterns in a string.

  • str_split(): Split strings into substrings.

Example code

5 gtExtras

gtExtras extends the gt package to add more flexibility in styling tables with additional formatting options and features.
  • gt_color_box(): Add color shading to cell values.

  • gt_highlight_rows(): Highlight specific rows in a table.

  • gt_plt_sparkline(): Add sparklines to table cells.

  • gt_fa_repeats(): Add font-awesome icons as repeat markers.

6 plotly

plotly is a package for creating interactive web-based plots, often used to enhance visualizations initially created with ggplot2.

Useful functions

  • plot_ly(): Create a new interactive plot.

  • ggplotly(): Convert ggplot2 plots to interactive plots.

  • layout(): Customize the layout of a plotly object.

  • add_trace(): Add a trace to an existing plot.

Example code

7 lubridate

Watch this space.. content about how to work with date and time data using the lubridate pack will be added soon.

8 Learn more

Courses that contain short and easy to digest video content are available at LearnMore365.com Each lessons uses data that is built into R or comes with installed packages so you can replicated the work at home. LearnMore365.com also includes teaching on statistics and research methods.