Transferred from: http://blog.csdn.net/u011253874/article/details/43115447
- <span style= "FONT-SIZE:14PX;" > #R语言备忘录三 #
- #数组array和矩阵matrix, list, data frame Dataframe
- #数组
- #数组的重要属性就是dim, Number of dimensions
- Matrix of #得到4
- Z <-1:12
- Dim (z) <-C (3,4)
- Z
- #构建数组
- X <-Array (1:20, Dim = C (4,5))
- #三维
- Y <-Array (1:18, Dim = C (2,3,3))
- #数组下标
- Y[1, 2, 3]
- #数组的广义转置, dimensions change, turn 2 dimensions into 1 dimensions, turn 3 dimensions into 2 dimensions, 1 dimensions into 3 dimensions, i.e. d[i,j,k] = C[j,k,i]
- C <-Array (1:24, Dim = C (2,3,4))
- D <-Aperm (A, C (2,3,1))
- #apply用于数组固定某一维度不变, perform calculations
- Apply (A, 1, sum)
- #矩阵
- #产生矩阵
- A <-Matrix (1:15, nrow=3, ncol=5, Byrow=true)
- B <-Matrix (3:17, nrow=5, ncol=3, Byrow=true)
- #求方阵行列式的值
- Det (Matrix (1:4, Ncol = 2))
- #内积
- #矩阵的内积
- A%*% B
- #也可以使用crossprod函数
- Crossprod (A, B)
- Crossprod (A)
- #向量的外积, also known as cross-product
- X <-1:5
- Y <-2*1:5
- X%o% y
- #也可以使用tcrossprod函数, outer
- Outer (x, y)
- Tcrossprod (x)
- #产生对角阵
- #如果变量是一个向量, a diagonal array with a vector as a diagonal element
- V <-C (1, 3, 5)
- Diag (v)
- #如果变量是一个矩阵, we take the diagonal elements of the matrix as diagonal elements
- Diag (A)
- #解线性方程Ax =b,
- b <-Matrix (C (1,1,1), nrow = 3, Byrow = TRUE)
- B <-Matrix (1:9, nrow = 3, Byrow = TRUE)
- Solve (B,B)
- #求矩阵的逆
- Solve (B)
- #ev $values is a characteristic root, and ev$vectors is a matrix of eigenvectors.
- SM <-Crossprod (A, a)
- EV <-Eigen (SM)
- #奇异解, SVD$D returns the singular value of matrix A, Svd$u is the orthogonal array u,svd$v corresponding to the orthogonal array V, a = UDVT
- Svda <-SVD (A)
- #把矩阵拉成向量
- As.vector (A)
- #数组或矩阵的维的名字
- X <-Matrix (1:6, Ncol = 2,
- Dimnames = List (c ("One", "B", "three"), C ("First", "Second")),
- Byrow = T)
- #亦或是
- Dimnames (A) <-list (C ("One", "B", "three"), C ("First", "Second"))
- Colnames (A) <-C ("First", "Second")
- Rownames (A) <-C ("One", "B", "three")
- #列表
- #构建列表
- Lst <-list (name= "Fred", wife= "Mary", No.children=3, Child.ages=c (4,7,9))
- #列表元素, list name [[Subscript]]
- LST[[2]]
- LST[[4]][2]
- #也可以使用名字代替下标
- lst[["Name"]
- Lst$name
- #修改列表
- Lst$name <-C ("John", "Tom")
- #删除列表某项
- Lst$name <-NULL
- #连接几个列表
- List. ABC <-C (list. A, List. B, List. C
- #数据框
- #data. Frame, usage and list are the same, if the list of ingredients satisfies the data frame, you can also use the As.data.frame conversion
- Df<-data.frame (
- Name=c ("Alice", "Becka", "James", "Jeffrey", "John"), Sex=c ("F", "F", "M", "M", "M"),
- Age=c (13, 13, 12, 13, 12),
- Height=c (56.5, 65.3, 57.3, 62.5, 59.0)
- Weight=c (84.0, 98.0, 83.0, 84.0, 99.5)
- )
- Df
- #矩阵可以通过data. FRAME (X) conversion
- #取数据框的一部分
- Df[1:2, 3:5]
- df[["Height"]
- Df$weight
- #命名
- Names (DF)
- Rownames (DF)
- The #attch () function calls the variables in the data frame into memory to facilitate the invocation of data
- Attach (DF)
- #取消连接
- Detach (DF)
- #调用edit进行编辑数据
- Edit (DF)
- #因子factor
- Sex <-C ("M", "M", "F", "F")
- SEXF <-factor (Sex)
- #因子水平
- Sex_level <-levels (SEXF)
- #用table统计各类数据的频数
- Sex_tab <-table (SEXF)
- #用gl () Production factor
- #gl (n, k, length = n * k, labels = 1:n, ordered = FALSE)
- </span>
、、、、、、、、、、、、、、、、、、、、、、、、、、、
- #R语言学习备忘录一 #
- #向量运算 #
- x <-C (1,2,3,6)
- #不小于x的最小整数
- Ceiling (x)
- #不大于x的最大整数
- Floor (x)
- #向0方向截取的x中的整数
- Trunc (x)
- #将x舍入为指定位的小数
- Round (x,digits=2)
- #将x舍入指定的有效数字位数
- Signif (x,digits=2)
- #三角函数
- COS (x)
- Sin (x)
- Tan (x)
- ACOs (x)
- ASIN (x)
- #分位数, such as 25% and 50% of the number of digits
- Quantile (X,c (. 25,.5))
- #求值域
- Range (x)
- #求乘积函数
- Prod (x)
- #滞后差分
- diff (X)
- #求最大值和最小值对应的位置
- Which.min (x)
- Which.max (x)
- #数据标准化
- X<-c (123,232,212,232,120,273)
- Mydata<-scale (x)
- MyData
- #正则表达式匹配
- Str<-c ("A", "a", "B", "C")
- grep ("A", Str,fixed=true)
- #字符
- #分割符strsplit
- Strsplit ("ABCDE", "" ")
- #连接字符paste
- Paste ("x", 1:10,sep= "")
- Paste ("x", 1:5,sep= "T")
- Paste ("Today is", date ())
- #大写转换toupper
- ToUpper ("Abcef")
- #小写转换tolower
- ToLower ("ABC")
- #计算字符数量 nchar
- X<-c ("AB", "Deew", "James")
- NCHAR (x)
- NCHAR (x[2])
- #提取或替换一个数值, and the Excel mid function is almost substr
- x<-"ABCDEFG"
- substr (x,2,5) #2到5个元素
- substr (x,2,5) <-"1111111"
- X
- #注意两种等差数列的差别
- 1:n-1
- 1: (n-1)
- #重复函数rep
- Y<-rep (1:5,2)
- Y
- #等间隔函数
- Seq ( -5, 5, by=.2)
- Seq (length=51, from=-5, by=.2)
- #上下两种方式效果一样
- #求行均值
- Apply (Mydata,1,mean)
- #求列均值
- Apply (Mydata,2,mean)
- #逻辑变量
- #判断一个逻辑向量是否都为真的函数是all
- All (c (1,2,3,4,5,6) >3)
- #判断一个逻辑向量是否有为真的函数any
- Any (c (1,2,3,4,5,6) >3)
- #缺失数据
- #NA表示数据缺省或缺失
- Z <-C (1:3, NA)
- Z
- A <-is.na (z)
- #修改缺失数据
- Z[is.na (z)] <-0
- #is. Nan () to determine whether the data is accurate, the INF is also a non-accurate
- x <-C (0/1, 0/0, 1/0, NA)
- Is.nan (x)
- Is.finite (x)
- Is.infinite (x)
- Is.na (x)
- #复数向量
- #复数z =x+isin (x)
- x <-seq (-pi, pi, by = PI/10)
- Y <-sin (x)
- Z <-Complex (re = x, Im = y)
- Plot (z)
- Lins (z)
- #向量的下标运算
- x <-C (1,4,7)
- X[c (2,3)]
- #修改元素值
- X[c (1,3)] <-C (22, 33)
- #逻辑向量
- x <-C (1,4,7)
- X < 5
- X[X<5]
- #分段函数
- Y <-Numeric (length (x))
- Y[X<0] <-1-x[x<0]
- Y[x>=0] <-1-x[x>=0]
- #即y =1-x,x<0; Y=1+x,x>=0
- #下标的负整数运算表示的是删除
- V <-10:20
- v[-(1:5)]
- #取字符串为下标
- Ages <-C (li=33, zhang=29, liu=18)
- ages["Zhang"]
- #给向量赋予名字
- Fruit <-x (5, 10, 1, 29)
- Names (fruit) <-C ("Orange", "banana", "apple", "peach")
Arrays array matrix list data frame Dataframe