開始使用 R

R 是一個功能強大的科學計算機,其本身內建非常大量的數學運算功能,本篇將介紹它的基本使用方式。

R 是一個功能非常豐富的程式語言,而在實際開始學習 R 語言之前,我們先粗略的瀏覽一下 R 的一些基本功能與使用方式,讓大家對於 R 有一些基本的認識與了解,等大家熟悉 R 的操作之後,再繼續學習後續的細部觀念與更深入的課題。


以下是 R 的一些基本操作介紹。

R 使用者介面

R 是一個使用指令操作的軟體,在學習 R 語言之前,我們先熟悉一下他的操作介面。

將 R 打開之後,可以看到一個 R Console 視窗,裡面會有一些 R 的版本與相關使用條款的資訊,往後所有的指令都是在這個 R Console 視窗中做輸入,而所有的文字輸出也都是在這裡,他是 R 中最主要的操作介面。

install-r-in-windows-17

我們可以在 R Console 中直接輸入我們想要計算的數學式子,把 R 當作一般的計算機使用:

12 + 3 * 6

然後按下 Enter 鍵之後,就可以得到計算的結果:

[1] 30

這裡的 30 就是計算結果,而開頭用中括號包起來的數字是標示這個數值是在向量中的位置編號(這個部分稍後會介紹,目前可先忽略它)。

R 支援很多種格式的輸入,例如:

(43.43 * 2^4 + 1.23e3) / 384.23

其中 2^4 是 (2^4),而 1.23e3 則是 (1.23 times 10^3),這樣的輸出為:

[1] 5.009708

變數

我們可以建立變數,將數值或各種資料儲存起來:

foo <- 23;

其中 <- 為 R 特有的指定運算子(assignment operator),在大多數的情況下也可以使用等號 = 取代,就像這樣:

foo = 23;

這樣就會在 R 中建立一個變數 foo,而它的值是 23,這時候我們可以輸入變數的名稱來乾看這個變數的值:

foo
[1] 23

尋找變數

如果您在使用 R 時想要找尋之前建立的變數,但是一時忘記完整的變數名稱,這時候就可以使用 apropos 這個函數來搜尋,他可以使用關鍵字來搜尋所有的變數,列出所有符合的變數名稱。假設我們有一個 very_long_name 變數:

very_long_name <- 38

若我們只記得一部份的變數名稱,就可以使用 apropos 搜尋一下:

apropos("long")
[1] "longley"        "seq_along"      "very_long_name"

這樣就可以很快找出我們需要的變數。

呼叫函數

除了簡單的數值運算之外,R 中內建非常多的功能函數,善用這些功能函數可以幫助我們進行各式各樣複雜的運算,例如計算從 110 的總和,就可以使用 sum 這個功能函數:

sum(1:10)

輸出為

[1] 55

這裡的冒號 : 是一個可以產生連續數列的運算子,1:10 會產生從 110 的數列,接著我們將這個數列交給 sum 這個計算總和的函數,計算出從 110 的總和。

這裡 1:10 所產生的數列稱為一個向量(vector),而我們所呼叫的 sum 則稱為函數(function),函數在呼叫時需要傳入的資料就稱為函數的參數(argument)。

R 中有內建非常多的數學運算函數以及常數,例如三角函數與 (pi):

sin(pi/2)
[1] 1

絕對值:

abs(-34)
[1] 34

指數:

exp(1)
[1] 2.718282

中位數:

median(1:5)
[1] 3

R

