R

Introduction to R

This guide is a note for datacamp, introduction to R.

I usually use Python, thus in this note, I used Python to contrast R on data collection to data cleaning to data frame…

R Python
Arithmetic with R
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponentiation: ^
Modulo: %%

Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponentiation: pow()
Modulo: %
Variable assignment
my_var <- 4
Variable assignment
my_var = 4
What’s that data type?
class()
Data Type
type()
Create a vector
the combine function c()
numeric_vector <- c(1, 2, 3)
character_vector <- c(“a”, “b”, “c”)
Create a vector?
In python, we have List: []
Naming a vector
names()
some_vector <- c(“John Doe”, “poker player”)
names(some_vector) <- c(“Name”, “Profession”)
Naming a vector?
I think we normally use another list And we use zip() function to put two lists together. Or we use dict()
One unique feature: vectors can be added together. If a <- c(1, 2, 3), b <- c(4, 5, 6), then a + b would be c(5, 7, 9) Lists added together would be like stick together, instead of adding each element one by one. For example, a = [1, 2, 3], b = [4, 5, 6], a + b would be [1, 2, 3, 4, 5, 6]
Some functions
sum()
max()
min()
mean(na.rm = FALSE) # na.rm: drop NA data
abs()
round()
Some functions
sum()
max()
min()
np.mean()
Index of vectors
In R, we start from 1.
First element, vector_sample[1].
First to third element, vector_sample[c(1,2,3)] or vector_sample[1:3]
Index of vectors
In python, we start from 0.
What’s a matrix?
matrix(1:9, byrow = TRUE, nrow = 3)
using byrow, we would fill the matrix by rows!
Python doesn’t have this function. We use pandas.DataFrame or numpy to make a matrix, and we can use numpy.reshape.
Naming a matrix
rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector
rowSums()
colSums()
In Python, we use dataframe, it works like this df.index = row_names_vector df.columns = col_names_vector