R 統計軟體 -- 語法與函數

統計軟體 R

簡介

安裝

操作方式

變數與運算

有序數列

向量

矩陣

多維陣列

複數

因子

串列

資料框

時間數列

流程控制

輸出入

呼叫

函數

2D 繪圖

3D 繪圖

互動介面

套件列表

其他語言呼叫

R 的應用

集合

邏輯推論

模糊邏輯

機率邏輯

檢定

搜尋

優化算法

線性代數

決策樹

人工智慧

分群分類

SVM 向量機

神經網路

遺傳演算法

資料採礦

訊號處理

影像處理

語音處理

自然語言

機器學習

機器人

生物統計

數位訊號處理

方程式求解

數值分析

微積分

微分方程

線性規劃

圖形理論

統計推論

字串處理

正規表示式

視窗程式

網頁程式

文件格式

貝氏網路

訊息

機率統計書

相關網站

參考文獻

最新修改

簡體版

English

輔助說明

輸出入

load() load the datasets written with save
data(x) loads specified data sets
library(x) load add-on packages
read.table(file) 讀表格
read.csv("filename",header=TRUE)

資料建立

指令 說明 範例
c(…) 連接成向量
from:to 產生序列
seq(from,to)
rep(x,times) 重複 rep(c(1,2,3),2) is 1 2 3 1 2 3; rep(c(1,2,3),each=2) is 1 1 2 2 3 3
data.frame(…) 建立框架資料 data.frame(v=1:4,ch=c("a","B","c","d"),n=10);
list(…) 建立串列 list(a=c(1,2),b="hi",c=3i);
array(x,dim=) 建立陣列 array(x, dim=c(3,4,2))
factor(x,levels=) 將 x 轉為因子 (factor) 型態
gl(n,k,length=n*k,labels=1:n) 產生因子的樣本
expand.grid() 所有可能性列表
rbind(…)
cbind(…)

取出向量 (Vector) 的部分資料

指令 說明 範例
x[n] x 中的第 n 個元素
x[-n] 去除第 n 個
x[1:n] 前 n 個
x[-(1:n)] 去除前 n 個
x[c(1,4,2)] 第 1, 4, 2 個
x["name"] 名稱為 "name" 的元素
x[x > 3] 所有大於 3 的元素
x[x > 3 & x < 5] 所有介於 3 到 5 之間的元素
x[x %in% c("a","and","the")] 在指定集合中的元素

取出串列 (List) 的部分資料

指令 說明 範例
x[n] list with elements n
x[[n]] n th element of the list
x[["name"]] element of the list named "name"
x$name id.

取出矩陣 (Matrix) 的部分資料

指令 說明 範例
x[i,j] element at row i, column j
x[i,] row i
x[,j] column j
x[,c(1,3)] columns 1 and 3
x["name",] row named "name"
x$name x 的名稱 (ID)

取出框架 (Frame) 的部分資料

指令 說明 範例
x[["name"]] column named "name"
x$name x 的名稱 (ID)

變數轉換與資訊

型態轉換:as.array(x), as.data.frame(x), as.numeric(x), as.logical(x), as.complex(x), as.character(x),

測試型態:is.na(x), is.null(x), is.array(x), is.data.frame(x),is.numeric(x), is.complex(x), is.character(x),

變數的相關資訊

指令 說明 範例
length(x) number of elements in x
dim(x) Retrieve or set the dimension of an object; dim(x) <- c(3,2)
dimnames(x) Retrieve or set the dimension names of an object
nrow(x) number of rows; NROW(x) is the same but treats a vector as a onerow matrix
ncol(x) and NCOL(x) id. for columns
class(x) get or set the class of x; class(x) <- "myclass"
unclass(x) remove the class attribute of x
attr(x,which) get or set the attribute which of x
attributes(obj) get or set the list of attributes of obj

資料選取與管理

指令 說明 範例
which.max(x) 取得最大元素的索引
which.min(x) 取得最大元素的索引
rev(x) 反轉向量
sort(x) 向量排序
cut(x,breaks) 分割成數段 (intervals , factors)
match(x, y)
which(x == a)
choose(n, k) computes the combinations of k events among n repetitions= n!/[(n − k)!k!]
na.omit(x) suppresses the observations with missing data (NA) (suppresses the corresponding line if x is a matrix or a data frame)
na.fail(x) returns an error message if x contains at least one NA
unique(x) if x is a vector or a data frame, returns a similar object but with the duplicate elements suppressed
table(x) returns a table with the numbers of the differents values of x (typically for integers or factors)
subset(x, …)
sample(x, size)
prop.table(x,margin=) table entries as fraction of marginal table

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License