R cheat sheet - GitHub Pages

R cheat sheet

By @MaryJoWebster March 2019 Data frame ? This is from Base R; this is used for storing data tables. Tidyverse makes a "tibble" which is supposed to be a slightly better version of a data frame.

% This is a pipe operator used in Tidyverse to connect actions. (Cmd+Shift+M on a Mac)

See the Tools menu in RStudio and look at Keyboard Shortcuts (the key strokes are different for Windows and Mac)

df$columnname --- You might see this syntax occasionally. This is how to be explicit about exactly which dataframe a column is coming from. It's the name of the data frame with a dollar sign and then followed by the name of the column.

Stringr::sub_str() -- you might see syntax like this. This is how to be explicit about which package a function is coming from. In this case it's the "stringr" package and sub_str is the name of a function from that package. getwd() --how to find out what your working directory is. You can type this in any file (script, markdown, etc) or in the console.

Dplyr basic syntax: Select(columnname, columname)

Filter(columname== `value')

Group_by(columnname)

Summarize( newcolumnname = n() ) --- this will count the records Summarize( newcolumnname = sum(columnname) ) -- this will sum the values in a column

Summarize( newcolumnname = mean(columnname) ) -- this will average values in a column

Arrange(columnname) -- this will sort your results by column specified, ascending

Arrange(desc(columnname) ? this will sort descending

Mutate( newcolumnname = .......) ) -- this is how you add a new column

How to calculate a percent when grouping data: Df %>% group_by(statename) %>% summarize(count = n() ) %>% mutate(pct = count/sum(count) #note- the mutate line is grabbing the "count" field created in the summarize line and dividing it by the sum of all the values in "count"

How to add a new column to a data frame using mutate. For this example, let's assume we have two columns we want to add together to create a "total" column.

df % mutate(total = column1 + column2)

Importing using Tidyverse: Readr package in Tidyverse comes with 5 "parsers" for importing different text file types. (Note that BaseR has a function called "read.csv" that functions very differently) read_csv() ? for comma-delimited files read_tsv() ? for tab-delimited files read_fwf() ? for fixed-width files read_log() for web log files If you need to import an Excel file, you need the readxl package from Tidyverse (see below) Syntax for read_csv. Make sure your data file is in your working directory. Df ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download