This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assign value 3 to variable radius | |
radius <- 3 | |
# assign value 3.14 to variable pi | |
pi <- 3.14 | |
# compute the area of the circle and assign it to | |
# a variable of the same name | |
area <- pi * radius * radius |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a numeric vector using c() | |
num_vect <- c(1, 2.3, 5) | |
num_vect | |
# test the data structure | |
is.vector(num_vect) | |
# test the data type | |
class(num_vect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vector coercion | |
# case 1: create a vector of different data types | |
vect1 <- c(1.5, 1L, 'one', TRUE) | |
vect1 | |
# test data type | |
class(vect1) | |
# case 2: create a vector of different data types excluding character | |
vect2 <- c(1.5, 1L, TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax | |
args(matrix) | |
# numeric matrix of 3 rows filled by columns | |
mat <- matrix(data = 1:9, nrow = 3, byrow = FALSE) | |
mat | |
# error in specifying rows/columns | |
mat1 <- matrix(data = 1:9, nrow = 2) | |
mat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example 1: list containing vector and matrix | |
vect1 <- c('Jack', 'Jill', 'John') # character vector | |
mat <- matrix(sample(12), nrow = 3) # 3 x 4 matrix | |
list1 <- list(vect1, mat) # list of 2 objects | |
list1 | |
# example 2: coerce vector using as.list function | |
# numeric vector | |
vect1 <- 1:5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import numpy | |
import numpy as np | |
# create numpy array from lists | |
np1 = np.array([1, 3, 5, 7, 9]) | |
np1 | |
# use the range function to create array | |
np2 = np.array(range(6)) | |
np2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# select array elements | |
np1 = np.arange(0, 10) | |
np1 | |
# select 3rd and 5th elements | |
np1[2] | |
np1[4] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# one dimensional array | |
np1 = np.arange(0, 12) | |
np1 | |
# reshape() method | |
# modify to 3 x 4 two dimensional array | |
np1.reshape(3, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install library | |
install.packages('tibble') | |
library(tibble) | |
# create tibble | |
tibble(x = letters, | |
y = 1:26, | |
z = sample(100, 26)) | |
# tibble features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install | |
install.packages('readr') | |
install.packages('haven') | |
# library | |
library(readr) | |
library(haven) | |
# read_csv | |
read_csv('hsb2.csv') |
OlderNewer