r语言electricity数据集_R语言学习-数据集
數(shù)據(jù)集通常是由數(shù)據(jù)構成的一個矩形數(shù)組,行表示觀測,列表示變量,對數(shù)據(jù)集的操作是數(shù)據(jù)分析的第一步
R語言的數(shù)據(jù)結構通常有以下幾種:標量,向量,矩陣,數(shù)組,數(shù)據(jù)框和列表
向量
向量是用于存儲數(shù)字型、字符型或邏輯型數(shù)據(jù)的一維數(shù)組。執(zhí)行組合功能的函數(shù)c()可用來創(chuàng)建向量
> a
> b
> a
[1] 1 2 3 4 5 6
> b
[1] "one" "two" "three"
通常在方括號中給定元素所處位置的數(shù)值,對向量中的元素進行檢索
> a
> a[5]
[1] "c"
> a[c(1,3,5)]
[1] "k" "h" "c"
> a[2:6]
[1] "j" "h" "a" "c" "m"
矩陣
矩陣是一個二維數(shù)組,每個元素的類型相同,通過函數(shù)matrix()創(chuàng)建矩陣
> y
> y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> cells
> rnames
> cnames
> mymatrix
> mymatrix
C1 C2
R1 1 26
R2 24 28
#這個矩陣中,byrow=TRUE是選擇行填充,dimnames是指定行名和列名
同樣矩陣元素的檢索也是用到方括號
X[i,]是選擇矩陣的第i行,X[,j]是選擇矩陣的第j列,X[i,j]選擇矩陣第i行第j列的元素
> x
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,5]
[1] 9 10
> x[2,5]
[1] 10
數(shù)組
數(shù)組與矩陣類似,到那時數(shù)組的維度可以大于2,數(shù)組可以通過array創(chuàng)建,一般格式如下
myarray
vector包含了數(shù)組中的數(shù)據(jù),dimensions是一個數(shù)值型向量,表示了維度的大小,dimnames則是指定維度的名稱
> dim1
> dim2
> dim3
> z
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24
數(shù)組的檢索與矩陣的檢索相似,通過方括號檢索
數(shù)據(jù)框
數(shù)據(jù)框是R語言中最常用的數(shù)據(jù)集,它不同的列可以包含不同的數(shù)據(jù)類型
數(shù)據(jù)框可以通過data.frame創(chuàng)建
mtdata
其中列向量col1,col2,col3可以為任何類型
> patienID
> age
> diabetes
> status
> patientData
> patientData
patienID age diabetes status
1 1 25 Type1 Poor
2 2 34 Type2 Improved
3 3 28 Type1 Excellent
4 4 52 Type1 Poor
數(shù)據(jù)框的檢索方式如下
> patientData[1,2]
[1] 25
> patientData[1:2]
patienID age
1 1 25
2 2 34
3 3 28
4 4 52
> patientData[c("diabetes","status")]
diabetes status
1 Type1 Poor
2 Type2 Improved
3 Type1 Excellent
4 Type1 Poor
> patientData$age
[1] 25 34 28 52
# $用于選區(qū)給定數(shù)據(jù)框中某個特定的變量
因子
變量可分為名義型變量,有序型,連續(xù)型變量
名義型變量是沒有順序之分的類別型變量,如糖尿病的類型
有序型變量代表一種順序關系,而非數(shù)量關系,比如病情的好壞,壞
連續(xù)型變量則同時表示了順序和數(shù)量
名義型變量和有序型變量在R中稱為因子
factor函數(shù)可以將變量轉化為因子,如果要排序,則需要ordered=TRUE參數(shù),levels可以指定排序順序
> patientID
> age
> diabetes
> status
> diabetes
> status
> patientData
Error: object 'patientData' not found
> patientData
> str(patientData)#顯示數(shù)據(jù)結構
'data.frame': 4 obs. of 4 variables:
$ patientID: num 1 2 3 4
$ age : num 25 34 28 52
$ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1
$ status : Ord.factor w/ 3 levels "Excellent"
> summary(patientData)#統(tǒng)計數(shù)據(jù)框各變量
patientID age diabetes
Min. :1.00 Min. :25.00 Type1:3
1st Qu.:1.75 1st Qu.:27.25 Type2:1
Median :2.50 Median :31.00
Mean :2.50 Mean :34.75
3rd Qu.:3.25 3rd Qu.:38.50
Max. :4.00 Max. :52.00
status
Excellent:1
Improved :1
Poor :2
列表
列表是R中數(shù)據(jù)集最為復雜的一種,是一些對象的有序集合
可以使用list()函數(shù)創(chuàng)建列表
mylist
> g
> h
> j
> k
> mylist
> mylist
$title
[1] "My Frist List"
$ages
[1] 25 26 18 39
[[3]]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
[[4]]
[1] "one" "two" "three"
總結
以上是生活随笔為你收集整理的r语言electricity数据集_R语言学习-数据集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dotnet安装包时找不到依赖关系_无法
- 下一篇: 可靠性测试设备技术含量_电子产品可靠性测