楊尚峰
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 'R 語言學習心得-基礎篇3' disqus: hackmd --- R 語言學習心得 基礎篇3 === ![downloads](https://img.shields.io/badge/download-R-brightgreen) ![grade](https://img.shields.io/badge/Grade-新手-brightgreen) ![chat](https://img.shields.io/discord/:serverId.svg) --- R的運算符號1 --- **除了基本的四則運算以外:** 1. Modulo : 10%%2 2. Power : 10**2 or 10^2 3. Integer division : 10 %/% 2 ![](https://i.imgur.com/vnwyaar.png) ---- R的運算符號2 --- ![](https://i.imgur.com/a4L3RRy.png) 1. A %in% B : A是否有在B向量裡面 2. 87有包含在85~90之間 3. 看Y裡面的元素是否包含在(0,2,4)向量中(依序) ---- R的運算符號3 --- ![](https://i.imgur.com/dxdzInk.png) **排序 :** 1. sort() : 小到大 2. order() : 一序列出順序編號 3. rev() : 倒過來排列 --- 數學函數 --- **常見的數學函數如下圖** ![](https://i.imgur.com/3STCXlX.png) ---- 統計函數 --- **常見的統計函數都有 : normal , chi-square ,t ,f …. 每一種統計函數又可以分4大類 : d/p/q/r** ```r d= density(密度) r= random generation(隨機) q = quantile(分位數) p=累積分布函數(累積機率) ``` ![](https://i.imgur.com/nYXidht.png) ---- **其他常用統計函數** ![](https://i.imgur.com/OnF9cGj.png) ---- 統計函數範例 --- 1. rnorm() : 產生符合**常態分佈的***隨機亂數* 2. r = random generation(隨機) 3. norm() = 常態分佈 ```r rnorm(n, mean = 0, sd = 1) #n=產生個數 sd=標準差 ``` ![](https://i.imgur.com/MznqO4v.png) ---- 統計函數範例2 --- 1. dnorm() : 產生符合常態分佈的機率密度 2. d= density(密度) 3. norm() = 常態分佈 ```r rn <- rnorm(n=300) rp <- dnorm(rn) a <- data.frame(num=rn,probability=rp);plot(a) ``` ![](https://i.imgur.com/GcQzsxu.png) ---- 統計函數範例3 --- 1. pnorm() : 小於等於某數所累積的機率 2. pnorm()是左尾累積 ![](https://i.imgur.com/VBOEVVH.png) 3. sample() : 產生隨機亂數 ```r sample(x= 1:10 , size = 10,replace = TRUE) # replace = TRUE 可產生重複數字 ; FALSE 則不能 #第一個x= 設定範圍 ``` ---- 統計函數範例4 --- **var()= 變異數 / sd() = 標準差 /min() /max()** ![](https://i.imgur.com/6iqI06N.png) 1. sd(x,na.rm=TRUE) 可剔除資料中的NA值 ---- 運算小技巧1 --- 1. set.seed(num) 2. 讓調適結果維持在定值 3. 適合用在需要產生固定的隨機數列(偽隨機) 4. num 不一定要整數 ![](https://i.imgur.com/pbtBK9g.png) ---- 運算小技巧2 --- 1. 中位數 : median(x) 2. 百分位數 : quantile(x) 3. 眾數沒有內建函數 : 可以自己定義 或使用組合(見下頁) ![](https://i.imgur.com/Xt3U1M3.png) ---- 運算小技巧3 --- ![](https://i.imgur.com/rnTSSq2.png) ```r table(a) | end(x)...x=向量 a | 回傳最後一個數與第一個數的指標 1 2 3 4 #出現的數字 end(c) 3 1 1 1 #出現次數 [1] 4 1 ``` ---- **快速函數組合的眾數** ```r a <- 向量 b <- names(sort(table(a))) a的眾數 <- b[end(b)[1]] ``` **等等會用自訂函數的方式建立眾數** :hamburger: ---- 線性回歸參數實例 --- **還記得 stat_smooth(method=”回歸方法”) 嗎?** **注意 : “data = “ 一定要寫不然會有error** ![](https://i.imgur.com/GMj1Dwk.png) --- 自訂函數1 --- 1. 與c語言的方式很像 2. 直接看例子 可以有return ![](https://i.imgur.com/5f9zK8H.png) ![](https://i.imgur.com/HEBuNA5.png) 3. 若系統偵測你尚未完成coding,換行時會有"+"號 ---- 自訂函數2 --- 1. R也有if-else 2. if-else判斷式有快速函數實現方式 ```r ifelse(判斷式 , True回傳值, False回傳值) ``` ![](https://i.imgur.com/La9Qbsl.png) ---- 自訂一個眾數function --- ![](https://i.imgur.com/qtyoIjA.png) **有興趣的可以去查詢每個函數的使用方法** **善用"?函數"去查詢吧** :+1: ---- 檔案讀取1 --- 1. 相對路徑 vs 絕對路徑 2. 相對路徑 : 與目標檔案在同一個目錄底下即可 3. 絕對路徑 : 每個上層目錄都要打出來 ```r relative path : 桌面有檔案A與檔案B=>在同一目錄(Desktop)底下 absolute path : C:\Users\user\Desktop\檔案A C:\Users\user\Desktop\檔案B ``` ---- 檔案讀取2 --- 1. 直接打上絕對路徑來讀取檔案 2. 使程式作業資料夾與檔案所在一致(相對路徑) 3. 將檔案複製(移動)到程式作業資料夾 :::success 教學將只介紹前2種 第3種可以請媽媽教你 ::: ---- 檔案讀取3 --- 1. read.table(file=”filename”, header=TRUE,sep=”,”) ```r Filename 要加副檔名 header=TRUE代表第一筆資料是欄位名稱 sep=”,” 以逗號分隔 ``` 2. read.csv(file=”filename”) 3. read.csv 可以少打一些字 :santa: ![](https://i.imgur.com/MuqUzaF.png) ---- :tea: Something Tricky :::danger 路徑中是 ” / ” 而非 “ \ ” window 複製路時是“ \ ” ,記得要改 ::: ---- 檔案讀取4(相對路徑) --- 1. 在讀取檔案前先將working資料夾 移到跟目標檔案一樣的目錄 2. 直接讀取目標檔案 ```r setwd() : set working directory getwd() : get working directory ``` ![](https://i.imgur.com/1joIEVZ.png) ---- head() & tail() --- **回傳變數、表格、矩陣、檔案或函數的部分資料** **head()傳回前面資料** **tail()則是傳回後面** ```r head(file,n=xL) …x line to show tail(file,n=xL) …x line to show ``` **R 內建一些data可以使用 : iris/tress ...** **可輸入 data() 來查詢內建資料集** ---- iris(安德森鳶尾花卉數據集) --- ![](https://i.imgur.com/4XFNrXI.png) --- ## Next Lesson ... 1. Decision trees 2. Drawing Special Graphics 3. more application in R ## More tutorial / note 1. [my coding-blog](fatcatcat-lab.blogspot.com) 2. [my movie-blog](fatcatcat-movie.blogspot.com) ###### tags: `R` `beginner` `cat` `tutorial`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully