LoginSignup
4
4

More than 5 years have passed since last update.

python で R のデータセットを使いたい

Last updated at Posted at 2016-12-02

The pandas.rpy module is deprecated and will be removed in a future version. We refer to external packages like rpy2.
らしいので、rpy2 を使うといいっぽい。

pip install rpy2 で入る。

ipython
In [1]: from rpy2.robjects import pandas2ri

# これをやると rpy2.robjects.vectors.DataFrame から pandas.core.frame.DataFrame に変換してくれる
In [2]: pandas2ri.activate()

In [3]: from rpy2.robjects import r

In [10]: r["iris"].head(10)
Out[10]:
    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
7            4.6          3.4           1.4          0.3  setosa
8            5.0          3.4           1.5          0.2  setosa
9            4.4          2.9           1.4          0.2  setosa
10           4.9          3.1           1.5          0.1  setosa

In [11]: type(r["iris"])
Out[11]: pandas.core.frame.DataFrame

In [12]: type(r["Titanic"])
Out[12]: numpy.ndarray

参考

追記@2016/12/5

rpy2

Installing should be as easy* as
pip install rpy2
(*: except on Windows)

とかサラッと無慈悲なので PypeR 使う方法もあるみたいだけど、2012で止まってる気がするけど大丈夫なんだろうか。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4