【正文】
[2,] 2 4 6 [3,] 3 6 9 ? x %*% rbind(x) [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 6 [3,] 3 6 9 ? y=diag(x) y [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 2 0 [3,] 0 0 3 ? diag(y) [1] 1 2 3 ? diag(3) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 矩陣合并與拉直 ? 函數(shù) cbind()把其自變量橫向拼成一個(gè)大矩陣,rbind()把其自變量縱向拼成一個(gè)大矩陣。 ? diag(matrix)返回由矩陣的主對角元素組成的向量。詳見隨機(jī)幫助,例如: ? ?qr ? 函數(shù) diag()的作用依賴于其自變量。只寫一個(gè)參數(shù) X的 crossprod(X)計(jì)算 X自身的內(nèi)積 。 ? 函數(shù) crossprod(X, Y)表示一般的交叉乘積(內(nèi)積) ,即 X的 每一列 與 Y的 每一列 的內(nèi)積組成的矩陣。因?yàn)榍罢咻^常用,所以 S選擇表示前者,但內(nèi)積最好還是用 crossprod(x)來計(jì)算。 ? 例如,設(shè) x是一個(gè)長度為 n的向量, A是一個(gè) n*n 矩陣,則“ x %*% A %*% x”表示二次型 。 ? 要進(jìn)行矩陣乘法,使用運(yùn)算符 %*%, A%*%B表示矩陣 A乘以矩陣 B(當(dāng)然要求 A的列數(shù)等于 B的行數(shù))。 nrow(A)為矩陣 A的行數(shù), ncol(A)為矩陣 A的列數(shù)。例如: 不規(guī)則運(yùn)算 ? 除非你清楚地知道規(guī)則,否則應(yīng)避免使用這樣的辦法(標(biāo)量與數(shù)組或向量的四則運(yùn)算除外)。例如,假設(shè) A, B, C是三個(gè)形狀相同的數(shù)組,則 ? D C + 2*A/B ? 四則運(yùn)算遵循通常的優(yōu)先級規(guī)則。 ? 還有一種特殊下標(biāo)是對于數(shù)組只用一個(gè)下標(biāo)向量(是向量,不是數(shù)組),比如 a[3:4] ,這時(shí)忽略數(shù)組的維數(shù)信息,把下標(biāo)表達(dá)式看作是對數(shù)組的數(shù)據(jù)向量取子集。 ? a[ , , ]或 a[]都表示整個(gè)數(shù)組。 ? 在每一個(gè)下標(biāo)位置寫一個(gè)下標(biāo)向量,表示對這一維取出所有指定下標(biāo)的元素,如 a[1, 2:3, 2:3]取出所有第一下標(biāo)為 1,第二下標(biāo)為 2或 3,第三下標(biāo)為 2或 3的元素。例如: ? b matrix(0, nrow=3, ncol=4) 生成 3行 4列的元素都為 0的矩陣。例如,定義一個(gè) 3行 4列,由 1:12按行次序排列的矩陣,可以用: ? b matrix(1:12, ncol=4, byrow=T) ? b matrix(1:12, ncol=4, byrow=T) b [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 注意在有數(shù)據(jù)的情況下只需指定行數(shù)或列數(shù)之一。 ? 格式為 matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) 其中第一自變量 data為數(shù)組的數(shù)據(jù)向量(缺省值為缺失值 NA), nrow為行數(shù), ncol為列數(shù),byrow表示數(shù)據(jù)填入矩陣時(shí)按行次序還是列次序,缺省情況下按列次序。 dimnames屬性可以省略,不省略時(shí)是一個(gè)長度與維數(shù)相同的列表( list,見后面),列表的每個(gè)成員為一維的名字。array()函數(shù)的完全使用為 array(x, dim=length(x), dimnames=NULL),其中 x是第一自變量,應(yīng)該是一個(gè)向量,表示數(shù)組的元素值組成的向量。例如,假設(shè)數(shù)組 a的元素為 1:24,維數(shù)向量為 c(2,3,4),則各元素次序?yàn)?a[1,1,1], a[2,1,1], a[1,2,1], a[2,2,1], a[1,3,1], ..., a[2,3,4]。 ? 一組值只有定義了維數(shù)向量( dim屬性)后才能被看作是數(shù)組。 ? 數(shù)組有一個(gè)特征屬性叫做維數(shù)向量( dim屬性),比如維數(shù)向量有兩個(gè)元素時(shí)數(shù)組為二維數(shù)組(矩陣)。 ? y。 ? y[x]=x[x]+20。(x)]=x[(x=3)amp。 ? y[x3]=x[x3]。 求 y. ? x=c(,1,1,2,3,4,1,2,3,4)。 改變部分元素值的技術(shù)與邏輯值下標(biāo)方法結(jié)合可以定義向量的分段函數(shù), 例如:要定義 y=f(x)為當(dāng) x0時(shí)取 1x,否則取 1+x, 可以用: y = numeric(length(x)) y[x0] =1 x[x0] y[x=0] = 1 + x[x0] ? 要定義 y=f(x)為當(dāng) x x, =x4時(shí)取x+10,否則取 x+20。 如果下標(biāo)都是假值則結(jié)果是一個(gè)零長度的向量,顯示為 numeric(0)。例如: x[(1:2)] [1] ? 三、取邏輯值的下標(biāo)向量 v為和 x等長的邏輯向量, x[v]表示取出所有 v為真值的元素,如: x 輸出: [1] x10 輸出: [1] TRUE FALSE TRUE x[x10] 輸出: [1] x[x0] 輸出: numeric(0) 可見 x[x10]取出所有小于 10的元素組成的子集。 is generic: you can write methods to handle specific classes of objects, see InternalMethods. References: Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_. Wadsworth amp。 group generic. In addition, the elementary trigonometric, logarithmic and exponential functions are available for plex values. 39。(z) = sqrt{x^2 + y^2}, and for phi= Arg(z), x = cos(phi) and y = sin(phi). They are all generic functions: methods can be defined for them individually or via the 39。 have their usual interpretation as returning the real part, imaginary part, modulus, argument and plex conjugate for plex values. Modulus and argument are also called the _polar coordinates_. If z = x + i y with real x and y, 39。 and 39。, 39。, 39。, 39。. The functions 39。 are never both 39。 and 39。 it strips attributes including names. Note that 39。 attempts to coerce its argument to be of plex type: like 39。. The vector can be specified either by giving its length, its real and imaginary parts, or modulus and argument. (Giving just the length generates a vector of plex zeroes.) 39。. ...: further arguments passed to or from other methods. Details: Complex vectors can be created with 39。 plex package:base R Documentation Complex Vectors Descr