數(shù)據(jù)類型
1.數(shù)值型2.字符型3.邏輯型4.復(fù)數(shù)型5.特殊值
1.數(shù)值型
就是數(shù)字蔬螟。
> x<-7
> class(x)
[1] "numeric"
2.字符型
使用單引號或者雙引號括起來的就是字符
> x<-"7"
> class(x)
[1] "character"
3.邏輯型
> x<-T#真(T,TRUE)假(F,FALSE)
> class(x)
[1] "logical"
> x<-True
Error: object 'True' not found
4.復(fù)數(shù)型
> x<- 7+7i
> class(x)
[1] "complex"
5.特殊值
指Inf,NA,NaN
Inf:infinite無窮大的意思,還有-Inf
NA:not available腮猖,表示缺失值或者NULL
NaN:計算產(chǎn)生的沒意義的結(jié)果问欠,not a number
數(shù)據(jù)結(jié)構(gòu)
1.向量2.矩陣3.數(shù)組4.因子5.數(shù)據(jù)框6.列表
VECTOR
創(chuàng)建vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
days_win_vector<-c(1,2,3,4,5)
days_win2_vector<-c(1:5)
days_win3_vector<-1:5
命名
names(days_win_vector)<-days_vector
查看屬性
str(days_win_vector)
選取子集
###by number &反選
days_win_vector[1]
days_win_vector[c(1,2)]
days_win_vector[-1]
days_win_vector[-c(1,2)]
###by names
days_win_vector["Monday"]
days_win_vector[c("Monday","Tuesday")]
###by 條件
####布爾值
days_win_vector>3
days_vector!="Monday"
days_win_vector[days_win_vector>3]
subset(days_win_vector,days_win_vector>3)
days_vector[days_vector!="Monday"]
subset(days_vector,days_vector!="Monday")
days_win_vector[days_win_vector>3&days_win_vector<5]
其它操作
###獲取向量長度
length(days_win_vector)
###獲取均值,最大懂讯,最小,和台颠,標(biāo)準(zhǔn)差
mean(days_win_vector)
max(days_win_vector)
min(days_win_vector)
sum(days_win_vector)
sd(days_win_vector)
###
x<-c(1,2,4,3,7,5,4)
sort(x,decreasing = T)
sort(x, decreasing = FALSE)
向量中的強制轉(zhuǎn)換原則
logical<numeric<character
> x<-c(T,T,F,T)
> str(x)
logi [1:4] TRUE TRUE FALSE TRUE
> x[2]<-2
> x
[1] 1 2 0 1
> str(x)
num [1:4] 1 2 0 1
> x[1]<-"1"
> str(x)
chr [1:4] "1" "2" "0" "1"
MATRIX
創(chuàng)建
x<-matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
dimnames = NULL)
data<-1:15
cn<-c("a","b","c","d","e")
rn<-c("c1","c2","c3")
mymatrix<-matrix(data = data,nrow = 3,ncol = 5,dimnames = list(rn,cn))
查看屬性
str(mymatrix)
選取子集
mymatrix[1,]
mymatrix[,1]
mymatrix[1,c(2,3)]
mymatrix[-1,]
數(shù)據(jù)框
創(chuàng)建
mydata<-data.frame(col1, col2, col3,...)
x<-c(1, 2, 3)
y<-c("large", "median", "small")
z<-c(NaN, Inf, 5)
mydata<-data.frame(x, y, z)
查看屬性
str(mydata)
is.data.frame(mydata)
names(mydata)
選取子集
mydata[1,2]
mydata[1,]
mydata[,2]
mydata$x
總想加點什么
md=mydata
md[,4]<-c(4:6)
mydata$u<-1
mydata$v<-c(4,5,6)
w<-c(9,0,0)
mydata$w<-w
要刪的話
mydata$w<-NULL
其他褐望。提取子集方法,重新賦值串前。
重命名列名
names(mydata)<-c("x","y" ,"h" ,"u", "p")
names(mydata)[names(mydata) == "y"] = c("fff")
names(mydata)[4] = c("a")
任務(wù):
#1.自己創(chuàng)建兩個數(shù)據(jù)框(矩陣瘫里,向量),按列按行兩種合并荡碾。觀察并寫下筆記
#2.記錄整理is.datatype,as.datatype類型函數(shù)谨读,寫筆記。
#3.查看rnrom()函數(shù)坛吁,并使用它做一下基礎(chǔ)的散點圖劳殖,直方圖贼邓,密度圖。筆記闷尿。
#4.以上都不需要library()其他包塑径。
#5.下次會說明factor,并講述基礎(chǔ)圖的做法填具,以及package的安裝方法统舀,如果想看看的話,可以看看這個http://blog.csdn.net/BOOMBOY/article/details/77477181
#快捷鍵:1.清屏:Ctrl+L 2.執(zhí)行腳本:Ctrl+R 3.刪除當(dāng)前位置到行尾:Ctrl+K
#4.刪除當(dāng)前行所有: Ctrl+U 5.復(fù)制黏貼正常使用
#6.和linux 相同的 Tab可以補全劳景,上下鍵調(diào)用歷史
#有用命令:history() ls() rm(list=ls()) help() setwd() getwd() help.start()