Skip to content

Instantly share code, notes, and snippets.

View aravindhebbali's full-sized avatar
🏠
Working from home

Aravind Hebbali aravindhebbali

🏠
Working from home
View GitHub Profile
@aravindhebbali
aravindhebbali / data_types_gist.R
Last active February 9, 2016 08:12
Gist for the R Data Types tutorial
# 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
@aravindhebbali
aravindhebbali / vectors_gist_1.R
Created February 9, 2016 11:16
R Data Structures: Introduction to Vectors - Part 1
# 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)
@aravindhebbali
aravindhebbali / vectors_gist_2.R
Created February 9, 2016 16:14
R Data Structures: Introduction to Vectors - Part 2
# 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)
@aravindhebbali
aravindhebbali / gist_matrix.R
Created February 11, 2016 07:33
R Data Structures: Introduction to Matrices
# 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
@aravindhebbali
aravindhebbali / gist_list.R
Created February 11, 2016 10:49
Working with Lists in R
# 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
@aravindhebbali
aravindhebbali / gist_numpy_1.py
Created February 12, 2016 10:01
Creating NumPy Arrays
# 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
@aravindhebbali
aravindhebbali / gist_numpy_2.py
Created February 15, 2016 03:52
NumPy Arrays: Selecting Array Elements
import numpy as np
# select array elements
np1 = np.arange(0, 10)
np1
# select 3rd and 5th elements
np1[2]
np1[4]
@aravindhebbali
aravindhebbali / gist_numpy_reshape.py
Created February 15, 2016 06:54
NumPy for Beginners: Reshaping NumPy Arrays
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)
@aravindhebbali
aravindhebbali / intro_to_tibble.R
Last active September 23, 2017 13:51
Code for Introduction to tibbles
# install library
install.packages('tibble')
library(tibble)
# create tibble
tibble(x = letters,
y = 1:26,
z = sample(100, 26))
# tibble features
@aravindhebbali
aravindhebbali / import_data_part_1.R
Created September 23, 2017 15:06
A Complete Guide to Importing Data into R - Part 1
# install
install.packages('readr')
install.packages('haven')
# library
library(readr)
library(haven)
# read_csv
read_csv('hsb2.csv')