# f(x)

## Using Github

git clone

### Staging area

git commit -m ".."

git push

## Vectors

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

### Indexed starting at 1

x[1] # the number 2

### Length (length)

x <- seq(1,5) # 1,2,3,4,5
x <- 1:5 # 1,2,3,4,5
x <- rep(2, 4) # 2,2,2,2
length(c(1,2)) # 2

## Recycling

### Shorter vectors get recycled

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

## Vector Manipulation

### Use a vector of Boolean values

x <- c('one','two','three')
x[2] # 'two'
x[c(1,2)] # 'one','two'
x[1:2] # 'one','two'
x[c(TRUE, FALSE, TRUE)] # 'one','three'
x <- c(1,2,3,4,5)
indices <- x > 3 # FALSE,FALSE,FALSE,TRUE,TRUE
x[indices]#3,4,5
x[x>3] #3,4,5

## Functions

### Return a single value

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

## Writing functions

### Example

function_name <- function(arguments) {
statements
return(object)
}
greet <- function(name) {
obj <- paste('Good morning', name, 'today is', date(), sep=" ")
return(obj)
}

greet("Mike") # Good morning Mike today is Thu Jan 14 15:24:26 2016

## Conditional Statements

### Only execute code under a condition

if (SOME CONDITION IS TRUE) {
do something
}

### Perform different actions under different conditions

if (SOME CONDITION IS TRUE) {
do something
} else if (SOME OTHER CONDITION IS TRUE) {
do something else
} else {
do a default action
}

## Conditional Statements

if(condition1) {
statements
} else if (condition2) {
statements
} else {
statements
}

## Conditions

### Conditions must be boolean (TRUE, FALSE)

# Boolean
if(TRUE) {
print("it's true!")
}

# Evaluated as a Boolean
if(x > 1) {
print("x is greater than one!")
}

## Assignments

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

#### r-2

By Michael Freeman

• 953