R Data Frame Cheat Sheet

Work with strings with stringr:: CHEAT SHEET Detect Matches strdetect(string, pattern) Detect the presence of a pattern match in a string. Strdetect(fruit, 'a') strwhich(string, pattern) Find the indexes of strings that contain a pattern match. Strwhich(fruit, 'a') strcount(string, pattern) Count the number of matches in a string. R has 657 built in color names To see a list of names: colors These colors are displayed on P. R color cheatsheet Finding a good color scheme for presenting data can be challenging. This color cheatsheet will help! R uses hexadecimal to represent colors Hexadecimal is a base-16 number system used to describe color. Data Visualization with ggplot2:: CHEAT SHEET ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same components: a data set, a coordinate system.

Welcome to the R cheat sheet.

Here you’ll find the most useful functions and techniques I’ve found in R all in one place.

This post is a work in progress – new functions will be catalogued in here as and when.

This cheat sheet assumes you have RStudio downloaded and set up on your computer.

RStudio has the mtcars dataset pre-loaded. This will be the example dataset for much of the cheat sheet.

Remember that there are often multiple ways with R to arrive at the same result!

I want to:

By function:

Reading data from a spreadsheet up into R

To read data from your CSV file, use the read.csv formula

Seeing the structure of your data

The str function gives you an overview of how your data looks.

Seeing a sample of your data

See the beginning, end or a random bit of your dataset.

Filter your data frame

Filter your data frame based on the data in one column. Thanks to Sharon Machlis for the tips in searching for numbers and strings here.

Merge data from several columns into one column vertically

We can use the gather() function to take data from several horizontal columns and display it vertically, without losing the distinctions in the column names. This is great for moving data spread across several years into a single year, allowing for plotting on geom_line() Can you get microsoft word on a macbook. in ggplot2.

Merge several identical datasets together, one underneath another

Use rbind(). Make sure your data frames have the same number of columns, named identically.

Do a find and replace

Print off a CSV file from a R data frame

It will print off a CSV file into your current directory.

Order your data frame

Make sure you remember the comma at the end or you’ll get an error
the default order is ascending. Put a minus sign in front of column_to_sort to sort descending. Remember this will not plot in order in ggplot2.

To sort your data quickly, an alternative way is to click on your data frame in the ‘data’ section of RStudio.

From there, a new tab will open to the right of your script. Click on that to get a spreadsheet-style visualisation of your data. Click on one of the tabs to sort the data in ascending order, then again to sort descending. Remember – this won’t sort your data permanently.

Remove any duplicate values from your data frame

R merge dataframes cheat sheet

Remove an object from your environment

R Data Frame Cheat Sheet

#use rm() as follows

to_delete <- data.frame(NA)
#data frame is deleted from the environment

R Data Frame Cheat Sheet

You can also use the grid option in RStudio, in the top-right corner, to select multiple objects for deletion easily

Test to see whether a statement is TRUE or FALSE

R Merge Dataframes Cheat Sheet

Format data as numeric values

Change some data into date format

Cheat Sheet For R

Use the as.Date() function to turn a character date format into a R date format.

R Data Frame Cheat Sheet Pdf

The format argument of as.Date() should detail the format of your date, including any spaces, slashes etc. For details on all possible date formats, go here.