> names(iris)#显示数据集中的变量名
[1] 'Sepal.Length' 'Sepal.Width' 'Petal.Length' 'Petal.Width' 'Species'
> str(iris) #查看数据集结构
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels 'setosa','versicolor',..: 1 1 1 1 1 1 1 1 1 1 ...
> attributes(iris)#得到数据结构的属性列表
$names
[1] 'Sepal.Length' 'Sepal.Width' 'Petal.Length' 'Petal.Width' 'Species'
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
[32] 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
[63] 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
[94] 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
[125] 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
$class
[1] 'data.frame'
> iris[1:5,] #显示数据的前5列
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
> iris[1:10, 'Sepal.Length'] #显示'Sepal.Length'的前10行
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9
> iris$Sepal.Length[1:10] #同上
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9
> summary(iris) #数据集中每个变量总的描述
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50
Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
> library(Hmisc)
> describe(iris[, c(1, 5)]) #同上代码功能,但显示的更漂亮些,呵呵!
iris[, c(1, 5)]
2 Variables 150 Observations
------------------------------------------------------------------------------------------------------------------------------------
Sepal.Length
n missing unique Info Mean .05 .10 .25 .50 .75 .90 .95
150 0 35 1 5.843 4.600 4.800 5.100 5.800 6.400 6.900 7.255
lowest : 4.3 4.4 4.5 4.6 4.7, highest: 7.3 7.4 7.6 7.7 7.9
------------------------------------------------------------------------------------------------------------------------------------
Species
n missing unique
150 0 3
setosa (50, 33%), versicolor (50, 33%), virginica (50, 33%)
------------------------------------------------------------------------------------------------------------------------------------
> range(iris$Sepal.Length) #返回Sepal.Length的最大值和最小值
[1] 4.3 7.9
> quantile(iris$Sepal.Length)#返回分位数
0% 25% 50% 75% 100%
4.3 5.1 5.8 6.4 7.9
> quantile(iris$Sepal.Length, c(0.1, 0.3, 0.65))#返回自定义的分位数
10% 30% 65%
4.80 5.27 6.20
> var(iris$Sepal.Length)
[1] 0.6856935
> hist(iris$Sepal.Length)
> plot(density(iris$Sepal.Length))
> table(iris$Species)
setosa versicolor virginica
50 50 50
> pie(table(iris$Species))
> barplot(table(iris$Species))
cov(iris$Sepal.Length, iris$Petal.Length)
## [1] 1.274315
cor(iris$Sepal.Length, iris$Petal.Length)
## [1] 0.8717538
cov(iris[, 1:4])
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length 0.6856935 -0.0424340 1.2743154 0.5162707
## Sepal.Width -0.0424340 0.1899794 -0.3296564 -0.1216394
## Petal.Length 1.2743154 -0.3296564 3.1162779 1.2956094
## Petal.Width 0.5162707 -0.1216394 1.2956094 0.5810063
> boxplot(Sepal.Length ~ Species, data = iris)
> with(iris, plot(Sepal.Length, Sepal.Width, col = Species,
+ pch = as.numeric(Species)))
> plot(jitter(iris$Sepal.Length), jitter(iris$Sepal.Width))
> pairs(iris)
>library(scatterplot3d)
>scatterplot3d(iris$Petal.Width, iris$Sepal.Length, iris$Sepal.Width)
> dist.matrix <- as.matrix(dist(iris[,="">->
> heatmap(dist.matrix)
> library(lattice)
> levelplot(Petal.Width ~ Sepal.Length * Sepal.Width, iris, cuts = 9,
+ col.regions = rainbow(10)[10:1])
> filled.contour(volcano, color = terrain.colors, asp = 1, plot.axes = contour(volcano,add = T))
> persp(volcano, theta = 25, phi = 30, expand = 0.5, col = 'lightblue')
> library(MASS)
Warning message:
程辑包‘MASS’是用R版本3.0.3 来建造的
> parcoord(iris[1:4], col = iris$Species)
> library(lattice)
> parallelplot(~iris[1:4] | Species, data = iris)