Python R
Arithmatic Operators
Assignment : Defining a number a = 10 ; b = 25 a <- 10 ; b <- 25
Addition a+b a+b
Subtraction a-b a-b
Multiplication a*b a*b
Division a/b a/b
Power : ab a ** b a^b
Remainder a%b a %% b
Integer Division a // b a %/% b
Logical Operators
Short-Circuit logical AND a and b a && b
Short-Circuit logical OR a or b a||b
Element-wise logical AND a and b a&b
Element-wise logical OR a or b a|b
Logical NOT !a !a
Relational Operators
Equal a == b a == b
Less than a < b a < b
Greater than a > b a > b
Less than or equal a <= b a <= b
Greater than or equal a >= b a >= b
Not Equal a != b a != b
Root and Logarithm
Square root [Link](a) sqrt(a)
Logarithm, base e [Link](a) log(a)
Logarithm, base 10 math.log10(a) log10(a)
Exponential function exp(a) exp(a)
Round off [Link](a) round(a)
Generate random numbers
Uniform Distribution [Link]((10, )) runif(10)
Uniform numbers between a and b [Link](a,b,(10, )) runif(10, min = a, max = b)
Normal Distribution random.standard_normal((10, )) rnorm(10)
Vectors
Sequences
1,2,3,.....,10 range(1,11) seq(10) or 1:10
1,4,7,10 arange(1,11,3) seq(1,10,by = 3)
10,7,4,1 arange(10,0,-3) seq(from= 10, to= 1,by =-3)
Reverse a[: : -1] rev(a)
Concatenation
Concatenate two vectors concatenate((a, a)) c(a,a)
Add elements 1,2,3,4 concatenate((range(1,5), a), c(1:4 , a)
axis = 1)
Repeating
123,123 concatenate((a, a)) rep(a,times=2)
1 1 1, 2 2 2, 3 3 3 [Link](3) rep(a,each=3)
1,22,333 [Link](a) rep(a,a)
Maximium & Minimum
Pairwise max maximum(a,b) pmax(a,b)
max of all values in two vectors concatenate((a, b)) . max() max(a,b)
Vector Multiplication
Multiply two vectors a*a a*a
Vector dot product a . b dot( a , b )
©SrihariPramod
Python R
Matrices
Defining a Matrix
Define a matrix a = array([[2,3] , [4,5]]) matrix(c(2,3,4,5) , dim=c(2,2))
rbind(c(2,3) ,c(4,5))
Concatenation (matrices) ; rbind and cbind
Bind rows concatenate((a,b) , axis = 0) rbind(a,b)
vstack((a,b))
Bind columns concatenate((a,b) , axis = 1) cbind(a,b)
hstack((a,b))
Array creation
0 filled array zeros((3,3)) matrix(0,3,3)
Any number filled array array([[9,9] , [9,9]]) matrix(9,3,3)
Identity Matrix identity(3) diag(1,3)
Indexing and accessing elements (Python: slicing)
Element , (row,col) a[1,2] a[2,3]
First row a[0, ] a[1, ]
First column a[: , 0] a[ ,1]
Remove one column [Link]([0,2,3],axis = 1) a[ ,-2]
Clipping : replace element a[: , 0] = 99 a[ ,1] <- 99
Transpose and inverse
Transpose [Link]() . transpose() t(a)
Determinant [Link](a) det(a)
Inverse [Link](a) solve(a)
Rank rank(a) rank(a)
Sum (Python : Numpy)
Sum of each column [Link](axis=0) apply(a,2,sum)
Sum of each row [Link](axis=1) apply(a,1,sum)
Sum of all elements [Link]() sum(a)
Cumulative sum (Columns) [Link](axis=0) apply(a,2cumsum)
Sorting
Sort all elements flat [Link]() .sort() t(sort(a))
Sort each column [Link](axis=0) apply(a,2,sort)
Sort each row [Link](axis=1) t(apply(a,2,sort))
Sort, return indices [Link]() .argsort() order(a)
Maximun and Minimum
max in each column [Link](0) apply(a,2,max)
max in each row [Link](1) apply(a,1,max)
max in array [Link]() max(a)
Matrix - and elementwise - multiplication
Elementwise multiplication a * b or multiply(a,b) a*b
Dot product matrixmultiply(a,b) a %*% b
Outer product outer(a,b) outer(a,b) or a %o% b
Cross product cross(a,b) crossprod(a,b)
Matrix Size : Dimensions
Matrix dimensions [Link] dim(a)
Number of columns [Link][1] ncol(a)
Number of elements [Link] prod(dim(a))
Number of dimensions [Link]
©SrihariPramod
Python R
Data Frames (Python : Pandas)
Creating a Dataframe
Dataframe DF = [Link]( a = c(10,5,2)
{‘a’: [10, 5, 2], b = c(20,5,30)
‘b’: [20, 5, 30]} DF = [Link](a,b)
)
Import csv file as dataframe DF = pd.read_csv(“[Link]”) DF <- [Link](“[Link]”)
Accessing elements
Display first 5 rows [Link]() head(df,5)
Select first column df[‘a’] df$a or df[1]
Common functions
Summary [Link] summary(df)
Check datatypes [Link] str(df)
Structure [Link] str(df)
Programming
Reading from a file f = load(“[Link]”) f <- [Link](“[Link]”)
Import library functions from pylab import * library(mtcars)
Comment # #
Print print a print(a)
Script file extension .py or .ipynb(Jupyter notebook) .R
Conditionals :
If -else statement if m > 0 : if (m > 0){
a = 100 a = 100
else : }
a=0 else {
a=0
}
Loops : for i in range(1,6) : for (i in 1:5){
For loop print(i) print(i)
print(i*5) print(i*5)
}
Functions
Function : Even or odd def even_odd(x): even_odd = function(x){
if x%2 == 0: if (x %% 2 == 0){
y = ‘Even’ y = “Even”
else : }
y = ‘Odd’ else{
return y y = “Odd”
}
return(y)
}
©SrihariPramod