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!"

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