Data Science Languages Cheetsheet
This is a table comparing syntax in R, Python, and Matlab/Octave
Language Reference for R
Language Reference for Python
Language Reference for Matlab
Cheatsheet for R
Cheatsheet for Python 2.7
Cheatsheet for Python 3
Cheatsheet for Octave
Amost always imports
R | Python | Matlab/Octave |
---|---|---|
library(tidyverse) | import numpy as np | |
import pandas as pd |
Basics
Construct | R | Python | Matlab/Octave |
---|---|---|---|
help on “func” | ?func | help(func) | doc func |
comment | # comment | # comment | % comment |
import | library | import | import |
string | “it’s” or ‘it”s’ | “it’s” or ‘it”s’ | “it’s” or ‘it”s’ (strings or chars) |
int | 1234L | 1234 | 1234 |
longint | none | switches transparently even to bigints |
int64(1234) |
type of x | class(x) | type(x) | class(x) |
Formatted print | print(sprintf(“pi:%$.2f”,pi)) | print(“pi:%.2f” % (numpy.pi)) | fprintf(“pi:%.2f”,pi) |
Control
Construct | R | Python | Matlab/Octave |
---|---|---|---|
for loop | for (j in 1:5){print(j)} | for j in range(6)): print(j) |
for j=1:5 disp(j) end |
list comprehension | lst = [j for j in range(5)] | ||
while loop | while(TRUE){ print(j) break } |
while True: print(j) break |
while true j break; end |
function | f <- function(x,y){ x+y } | def f(x,y) x+y |
function val = f(x,y) val = x + y; |
Arrays and Matrices
Construct | R | Python | Matlab/Octave |
---|---|---|---|
array | a = 1:3 | a = np.array([1,2,3]) | a = [1,2,3] |
matrix | m = matrix(1:4,2,2) | m = np.matrix(‘1 2; 3 4’) | m = np.matrix(‘1 2; 3 4’) |
number of elements | length(m) | np.size(m) | numel(m) |
dimensions | dim(m) | np.shape(m) | size(m) |
list | l <- list(c(1,2,3)) | lst = [1,2,3] | not used much I think |
Data Frames might be Tables in Matlab
Intro to pandas 10 minutes to pandas
Data Frames
Construct | R | Python | Matlab/Octave |
---|---|---|---|
Simple Data Frame | df <- data.frame(x=c(1,2,3),y=(4,5,6)) | df = pd.DataFrame({ ‘x’ : [1,2,3],’y’:[4,5,6] }) | ? |
Read csv | df <- read.csv(“mydata.csv”) | df = pd.read_csv(‘foo.csv’) | ? |
Extract column | x <- df$x | x = df.x | ? |