Intro to R

Outline

Version control review

What is R?

Diving in...

{version control review}

Using Github

Your machine

fork

Your copy

Starter repo

git clone

git add *

Edit files

Staging area

git commit -m ".."

git push

Viewing old versions

View commit log

Checkout old version

Return to current version

git log --oneline
git checkout cbe1de6
git checkout master

{R}

R

Built for working with data

Open-source

RStudio is a great IDE

Extraordinarily popular

R

How does it work?

Write a script (many lines of code)

Instructions are interpreted one at a time

Tell R to run your script

May have multiple scripts

What can it do?

Read data

Manipulate data

Analyze data

Visualize data

{diving in...}

Basic Syntax

Write a comment

Create a numeric variable

Create a string variable

# Start a comment with a "#" symbol
# Assign the value 2 to your variable x
x <- 2
# Assign the string "hello" to your variable x
x <- "hello"
x <- 'hello' # these do the same thing

{this is just a demo environment}

Starting R (interactive)

Type the letter r

Run an Rscript

Type Rscript filename.r

RStudio

Script

Console

Environment

Plots, packages, etc.

{vectors}

Vectors

Sequence of elements of the same type

Can be stored in a variable

Made by combining elements

x <- c(2,5,1,3)

Indexed starting at 1

x[1] # the number 2

Functions

Capabilities built into R

Accept a number of arguments

Return a single value

x <- c(2,5,1,3) # 2,5,1,3

function name 'c'

arguments

returns a vector

Documentation

# Generic
help(function_name)

# Example
help(c)
# Or (generic)
?function_name

# Example
?c

Packages

Additional set of R function

Used for analysis, manipulation, viz.

Need to be installed (once)

Need to be loaded (every time)

# Generic 
install.packages('package-name')

# Example
install.packages('stringr')
# Generic 
library(package-name)

# Example
library(stringr)

Lots of them!

Assignments

Assignment-2: Foundational skills (due Wed. 1/20)

r-intro

By Michael Freeman

r-intro

  • 582
Loading comments...

More from Michael Freeman