Está en la página 1de 2

Informática - 1º de Física

Computación Científica (Alberto Ruiz)


En la siguiente tabla se compara la sintaxis de las construcciones de programación básicas en
Python y R.

PYTHON R
-------------------------------------------------

x = 3 x <- 3

True, False T, F
TRUE, FALSE

** ^
// %/%
% %%
@ %*%

v = [2,3,4]
v = np.array([2,3,4]) v <- c(2,3,4)

z = 2+3j z <- 2+3i

1:5
range(5) seq(5)
0,1,2,3,4 1,2,3,4,5

np.arange(4,7,0.5) seq(4,7,by=0.5)
4 4.5 5 5.5 6 6.5 4 4.5 5 5.5 6 6.5 7

np.linspace(2,3,11) seq(2,3,length=11)
2 2.1 2.2 ... 2.9 3

m = np.array( [[1,2,3], m <- matrix( c(1,2,3,


[4,5,6]] ) 4,5,6), 2, 3, byrow=T)

m.shape nrow(m), ncol(m), dim(m)

m.T t(m)

hstack, vstack cbind, rbind

np.linalg.solve solve

m[:,2] m[,3]

np.random.randn rnorm
np.random.rand runif
np.random.randint sample(1:6, 10, replace=T)
if cond: if (cond) {
B1 B1
else: } else {
B2 B2
}

while cond: while (cond) {


B B
}

for k in s: for (k in s) {
B B
}

def fun(x,y): fun <- function(x,y) {


B B
return v v
}

global x x <<- 3
x = 3

{'a': 3, b: '4'} list(a=3, b=4)

[ f(x) for x in s if p(x) ] sapply( Filter(p, s) ,f)

https://notebooks.gesis.org/binder/jupyter/user/albertoruiz-inforfis-k0dkac3q/notebooks/notebooks/
intro-R.ipynb

También podría gustarte