R_vs_Python
R_vs_Python
Arithmatic Operators
Assignment : Defining a number a = 10 ; b = 25 a <- 10 ; b <- 25
Remainder a%b a %% b
Not Equal a != b a != b
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)
Concatenation
Concatenate two vectors concatenate((a, a)) c(a,a)
Vector Multiplication
Multiply two vectors a*a a*a
©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)
©SrihariPramod
Python R
Data Frames (Python : Pandas)
Creating a Dataframe
Dataframe DF = pd.DataFrame( a = c(10,5,2)
{‘a’: [10, 5, 2], b = c(20,5,30)
‘b’: [20, 5, 30]} DF = data.frame(a,b)
)
Common functions
Summary df.describe summary(df)
Check datatypes df.dtypes str(df)
Structure df.shape str(df)
Programming
Reading from a file f = load(“data.txt”) f <- read.table(“data.txt”)
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
}
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