在 Julia 中使用 R 函式
你已經看過如何在 Julia 中使用 Python 的函式,R 的用法也很類似。你會使用先前已匯入的 RCall 套件,接著用 rimport 巨集來匯入套件。先前你匯入的是 base,也就是 R 內建的基礎函式集合。
在這個練習中,你需要用 Julia 常見的方括號語法定義兩個向量 x 與 y。接著反轉向量 y 的順序,最後在圖上繪製 x 對 y。
本練習屬於課程
Julia 中級
練習說明
- 在 Julia 中建立兩個向量:一個包含 1 到 4,另一個包含 5 到 8。
- 使用 R 的
rev函式,將y作為引數傳入,反轉向量y的順序。 - 使用 R 的
plot函式,繪製x對y。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
using RCall
@rimport base as r_base
# Define two vectors, x and y, with values one to four and five to eight
x = [____, ____, ____, ____]
y = [____, ____, ____, ____]
# Reverse the order of the values in y using R
y = ____.____(____)
# Plot x and y using R
r_base.____(____, ____)