1. Basic data Type (Numeric,logical,character,na,double,complex,integer)
2. Date variables
Common functions
Sys.date ()-Returns the current date of the system, Sys.time ()-Returns the current date and time of the system, date ()-Returns the current date and time of the system,
As. Date ()-Converts a date value in the form of a string to a date variable, as. Date (x,format= "",...)
As. Posixllt ()-Converts a string into a date variable containing a time zone, as. Posixllt (x,tz= "", format)
Strptime ()-Converts a string variable to a date variable containing a time, Strptime (x,format,tz= "")
Strfttime ()-Converts a date variable to a string variable of the specified format, strfttime (X,format)
Format ()-Converts a date variable to a string variable of the specified format, format (x,...)
3. View the type of object
Class (), mode (), typeof ()
4. Data structure
(1) Vector
Vector creation: C () function create vector
Vector index: #下标方式索引 vector<-c (1,2,3,4) vector[1] Vector[c (1:3)]
#按名称索引 names (vector) <-c ("One", "I", "three", "four") Vector[c ("One", "one")]
#which方式索引 which (vector==1) which (vector==c) Which.max (vector)
#subser方式索引 subset (VECTOR,VECTOR>2&VECTOR<4)
#%in% Way index C (1,5)%in%vector
Vector edit: #向量扩展 (X<-c (X,c (5,6,7))) #单个元素的删除 X<-x[-1] #多个元素的删除 (X<-x[c (3:5)])
Vector Sort: sort (x,decreasing = False,na.last = TRUE ...) Reverse--rev () function
Arithmetic progression creation: seq (from = 1, to = 1, by = ((To-from)/length.out-1), length.out = NULL,...) Seq (1,-9,by =-2)
Establishment of a repeating sequence: Rep (x,times=1,length.out=na,each=1) Rep (1:3, each=2, times=2) 112233112233112233
(2) matrix
Create matrix: Matrix (Data=na,nrow=1,ncol=1,byrow=false,dimnames=null)
X<-c (1:9)
A<-matrix (X,nrow=5,ncol=2,byrow=false,dimnames=list (C ("R1", "R2", "R3", "R4", "R5"), C ("C1", "C2")))
Matrix and convert to vector: As.vector (), element reads data by column when converted to vector
Matrix index: #根据位置索引 a[2,1]
#根据行和列的名称索引 a["R2", "C2"]
#使用一维下标索引 a[,2]
#使用数值型向量索引 a[c (3:5), 2]
Matrix editing: #矩阵合并 (A1<-rbind (A,c (11,12))) (A2<-rbind (A,c (11:15)))
#删除矩阵中元素 A5<-a[-1,] #删除矩阵中的第一行
Matrix operations: Colsums ()-Sums the columns of the Matrix Rowsums ()-sums the rows of the Matrix Colmeans ()-the mean value of each column of the Matrix Rowmeans ()-the mean value for each row of the matrix
T ()-matrix row-to-column conversion det ()-Determinant of The Matrix Crossprod ()-Solving the Inner product outer () of two matrices-solving matrix multiplication by the outer product%*%-matrix
Diag ()-diagonal element solve ()-matrix solution Inverse Matrix eigen ()-solving eigenvalues and eigenvectors of matrices
(3) array
Create array: Array (data,dim=length (data), Dimnames=null)
X<-c (1:9)
Dim1<-c ("A1", "A2", "A3")
Dim2<-c ("B1", "B2", "B3", "B4", "B5")
Dim3<-c ("C1", "C2")
A<-array (X,dim=c (3,5,2), Dimnames=list (DIM1,DIM2,DIM3))
Array index: #按下标索引 a[2,4,2]
#按维度名称索引a ["A2", "B3", "C1"]
#查看数组的维度 Dim (A)
(4) Data frame
Create Data frame: Data.frame ()
#向量组成数据框
Data_iris<-data.frame (S.length=c (1,1,1,1), S.width=c (2,2,2,2), W.length=c (3,3,3,3), W.width=c (4,4,4,4))
#矩阵组成数据框
Data_matrix<-matrix (c (1:8), C (4,2))
Data_iris2<-data.frame (Data_matrix)
Data frame index: #列索引 data_iris[,1] | | Data_iris$s.length | | data_iris["S,length"]
#行索引 data_iris[1,] | | Data_iris[1:3,]
#元素索引 data_iris[1,1] data_iris$s.length[1] data_iris["S,length"][1]
#subset索引 subset (Data_iris, s.length=1)
#sqldf函数索引 Library (SQLDF) newdf<-sqldf ("select * from Mtcars where carb=1 order by mpg", row.names=true)
Data frame editing: #增加新的样本数据 data_iris<-rbind (Data_iris,list (9,9,9,9))
#增加数据集的新属性变量 Data_iris<-rbind (Data_iris,species=rep (7,5))
The foundation of R language--data object and data reading and writing