Introduction to R Flashcards
Assignment
-
help(Syntax)
- Function with argument that lists all of the operators in R.
mode()
- Function without argument that revels storage.
typeof()
- Function without argument that reveals type of object.
- Similar to is(argument) function.
setwd()
- Function without argument that changes your working directory.
getwd()
- Function without argument that gets the current file path of your working directory.
Object
- A storage space with an associated name.
- Everything in R is stored as an object.
Object Naming Protocols
1) Case sensitive names.
2) Must start with a period or letter.
- If starts with a period, a letter must follow.
3) Periods can be found anywhere in an R name but the underscore cannot start a name.
4) Names cannot have spaces.
5) Names almost never take quotes around them.
Functions
- A special type of R object that exist / are created to do something.
- Usually take arguments but can also have no arguments.
Vectors
- An ordered sequence in R.
- No scalars exist in R = a single value is an vector of length 1.
- [1] denotes the first element on a vector in R.
- Two types: atomic and generic vectors.
Atomic Vectors
- A vector containing only one data type.
Generic Vectors (Lists)
- A vector that can contain more than one data type.
Generating Vectors
- Can use two different functions:
1) seq()
2) c()
3) :
:
- Known as the colon operator.
- Generates a sequence from:to in steps of 1.
- Can be in either increasing or decreasing order.
seq()
- Known as the sequence function.
- Format of seq(from,to,by).
- Technically constructed as seq (from = a, to = b, by = c) but this is long.
- Can be in either increasing or decreasing order.
c()
- Known as the concatenate function.
- Combines / concatenates values into a vector.
- Output type determined by the highest type among vector components and follows the rule: character > integer > logical.
1) If any value is a character, the vector becomes a character vector.
2) If no characters, and a mix of integer and logical, becomes an integer vector where logicals are converted into integers.
class(argument)
- Known as the class function.
- Gives the data type of the vector.
vector1[vector2]
- Selects elements of vector1 whose indices are given by vector2.
- vector2 is known as the index vector.
Matrix
- An atomic vector with row and column indices.
- All rows and columns must be of the same size but can have different # of row and columns.
- Elements accessed via indexing.
Accessing Matrix Elements (Few Examples)
1) objectname[r,c] = access a specific cell.
2) objectname[r,] = access all columns of a specific row.
3) objectname[,c] = access all rows of a specific column.
dim(argument)
- Known as the dimension function.
- Gives the dimension of a matrix input.
is.matrix(argument)
- Gives the data type of the matrix.
rowbind(argument)
- Function that assigns names to the rows, starting from row #1.
cbind(argument)
- Function that assigns names to the columns, starting from column #1.