Chapter 1 - Introduction to R Flashcards
Memorize common functions
Exponential/Log functions
exp(…) and log(x, base)
Install a package and load
install.packages(‘…’), library(package)
Scatter plot
plot(X,Y)
Histogram
hist(X)
Make a vector
x <- c(…)
Sum, Mean, Standard dev, length of a vector?
sum(), mean(), sd(), length()
Multiple plots at once? 1 row 2 col? 2 row 1 col?
par(mfrow = c(1,2)) and par(mfrow = c(2,1))
Make lines in a plot
lines(X,Y)
What order does R upcast to for vectors?
Boolean -> Numeric -> String
Check object types for numeric, factor, logical, and character.
as.”type”
Print a string without quotations.
cat(…)
Count the number of characters in a string.
nchar(a)
Character translation function
chartr(old, new, string)
Make a string upper case, lower case?
toupper(a), tolower(a)
Get a sub-string.
substr(beg,end,string)
Paste function
paste(a,b,sep = “”, collapse = “”)
Split a string
strsplit(a, split = “”)
Combine two vectors into one
c(a,b)
Make columns in a regular vector
c(‘name’ = c(…))
Make a sequence of numbers in R
seq(start,end,step) or a:b (for integers)
Repeat a vector
rep(x, integer for repeating the whole vector)
rep(x, vector for times of each item)
rep(x, each = a)
Make a matrix
x <- c(…); dim(x) <- c(row,col)
or
matrix(x,nrow, ncol, byrow, bycol)
Adjust the dimension names of a matrix.
rownames(x) <- c(…)
colnames(x) <- c(…)
names(dimnames(x)) <- c(…)
Combine columns and rows for two vecotrs/matrix
Example
cbind(A = 1:3, B = 4:6, C = 7:9)
rbind(A = 1:4, B = 4:6, C = 7:9)