Introduction to R
Intro to basics
- basic types
- numerics
- integers
- logical
- characters
Vectors
names()
sum()
- logical comparison operators
-<
for less than
->
for greater than
-<=
for less than or equal to
->=
for greater than or equal to
-==
for equal to each other
-!=
not equal to each other
Matrices
matrix()
rowSums()
colSums()
cbind()
rbind()
Example:
ticket_prices <- matrix(c(5, 5, 6, 6, 7, 7), nrow = 3, byrow = TRUE, dimnames = list(movie_names, col_titles))
Factors
factor()
levels()
summary()
- two types of categorical variables:
- nominal categorical variable
- ordinal categorical variable
Example:
factor(some_vector, ordered = TRUE, levels = c("Level_1", "Level_2" ...))
Data frames
head()
tail()
str()
data.frame()
subset()
a[order(a)]
Lists
list()
Example: select the second component,the first element
shining_list[[2]][1]
Conclusion
- Vectors (one dimensional array): can hold numeric, character or logical values. The elements in one vector all have the same datatype.
- Matrices (two dimensional array): can hold numeric, character or logical values. The elements in one matrix all have the same datatype.
- Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type.
- List: a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other.