R Learning

Matrices

matrix(data_vector, byrow = TRUE, nrow = 3)

This fills the matrix with elements from data_vector.

rownames(m) <- foo
colnames(m) <- bar

This gives you neat row and column titles when you print the matrix.

Other stuff I learnt:

matrix[,2] # select second column

Factors

nominal (like C++ enum) or ordinal (like movie ratings).

ordinal:

factor(foo, ordered = TRUE, levels = c("bronze", "silver", "gold"))

Data Frame

data frame = table

str(foo) gives you
number of observations (rows), number of variables (columns)
variable: type and first few observations

foo_frame[,"col_name"]
foo_frame$col_name

foo_frame[foo_frame$rings, "names"]
# The first part selects rows, the second part selects columns.

# Get column "bar"
foo_frame$bar

Table

# To get count ratios
table(foo_frame$sex)

# To get column-wise ratios
table(foo_frame$sex, foo_frame$Survived)

# To get proportions
prop.table(table(foo_frame$sex, foo_frame$Survived), margin = 1)

Workspace

contents:

ls()

Reference

Datacamp Introduction to R

Created: May 9, 2016
Last modified: September 28, 2019
Status: in-progress notes
Tags: notes, R

comments powered by Disqus