Here is a R and Python syntax table, I have also included Numpy commands to make it more comparable. Where a cell is empty I could not find an equivalent.

Task | Python | Python Numpy | R |

sequence | x=[I for I in range(1,11)] | x <- 1:10 | |

scalar | x=1 | x=array(1) | x <- 1 |

vector/list | x=[1,2] | x=array((1,2)) | x <- c(1,2) |

constant vector | x=100*[1] | x=ones(100) | x <- rep(1,100) |

append | x.append(1) | x <- c(x,1) | |

matrix | x=[[1,2],[3,4]] | mat([[1,2],[3,4]]) | x <- matrix(c(1,2,3,4),ncol=2,byrow=TRUE)) |

column stack | hstack((x,y)) | cbind(x,y) | |

row stack | vstack((x,y)) | rbind(x,y) | |

for | for I in range(1,11): print I | for I in c(1:10)) { print(I) } | |

while | I=1 while (I<10): I+=1 | I <- 1 while (I<10) { I <- I+1 } | |

if | if I==10: print 'Yes' else: print 'No' | if (I==10) { print('Yes') } else { print ('No') } | |

length | len(x) | len(x) | length(x), nrow(x) |

columns | len(x[0]) | x.shape[1] | ncol(x) |

dimension | x.shape | dim(x) | |

summary | summary(x) | ||

read csv | import csv reader=csv.read(open('file','r')) mydata=[] for line in reader: mydata.append(line) | mydata <- read.csv("file", header=TRUE) | |

write csv | import csv writer=csv.writer(open('file','w')) for d in data: writer.writerow(d) | write.csv(data, file="file", row.names = FALSE) | |

sum | sum(x) | sum(x) | sum(x) |

select element | x[1][1] | x[1,1] | x[2,2] |

last element | x[-1] | x[-1] | x[-1] |

select column | x[:,1] | x[,2], x$Name | |

correlation | corrcoef(x,y) | cor(x,y) | |

mean | mean(x) | mean(x) | |

function | def func(x): print x return x | func <- function(x) { print(x) return(x) } | |

dot product | dot(x,b) | x*b | |

transpose | transpose(x) | t(x) | |

matrix product | b*x | t(b) %*% x | |

random | random.random() | random.rand(1) | runif(1,0,1) |

sort | x.sort() | sort(x) | |

help | help(command) | help(command) | help(command), ??command |

## 2 comments:

This looks like a nice resource, thanks for sharing. BTW, it looks like you have a slight error/omission on the dot product for R. You have x*b... it should really be sum(x*b).

nice resource indeed,

i would have wanted more

it's a little unfair for numpy,

because numpy has methods for reading and writing text files (csv and whatnot)

pandas should be here in comparison,

especially with R,

because it's statistics oriented and extremely useful

Post a Comment