1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ipython notebookでRの関数を手軽に使いたい

Posted at

ipython で Rの関数を使いたいとき%%Rを使うか、

python
import pyper as pr
r = pr.R(use_pandas =True)

これだと%%Rよりも変数スコープを気にせず

python
def sample(lambda_target, sample_size):
    return r('rpois(%d, %f)'%(sample_size, lambda_target))

こういったpythonのメソッドを作れて便利

しかしこれだと戻り値が

'try({rpois(50, 8.000000)})\n [1] 8 6 11 8 10 9 8 10 5 11 8 4 13 11 4 10 5 14 5 9 11 10 7 9 1\n[26] 8 6 10 4 8 14 13 3 8 13 2 9 6 6 7 11 7 5 6 5 8 7 10 5 8\n'

というRコンソール的な出力の文字列になってしまう。

python
re.sub(r' +',  ',' ,(re.sub(r'\[.+?\]', '', return_str.split(')')[-1].replace('\n','')).strip())).split(",")
#結果はstringなのでキャスト必須

なのでこれを毎回かませている

戻り値をもっと使えるような形にするやり方はあるのだろうか。。

1
1
2

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?