library(reticulate)
Cargamos datos IRIS en chunk de R
datos <- iris
Vemos el encabezado de los datos
head(datos)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
En chunk de R disponemos los datos para Python
datos_py <- r_to_py(datos)
En chunk de Python recuperamos los datos dispuestos en el chunk de R
import numpy as np
import pandas as pd
r.datos_py.head()
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 0 5.1 3.5 1.4 0.2 setosa
## 1 4.9 3.0 1.4 0.2 setosa
## 2 4.7 3.2 1.3 0.2 setosa
## 3 4.6 3.1 1.5 0.2 setosa
## 4 5.0 3.6 1.4 0.2 setosa
library(Matrix)
N <- 6
sparse_mat <- sparseMatrix(
i = sample(N, N, replace = F),
j = sample(N, N, replace = F),
x = runif(N),
dims = c(N, N)
)
sparse_mat
## 6 x 6 sparse Matrix of class "dgCMatrix"
##
## [1,] . . . . 0.6864545 .
## [2,] . . . 0.958531 . .
## [3,] . . 0.4775074 . . .
## [4,] 0.2312821 . . . . .
## [5,] . . . . . 0.231873
## [6,] . 0.7018502 . . . .
Disponemos la matrix de sparse_mat en chunk de R para Python
sparse_mat_py <- r_to_py(sparse_mat)
Recuperamos la matrix sparse_mat del chunk de R en chunk de Python
r.sparse_mat_py
## <6x6 sparse matrix of type '<class 'numpy.float64'>'
## with 6 stored elements in Compressed Sparse Column format>
Ahora recuperamos la matrix en chunk de R desde el chunk de Python
py_to_r(sparse_mat_py)
## 6 x 6 sparse Matrix of class "dgCMatrix"
##
## [1,] . . . . 0.6864545 .
## [2,] . . . 0.958531 . .
## [3,] . . 0.4775074 . . .
## [4,] 0.2312821 . . . . .
## [5,] . . . . . 0.231873
## [6,] . 0.7018502 . . . .