0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mac: R のデータセットを使いたい

Last updated at Posted at 2024-07-10

以下は Mac だけでのことのようだ。Windows では問題なく使えている。

R が用意しているデータセット,たとえば iris などを Julia で使うとき, using RDatasets; iris = dataset("datasets", "iris") などとしていた。少なくとも 2 年ほど前までは,これで動いていたのだけど,久しぶりにやってみると Rdatasets や RData パッケージがちゃんと使えないという状況になっていた(プリコンパイルできないとか)。

その回避法を書いておく。

  1. R がインストールされていることを前提とする
  2. RCall パッケージが使えることを前提とする。必要ならば import Pkg; Pkg.add("RCall")

準備ができたら,以下を実行する。

using RCall
df = rcopy(R"iris")

これだけで iris データセットが df という名前のデータフレームとして使えるようになる。
その後も Julia でこのデータセットを使いたいような場合は,CSV.write しておけばよい。必要なときに CSV.read すればよい。

julia> using RCall

julia> df = rcopy(R"iris");

julia> first(df, 5)
5×5 DataFrame
 Row  Sepal_Length  Sepal_Width  Petal_Length  Petal_Width  Species 
      Float64       Float64      Float64       Float64      Cat…    
─────┼───────────────────────────────────────────────────────────────
   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

julia> using Plots

julia> histogram(df.Sepal_Length)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?