Posit Recipes
Some tasty R code snippetsR Basics
Do basic tasks with R, like import data and call functions.
Transform Tables
Do things like filter, sort, and pivot your tables of data.
- Extract columns from a table as a new table
- Rename columns in a table
- Add a column to a table and compute its values
- Compute summary statistics for a table
- Compute summary statistics for groups of rows within a table
- Filter your table to rows with specific values
- Group a continuous variable into categories
- Sort a table by row
- Create a two-way contingency table
- Create a one-way frequency table
- Create a three-way contingency table
- Pull first or last observation of each group from a table
Visualize Data
Plot your data in various ways.
- Visualize two vectors using a scatterplot
- Visualize data with a scatterplot
- Visualize counts with a bar chart
- Visualize grouped counts with a stacked bar chart
- Visualize a distribution with a histogram
- Visualize ordered continuous data with a line chart
- Visualize distributions with boxplots
- Change the axis labels of a plot
Dates & Times
Work with date-time data.
- Parse a string to date or datetime
- Parse datetimes with mixed formats
- Manually create a date with make_date()
- Extract components of dates and datetimes
- Extract the day of the week from dates or date-times
- Extract a month as a name or abbreviated name
- Summarize data over units of time
- Update date-times
- Round a date or datetime
- Display date-times in a new time zone
- Replace the time zone of a date-time
- How should I use R to represent the passage of time?
- Add a duration to a date-time
- Create a duration
- Add a period of time to a datetime
- Create a period
- Create an interval
- Check if a date or time lies within an interval
- Check whether two events or processes overlap
- Measure the length of an interval with a specified unit of time
- Use dates to build customized character strings
- Reformat a date as a character string
Factors
Work with categorical data in R.
Strings
Work with character strings in R.
- Find the length of a string
- Remove unwanted whitespace from a string
- Pad a string
- Combine strings
- Collapse a vector of strings
- Build strings from text and data
- Remove and replace specific patterns in strings
- Extract a portion of a string
- Visualize matches in a string
- Write a regular expression that uses a quantifier
- Match a set of characters
- Match the start or end of a string
- Refer to a matched pattern within one regular expression
- Use look arounds to find matches
- Determine if a string contains a pattern
- Extract strings that match a pattern
- Extract a pattern from a string
- Count the number of matches of a pattern in a string
- Change the case of a string
Model Data
Fit basic models and work with the output.
- Make a table of metrics related to a model’s components
- Make a table of metrics related to a model’s goodness of fit
- Make a table of metrics related to the data points used to train/fit a model
- Make a table of model predictions given new input/test data
- Perform an analysis of variance (ANOVA)
- Build a survival object to use in survival models
- Generate and plot a Kaplan Meier curve
- Check for significant differences between survival curves
- Fit a Cox proportional hazards model
Figures & Tables
Polish tables for display and publication.
- Create a table for publication
- Format the column names of a table for publication
- Format the title of a table for publication
- Format footnotes in a table for publication
- Add a label that spans several columns in a table for publication
- Add a label above the row names in a table for publication
- Format values in a table for publication
- Add color based on cell values in a table for publication
- Format missing values in a table for publication
- Create groups of rows in a table for publication
- Add summary rows to the bottom of a table for publication
- Style cells and values in a table for publication
These recipes were created by the Posit Academy team. Learn more about Posit Academy.