1、基本數據類型(numeric,logical,character,NA,double,complex,integer)
2、日期變量
常用函數
Sys.Date()-返回系統當前的日期,Sys.time()-返回系統當前的日期和時間,date()-返回系統當前的日期和時間,
as.Date()-將字符串形式的日期值轉換為日期變量,as.Date(x,format="",...)
as.POSIXllt()-將字符串轉化為包含時間及時區的日期變量,as.POSIXllt(x,tz="",format)
strptime()-將字符串變量轉換為包含時間的日期變量,strptime(x,format,tz="")
strfttime()-將日期變量轉換為指定格式的字符串變量,strfttime(x,format)
format()-將日期變量轉換為指定格式的字符串變量,format(x,...)
3、查看對象的類型
class()、mode()、typeof()
4、數據結構
(1)向量
向量創建:c()函數創建向量
向量索引:#下標方式索引 vector<-c(1,2,3,4) vector[1] vector[c(1:3)]
#按名稱索引 names(vector)<-c("one","two","three","four") vector[c("one","two")]
#which方式索引 which(vector==1) which(vector==c(1,2)) which.max(vector)
#subser方式索引 subset(vector,vector>2&vector<4)
#%in%方式索引 c(1,5)%in%vector
向量編輯 : #向量擴展(x<-c(x,c(5,6,7))) #單個元素的刪除 x<-x[-1] #多個元素的刪除 (x<-x[c(3:5)])
向量排序:sort(x,decreasing = FALSE,na.last = TRUE...) 倒序——rev()函數
等差數列的建立:seq(from = 1, to = 1, by = ((to - from)/length.out - 1),length.out = NULL,...) seq(1,-9,by = -2)
重復數列的建立:rep(x,times=1,length.out=NA,each=1) rep(1:3, each=2, times=2) 112233112233112233
(2)矩陣
創建矩陣: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")))
矩陣和轉換為向量:as.vector(),轉換為向量時元素按列讀取數據
矩陣索引:#根據位置索引 a[2,1]
#根據行和列的名稱索引 a["r2","c2"]
#使用一維下標索引 a[,2]
#使用數值型向量索引 a[c(3:5),2]
矩陣編輯:#矩陣合並(a1<-rbind(a,c(11,12))) (a2<-rbind(a,c(11:15)))
#刪除矩陣中元素 a5<-a[-1,] #刪除矩陣中的第一行
矩陣的運算:colSums()-對矩陣的各列求和 rowSums()-對矩陣的各行求和 colMeans()-對矩陣各列求均值 rowMeans()-對矩陣各行求均值
t()-矩陣行列轉換 det()-求解矩陣的行列式 crossprod()-求解兩個矩陣的內積 outer()-求解矩陣的外積 %*%-矩陣乘法
diag()-對矩陣取對角元素 solve()-對矩陣求解逆矩陣 eigen()-對矩陣求解特征值和特征向量
(3)數組
創建數組: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))
數組索引:#按下標索引 a[2,4,2]
#按維度名稱索引a["A2","B3","C1"]
#查看數組的維度 dim(a)
(4)數據框
創建數據框: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_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_iris<-rbind(data_iris,list(9,9,9,9))
#增加數據集的新屬性變量 data_iris<-rbind(data_iris,Species=rep(7,5))
#數據框列名的編輯 names(data_iris)
(5)因子
創建因子序列 :
將statistics分解成因子型變量,水平為26個小寫字母 (ff<-factor(substring("statistics"),1:10,1:10,levels=letters))
去除沒有包含在向量中的水平 f<-factor(ff)
#創建因子型向量,水平名稱為letter factor(letters[1:20],labels="letter")
#創建有序的因子序列 z<-factor(LETTERS[1:4],ordered=TRUE)
通過gl()函數創建因子序列 gl(n,k,length=n*k,labels=seq_len(n),ordered=TRUE)
n-表示因子水平的個數
k-表示每個水平的重復數
length-表示生成的序列的長度
labels-一個n維向量,表示因子水平
ordered-一個邏輯值,為TRUE表示有序因子,為FALSE則表示無序因子
gl(2,3,labels=c("T","F"))
因子的存儲方式:
> status<-c("Poor","Improved","Excellent","Poor")
> class(status) #查看向量的類型
[1] "character"
> s<-factor(status,ordered=TRUE)
> s
[1] Poor Improved Excellent Poor
Levels: Excellent < Improved < Poor
> class(s)
[1] "ordered" "factor" #查看數據的類型
> storage.mode(s) #查看存儲類型,可以看出因子是按整數存儲的
[1] "integer"
> as.numeric(s) #轉換為數值型向量
[1] 3 2 1 3
> levels(s) #查看因子的水平
[1] "Excellent" "Improved" "Poor"
(6)列表
創建列表:list(object1,object2,...)
data<-list(a=c(1,2,3,4),b=c("true","false"),c=c("one","two","three","four"),d=(1+3i))
列表索引:#列索引 data[[1]] || data$a ||data[["a"]]
#元素索引 data[[1]][1]
列表編輯:列表的編輯和向量類似,使用c()函數進行合並。
#增加名稱為e的一列
data1<-c(data,list(e=c(3,4,5)))
或者data1<-c(data,e=list(c(3,4,5)))