30 留言

  1. New Yorkers can place bets online on sports events starting January 8,
    2022. Four major operators have been granted licenses to
    mobile betting. They will be able offer their services in a variety ways.

    my web-site: Dealers (Cairngorms-Leader.Org)

  2. YTCHOU

    你好,想知道中括號包起來的數字1有什麼特別的涵義嗎?

  3. There are many advantages when playing poker online. Online poker is simple to play with no waiting lists and no second-hand smoking.
    You can also play many different games.
    You can also play your preferred game at the convenience of your home.

  4. Gambling (southjerseymatchmakersreviews.Com) online has become increasingly popular in the last 10 years.
    In 1996, there were just fifteen websites. In 1997 there were over 200 websites.
    According to Frost & Sullivan, online gambling generated $830 million in profits in 1998.

  5. There are numerous advantages for playing online poker.

    It is easy to play, without waiting lists and no second-hand smoking.
    You can also play an array of games. Additionally,
    you can play your favorite game at the convenience of your home.

    Also visit my website: bet (Carmella)

  6. Poker players have plenty of new options thanks to the
    internet. Poker online has seen a dramatic increase
    in popularity over the last few years. Online poker is an excellent option if you enjoy
    the game but don’t have time to visit casinos.

    My site; best

  7. There are many benefits when you play at an online casino.
    You don’t have to invest a lot of money. Online
    casinos usually provide free games for players to try to find the best games.

    Also visit my web-site … Gambler (Andysdressform.Com)

  8. %%

    my page – uk payday loans (laybach.in)

  9. There are numerous reasons to participate in sports betting, from the possibility of making
    money to the fact that it brings excitement and a reason to look forward to the
    overall experience.

    Here is my web page :: best

  10. There are numerous advantages when you play at an online casino.
    You don’t have to spend a significant amount of money. Many online casinos provide free
    games, and players are able to test them out to determine what games they like
    the best.

    my web page: live [Elwood]

  11. Sports fans in New York are allowed to place bets online from Jan.
    8th 2022. Four major operators have obtained
    licenses for mobile betting which will allow them to provide their services in different ways.

  12. A player can choose the theme they prefer and pick
    from a variety of online slots, which include classics as well as more recent branded releases.

    The payouts can vary but they all follow the same mathematical formula.

    Also visit my website – gaming (Cleo)

  13. Poker is legal online. However you must verify that you’re
    of legal age. The majority of poker sites require
    players to be 18 years of age, or at least the legal drinking age in your
    jurisdiction.

    Here is my homepage – games (lostinamericafilm.com)

  14. There are many reasons to get into Sports – https://luniversam.com, betting, from the
    possibility of making profits to the fact that it
    adds excitement and something to root for to the overall viewing experience.

  15. There are many different rules and regulations that govern online gambling.
    In the US, for example, you can’t base an online gambling site in the country
    you reside in however you can locate it elsewhere.

    Here is my web-site … best

  16. The online gambling industry has become popular over the last decade.
    In 1996, there were only fifteen websites. By 1997, there were more than 200.

    In 1998 a Frost & Sullivan report stated that online gambling generated $830 million in revenue.

    Also visit my web page :: Gaming [https://Europaws.Org/]

  17. Online poker comes with many advantages. Poker online is simple
    to play with no waiting lists or second-hand smoking.
    Additionally, you can play a wide variety of games. You can also play your
    preferred game right from your home.

    Also visit my website – best (Lamar)

  18. Online slots are a favored game played by millions of players.
    This exciting and fun kind of gambling is a perfect way to pass
    the time or earn money. There are many different games to pick from and there are free games you can play.

  19. Slots online are a favorite pastime among millions of online casino players.

    This exciting and thrilling type of gambling is an excellent way to have
    fun or make additional money.

  20. Online casinos offer many benefits. It doesn’t require an enormous amount of money to join. Online casinos often offer free games for players to play to discover the most popular ones.

  21. %%

    Here is my webpage: online

  22. There are many different rules and rules that govern online gambling.
    In the US, for example you aren’t able to base an online gambling site in the country where you reside however, you
    can base it elsewhere.

    Also visit my web site Blackjack (webcom-Media.Com)

  23. bet

    Betting online offers bookmakers as well as bettors many advantages.
    Online betting sites permit people to bet on their favourite sports,
    as well as earn money. They offer a variety of ways to deposit money.

  24. There are a myriad of rules and regulations that govern online gambling.
    In the US for instance you cannot base gambling websites in the country in which you reside however, you can base it elsewhere.

    My blog … gaming (https://Izmiriplanliyorum.org)

  25. There are numerous reasons to participate in sports betting, from the
    chance to make a profit to the fact that it gives excitement and something to look forward to the overall experience.

    Stop by my site … best

  26. There are a variety of treatments for gambling addiction. One-on-one counseling is one of them, as are medication and lifestyle changes.
    This can turn into an addiction if you cannot stop yourself from engaging
    in it.

    Here is my web-site; dealers

Leave a Reply