User:Zadrali/PythonR

Below are materials for my new book, Python to R and R to Python dictonary

Preliminary
This compares Python 3 and R. There are remarks when Python 2 is different from Python 3.

Arithmetics
Four arithmetic operations:
 * Python:
 * R: same

Note: In Python 2,  means integer division if x and y are integers. In Python 3 and R,  means real division always.

Power:
 * Python:  only (  is bitwise xor)
 * R:

Integer division and remainder:
 * Python: ,
 * R: ,

Mathematical functions:
 * Python:
 * After :   etc.
 * After :   etc.
 * R:  etc. (no need to import any libraries)

Libraries
Installing: Using:
 * Python: use  (for global installation) and possibly   (for local installation)
 * R:  (locally)
 * Python:
 * - then everything from this package should be qualified, e.g.
 * - then everything from this package should be qualified with prefix  instead of
 * - then everything from this package can be used without qualification
 * R:  or
 * If "foo" does not exist, the former returns error, the latter returns.

Printing variables
Simple printing: Notes:
 * Python:
 * R:
 * ( prints only one value).
 * ( does not print newline by itself)
 * both Python's  and R's   and   have additional flags, see   in both languages and   in R.
 * In Python 2,  should be used instead, as   was a language feature rather than a function.

Formatted printing:
 * R:
 * Python:

Note: the alternative way to do this in Python is Here  and   mean "0-th value in format %d" and "1-st value in format %f", respectively. You can also use,   etc. for arguments in default format.

Vectors, matrices, arrays
In R, vectors, matrices, and arrays are integral part of the language. In Python, they require  library. Usually it is called as:

In Python, there are no vectors or matrices, only arrays. (Strictly speaking, there is a type np.matrix, but it is seldom used.) In R, vectors, matrices, and arrays are different kinds of data. Arrays are seldom used unless one needs more than 2 dimensions.

Types:
 * Python:
 * R:,  ,

General considerations
Python: indices start with zero. For instance, the very first element of a 2d array  is.

R: indices start with one. For instance, the very first element of a matrix or 2d array  is.

Python: arrays are stored row-wise. For instance, the elements of a 10*10 array  are stored as , and so on.

R: matrices are stored column-wise. For instance, the elements of a 10*10 matrix  are stored as , and so on. Arrays are stored similarly, for instance, the elemens of a 2*2*2 array  are stored in the following order:.

Filled by zeros, ones, or any value
Vector, matrix, or array filled by zeros:
 * Python:  (for 3*4*5 array filled by zeros)
 * For 1d arrays, we can use  instead of
 * There is also  which makes the same structure as x filled by zeros.
 * R:
 * For vectors:
 * For matrices:
 * For arrays:

Vector, matrix, or array filled by ones:
 * Python: as above, but use  instead of   (or   instead of
 * R: as above, but use 1 instead of 0.

Vector, matrix, or array filled by any value:
 * Python:  - 3*4*5 array filled by 7's (since numpy 1.8)
 * Use  to determine the numpy version.
 * Use  for older versions
 * R: as above, but use the desired value instead of 0 or 1.

Filled by elements which you supply
Make a vector from given elements:
 * Python:  (or  )
 * R:

Make a matrix from given elements:
 * Python:  (tuples can also be used).
 * R:  or
 * Note: in R, the ordering of elements is different, see below.

Make an array from given elements:
 * Python: as above ( of nested lists or tuples)
 * R:

Uninitialized, can be filled by arbitrary elements
Uninitialized vector, matrix, or array, filled with arbitrary values:
 * Python:
 * R: no such thing

Diagonal matrix
Diagonal square matrix (1 on main diagonal, 0 otherwise):
 * Python:
 * R:

Diagonal square matrix (arbitrary vector on main diagonal, 0 otherwise):
 * Python:
 * R:

Arithmetic progression

 * R:
 * or  produces
 * or  produces
 * produces
 * Python:
 * produces
 * produces
 * produces

Random elements
Vectors, matrices, and arrays filled with random values (standard normal distribution):
 * R:
 * - vector of 5 randoms
 * - 4*5 random matrix
 * - 3*4*5 random array
 * Python:  or   - 3*4*5 random array

Vectors, matrices, and arrays filled with random values (standard uniform distribution):
 * R: use  instead of
 * Python: use  instead of , or   instead of

Getting or modifying elements
Note: all of the below can be used together, e.g.  works in R (and similarly in Python).

One element
One element:
 * Python:
 * R: same (recall however that R starts indices from 1)

n-th element in matrix or multidimensional array, in the order of storage:
 * Python: . For example, if   is a 10*10 array, then   is the same as
 * R: . For example, if   is a 10*10 matrix, then   is the same as
 * both can be used to modify the element, e.g.  works in Python.

Last element over some dimension:
 * Python:  - last element of an 1d array  ;   - it's second last element;   - 5th element in the last row of a 2d array   etc.
 * R: no such shortcut. For k-th last element of a vector use .  For 5-th element of the last row, use , etc.

Many elements
Subrange of dimension (slice):
 * Python:  selects elements ,  ,   (but not 3,7)
 * R:  or   selects elements ,  ,  , and

Slice with stride:
 * Python:  selects elements 5,7,9.
 * R:  selects elements 5,7,9.
 * Note: Python also has following shortcuts:
 * - all elements from n-th to last (R's expression for this is
 * is the same as  (no such shortcut in R)

All elements over some dimension:
 * Python:  or  ,
 * Python also has an ellipsis shortcut, which means "as many colons as neccessary", e.g. for 5d array,  is the same as  . There is no such thing in R.
 * R: ,

Group of elements given by their indices:
 * Python:  (inds could also be a list)
 * R:
 * both can be used to modify the element, e.g.  works in both languages

Group of elements given by a boolean array:
 * Python:
 * R: