diff --git a/book/in_memory/pitfalls.qmd b/book/in_memory/pitfalls.qmd index 8ec1ba3..0cab45e 100644 --- a/book/in_memory/pitfalls.qmd +++ b/book/in_memory/pitfalls.qmd @@ -1,12 +1,11 @@ --- -title: In-memory interoperability +title: Pitfalls when using both Python and R engine: knitr --- -## Pitfalls when mixing Python and R Python and R are very different programming languages. Here are some pitfalls you might encounter when mixing both languages. -### Column major vs row major +## Column major vs row major Matrices are stored contiguously in-memory, and are adressed by a single memory addresses, instead of multiple indices along the axis. A translation needs to happen between this single adress and the indices along the axes, and how that translation happens depens on how the matrix is represented in-memory. ![Different in-memory representations](images/inmemorymatrix.png){#fig-imm-matrix} @@ -16,13 +15,22 @@ There is usually no issue when converting R matrices to Python matrices: reticul If you notice something amiss with your matrices, check whether you need to transpose them or change the row/column major attribute. -### Indexing: 0-based or 1-based +## Indexing: 0-based or 1-based Take care to remember that arrays and matrices in Python are indexed starting from 0 (as in, index 0 refers to the first element), while R uses 1-based indexing, where index 1 refers to the first element. -### Dots in variable names +## Dots in variable names In R it is very common to use dots in symbols and variable names. This is invalid in Python: dots are used for function calls. + When using rpy2, these dots are usually translated to underscores `_`. If this does not happen automatically, you can specify mappings for these symbols. -### Integers and floating point numbers +## Integers and floating point numbers In R, unless you explicitly specify, any number is represented as a floating point number. Python can be more strict about using integers or floating point numbers. + +```{r int_example} +float_ex <- 12 +int_ex <- 12L + +cat(is.integer(float_ex)) +cat(is.integer(int_ex)) +```