Thursday, 3 March 2016

R 學習筆記(基本指令操作#1)

Learning Target

1. 輸出結果
2. 設定變數
3. 列出目前現有變數
4. 刪除變數
5. 建立向量 (vector)

Note

1. 在R語言中,可以使用 print() 和 cat() ,來輸出結果。但這兩種方法有些許差異,分別應用在不同的情況。
*print() 只能夠輸出單一個 object 的內容,用於輸出 matrix, list 這兩種資料結構的型態非常的方便,不用再額外定義格式
>print(matrix(c(1,2,3,4), 2 ,2))      [,1] [,2] [1,]    1    3 [2,]    2    4
>print("The result of ", x, "plus", y, "is", x+y)  Error in print.default("The result of ", x, "plus", y, "is", x + y) :    invalid 'quote' argument
*例如要使用上述的方法來印出資料的話,則需使用 cat() 。但是 cat() 無法印出 matrix, list 這兩種資料結構。
>cat("The result of ", x, "plus", y, "is", x+y) The result of  3 plus 4 is 7
>cat(list("Hello~", "there")) Error in cat(list(...), file, sep, fill, labels, append) :    argument 1 (type 'list') cannot be handled by 'cat'
2. R 語言要進行變數的設定非常的簡單只需要使用小於的符號加上一個減號(<-)
>x <- 3 >y <- 4 >cat("The result of ", x, "plus", y, "is", x+y) The result of  3 plus 4 is 7
3. 使用 ls() 便可以看到目前所有設定的變數
>ls() [1] "x"   "y"
4. 使用 rm() 則可以刪除特定的變數
>rm(x) >x Error: object 'x' not found
5. c()可以用來建立向量(vector)
>v1 <- c(1, 2, 3)
>v2 <- c("Hello~", "there!")
>c(v1, v2)
[1] "1"      "2"      "3"      "Hello~" "there!"

Related Posts:

  • R學習筆記(系統環境相關操作#1) Learning Target 1. 設定工作路徑(Working directory) 2. 儲存現階段工作狀態(Saving workspace) 3. 列出執行過的歷史指令(Command history) 4. 儲存上一次指令執行結果 5. 顯示目前載入的套件(package) … Read More
  • R學習筆記(宣告及定義方法 function) Learning Target 1. 如何宣告方法 function Note 請參考以下我的 R Script #利用function這個方法來宣告方法 test <- function(a, b){   if(b==0) return("b can't be zero"… Read More
  • R 學習筆記(常見的錯誤) Learning Target 1. 了解常見的小錯誤,不容易被發現,但會造成結果的錯誤 Note *遺漏掉括號。例如: ls() 或其他可以不用帶入變數的方法遺漏掉括號的話 R 會將該 function的原始碼顯示出來。 *不同作業系統路徑表示斜線符號不同。Windows 所使用的為… Read More
  • R 學習筆記(基本指令操作#2) Learning Target 1. 基本統計計算 2. 建立序列(Sequences) 3. 向量(Vector)比較 4. 選取向量(Vector)元素(Elements) 5. 向量(Vector)四則運算(Arithmetic) Note 1. 這裡將列出統計常用的幾個計算f… Read More
  • R學習筆記(系統環境相關操作#2) Learning Target 1. 顯示目前已安裝的套件(package) 2. 利用 CRAN 安裝套件(package) 3. 設定 CRAN Mirror 4. 關閉 R 啟動訊息 5. 執行 script 6. 顯示以及設定環境變數(Environment Variable… Read More

1 comment:

  1. R 學習筆記(基本指令操作1) ~ K.L. 隨手札記 >>>>> Download Now

    >>>>> Download Full

    R 學習筆記(基本指令操作1) ~ K.L. 隨手札記 >>>>> Download LINK

    >>>>> Download Now

    R 學習筆記(基本指令操作1) ~ K.L. 隨手札記 >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete