{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/laurent/Desktop/software/python/py36_env/lib/python3.6/site-packages/rpy2-3.2.7-py3.6-linux-x86_64.egg/rpy2/robjects/pandas2ri.py:14: FutureWarning: pandas.core.index is deprecated and will be removed in a future version. The public classes are available in the top-level namespace.\n",
" from pandas.core.index import Index as PandasIndex\n",
"/home/laurent/Desktop/software/python/py36_env/lib/python3.6/site-packages/rpy2-3.2.7-py3.6-linux-x86_64.egg/rpy2/robjects/pandas2ri.py:34: UserWarning: pandas >= 1.0 is not supported.\n",
" warnings.warn('pandas >= 1.0 is not supported.')\n"
]
}
],
"source": [
"from functools import partial\n",
"from rpy2.ipython import html\n",
"html.html_rdataframe=partial(html.html_rdataframe, table_class=\"docutils\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# tidyr in Python"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/laurent/Desktop/software/python/py36_env/lib/python3.6/site-packages/rpy2-3.2.7-py3.6-linux-x86_64.egg/rpy2/robjects/lib/tidyr.py:13: UserWarning: This was designed againt tidyr version 0.8.3 but you have 1.0.2\n",
" 'but you have %s' % (TARGET_VERSION, tidyr.__version__))\n"
]
}
],
"source": [
"from rpy2.robjects.lib.tidyr import DataFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(note: `dplyr` is implicitly used by `tidyr`.)\n",
"\n",
"In addition to that, and because this tutorial is in a notebook,\n",
"we initialize HTML rendering for R objects (pretty display of\n",
"R data frames)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import rpy2.ipython.html\n",
"rpy2.ipython.html.init_printing()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"DataFrame with 3 rows and\n",
" 3 columns:\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" x | \n",
" y | \n",
" z | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" a | \n",
" 3 | \n",
" 6 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" b | \n",
" 4 | \n",
" 7 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" b | \n",
" 5 | \n",
" 8 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"R object with classes: ('data.frame',) mapped to:\n",
"[StrSexpVector, IntSexpVector, IntSexpVector]\n",
" x: \n",
" [RTYPES.STRSXP]\n",
" y: \n",
" [RTYPES.INTSXP]\n",
" z: \n",
" [RTYPES.INTSXP]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import OrderedDict\n",
"from rpy2.robjects.vectors import (StrVector,\n",
" IntVector)\n",
"dataf = DataFrame(OrderedDict(x=StrVector((\"a\", \"b\", \"b\")),\n",
" y=IntVector((3, 4, 5)),\n",
"\t\t z=IntVector((6, 7, 8))))\n",
"dataf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"DataFrame with 3 rows and\n",
" 3 columns:\n",
"\n",
" \n",
" \n",
" | \n",
" | \n",
" z | \n",
" a | \n",
" b | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 6 | \n",
" 3 | \n",
" NA_integer_ | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 7 | \n",
" NA_integer_ | \n",
" 4 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 8 | \n",
" NA_integer_ | \n",
" 5 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
"R object with classes: ('data.frame',) mapped to:\n",
"[IntSexpVector, IntSexpVector, IntSexpVector]\n",
" z: \n",
" [RTYPES.INTSXP]\n",
" a: \n",
" [RTYPES.INTSXP]\n",
" b: \n",
" [RTYPES.INTSXP]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataf.spread('x', 'y')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Reuse. Get things done. Don't reimplement.**"
]
}
],
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}