实践1(最初几步)

library(tidyverse)
## -- Attaching packages -------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.1     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## -- Conflicts ----------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
(x <- 1:100)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100
sample(x,20)
##  [1] 13 11 36 83 44 27 31 86 25 78 74 56 98 81  2 21 97 45  8 89
set.seed(100)
sample(x,20)
##  [1] 74 89 78 23 86 70  4 55 95  7 91 93 43 82 61 12 51 72 18 25
z <- sample(1:200000,1000) # 不放回抽样
z[1:10]
##  [1] 126387 123828   8539 187769 123485  20126  78557 149087  16607  15068
z[c(1:10)]
##  [1] 126387 123828   8539 187769 123485  20126  78557 149087  16607  15068
z1 <- sample(x,200,replace = TRUE) # 放回抽样
unique(z1)
##  [1] 96 33 89 80 78 79 13 61 44 53 56 68 91 97 73 25 95 98 84 21 14 16 58
## [24] 75 94 65 11 27 23  9 62 41 31 60 30 83 90 47 72 51 87 64  4 45 38 18
## [47] 99  1 71 49 40 92  6 76 34 15 43 93 24 46 59 69 54 77 63 20 39  7 86
## [70] 74 88 32 37 35 52 82 55 22 50 85 81 57 17 19 48 70  8
table(z1)
## z1
##  1  4  6  7  8  9 11 13 14 15 16 17 18 19 20 21 22 23 24 25 27 30 31 32 33 
##  1  3  3  3  1  2  3  3  3  3  1  2  3  2  2  2  2  3  3  4  2  3  2  1  3 
## 34 35 37 38 39 40 41 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 
##  4  1  1  4  5  3  2  1  4  3  1  3  1  1  3  2  1  4  1  1  1  1  5  1  5 
## 61 62 63 64 65 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 
##  1  1  3  2  3  1  1  1  4  1  1  1  3  1  2  2  4  1  2  2  1  4  1  1  3 
## 88 89 90 91 92 93 94 95 96 97 98 99 
##  2  3  2  5  2  2  1  5  1  4  4  3
data <- tibble(x = 1:10,
               y = 2:11,
               z = rep(c(TRUE,FALSE),time = 5)
                       )
setdiff(1:10,2:11)  # 集合差
## [1] 1
setdiff(data$y,data$x) 
## [1] 11
union(data$x,data$y) # 集合并
##  [1]  1  2  3  4  5  6  7  8  9 10 11
union(data$x,data$y) %>% sort(decreasing = TRUE)
##  [1] 11 10  9  8  7  6  5  4  3  2  1
intersect(data$x,data$y) # 集合∩
## [1]  2  3  4  5  6  7  8  9 10
sample(1:100,20,prob = 1:100)  # 不等概率随机抽样,抽到概率与1:100成比例
##  [1] 35 78 32 73 88 66 90 33 21 60 50 56 12  7 64 48 76 94 62 43

实践2(一些简单运算)

pi*10/2
## [1] 15.70796
x <- 15*3-5+pi*exp(5.5)
x
## [1] 808.7224
print(x)
## [1] 808.7224
(x <- 59/5*6) # 赋值带打印
## [1] 70.8
pi^(1:5)
## [1]   3.141593   9.869604  31.006277  97.409091 306.019685
print(x,digits = 5)
## [1] 70.8

实践3(R数据对象)

x <- pi*10^5
x %>%  class() 
## [1] "numeric"
x %>% typeof()
## [1] "double"
cars %>% typeof()
## [1] "list"
cars %>% class()
## [1] "data.frame"
cars %>% summary()
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
cars %>% head()
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
cars %>% tail()
##    speed dist
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85
cars %>% str()
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
cars %>% row.names()  # 行名
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28"
## [29] "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" "41" "42"
## [43] "43" "44" "45" "46" "47" "48" "49" "50"
cars %>% attributes() # 包括名字,类型和行名
## $names
## [1] "speed" "dist" 
## 
## $class
## [1] "data.frame"
## 
## $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] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
class(dist~speed) # formula
## [1] "formula"
plot(cars$dist,cars$speed,xlab = "dist",ylab = "speed") # 散点图

实践4(线性回归)

先介绍cut函数

# Examples
Z <- stats::rnorm(100)
table(cut(Z, breaks = -6:6,include.lowest = TRUE))
## 
## [-6,-5] (-5,-4] (-4,-3] (-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3] 
##       0       0       0       1      13      35      33      14       3 
##   (3,4]   (4,5]   (5,6] 
##       1       0       0
sum(table(cut(Z, breaks = -6:6, labels = FALSE)))
## [1] 100
sum(graphics::hist(Z, breaks = -6:6, plot = FALSE)$counts)
## [1] 100
cut(rep(1,5), 4) #-- dummy
## [1] (0.9995,1] (0.9995,1] (0.9995,1] (0.9995,1] (0.9995,1]
## Levels: (0.999,0.9995] (0.9995,1] (1,1.0005] (1.0005,1.001]
tx0 <- c(9, 4, 6, 5, 3, 10, 5, 3, 5)
x <- rep(0:8, tx0)
stopifnot(table(x) == tx0)

table( cut(x, b = 8))
## 
## (-0.008,1]      (1,2]      (2,3]      (3,4]      (4,5]      (5,6] 
##         13          6          5          3         10          5 
##      (6,7]   (7,8.01] 
##          3          5
table( cut(x, breaks = 3*(-2:5)))
## 
## (-6,-3]  (-3,0]   (0,3]   (3,6]   (6,9]  (9,12] (12,15] 
##       0       9      15      18       8       0       0
table( cut(x, breaks = 3*(-2:5), right = FALSE))
## 
## [-6,-3)  [-3,0)   [0,3)   [3,6)   [6,9)  [9,12) [12,15) 
##       0       0      19      18      13       0       0
##--- some values OUTSIDE the breaks :
table(cx  <- cut(x, breaks = 2*(0:4)))
## 
## (0,2] (2,4] (4,6] (6,8] 
##    10     8    15     8
table(cxl <- cut(x, breaks = 2*(0:4), right = FALSE))
## 
## [0,2) [2,4) [4,6) [6,8) 
##    13    11    13     8
which(is.na(cx));  x[is.na(cx)]  #-- the first 9  values  0
## [1] 1 2 3 4 5 6 7 8 9
## [1] 0 0 0 0 0 0 0 0 0
which(is.na(cxl)); x[is.na(cxl)] #-- the last  5  values  8
## [1] 46 47 48 49 50
## [1] 8 8 8 8 8
## Label construction:
y <- stats::rnorm(100)
table(cut(y, breaks = pi/3*(-3:3)))
## 
## (-3.14,-2.09] (-2.09,-1.05]     (-1.05,0]      (0,1.05]   (1.05,2.09] 
##             0            15            40            35             9 
##   (2.09,3.14] 
##             1
table(cut(y, breaks = pi/3*(-3:3), dig.lab = 4))
## 
## (-3.142,-2.094] (-2.094,-1.047]      (-1.047,0]       (0,1.047] 
##               0              15              40              35 
##   (1.047,2.094]   (2.094,3.142] 
##               9               1
table(cut(y, breaks =  1*(-3:3), dig.lab = 4))
## 
## (-3,-2] (-2,-1]  (-1,0]   (0,1]   (1,2]   (2,3] 
##       0      18      37      33      11       1
table(cut(y, breaks =  1*(-3:3), dig.lab = 4)) %>% sum()
## [1] 100
# extra digits don't "harm" here
table(cut(y, breaks =  1*(-3:3), right = FALSE))
## 
## [-3,-2) [-2,-1)  [-1,0)   [0,1)   [1,2)   [2,3) 
##       0      18      37      33      11       1
#- the same, since no exact INT!

## sometimes the default dig.lab is not enough to be avoid confusion:
aaa <- c(1,2,3,4,5,2,3,4,5,6,7)
cut(aaa, 3) %>% class()
## [1] "factor"
cut(aaa, 3, dig.lab = 4, ordered = TRUE) %>% str()
##  Ord.factor w/ 3 levels "(0.994,3]"<"(3,5]"<..: 1 1 1 2 2 1 1 2 2 3 ...
## one way to extract the breakpoints
labs <- levels(cut(aaa, 3))
cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
      upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
##      lower upper
## [1,] 0.994  3.00
## [2,] 3.000  5.00
## [3,] 5.000  7.01
ncol(cars);nrow(cars)
## [1] 2
## [1] 50
dim(cars)
## [1] 50  2
library(DT)

table(cars$speed)
## 
##  4  7  8  9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 
##  2  2  1  1  3  2  4  4  4  3  2  3  4  3  5  1  1  4  1
quantile(cars$speed)
##   0%  25%  50%  75% 100% 
##    4   12   15   19   25
cars %>% datatable()
# 简单线性回归
lm_fit <- lm(dist~speed,data = cars)
summary(lm_fit)
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -29.069  -9.525  -2.272   9.215  43.201 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.5791     6.7584  -2.601   0.0123 *  
## speed         3.9324     0.4155   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared:  0.6511, Adjusted R-squared:  0.6438 
## F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12
cars <- cars %>% 
  mutate(qspeed = cut(speed,breaks = quantile(speed),include.lowest = TRUE))

cars[3] %>% class()   # 数据框
## [1] "data.frame"
cars[,3] %>% class()  # 因子类型
## [1] "factor"
table(cars$qspeed)
## 
##  [4,12] (12,15] (15,19] (19,25] 
##      15      11      12      12
cars %>% str()
## 'data.frame':    50 obs. of  3 variables:
##  $ speed : num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist  : num  2 10 4 22 16 10 18 26 34 17 ...
##  $ qspeed: Factor w/ 4 levels "[4,12]","(12,15]",..: 1 1 1 1 1 1 1 1 1 1 ...
plot(cars$dist~cars$qspeed)

lm_fit2 <- lm(dist~.,data = cars)  # 多元线性回归
summary(lm_fit2)
## 
## Call:
## lm(formula = dist ~ ., data = cars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -28.085  -9.791  -2.837   8.441  42.841 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -21.354     11.918  -1.792 0.079889 .  
## speed            4.268      1.209   3.531 0.000968 ***
## qspeed(12,15]    2.166      8.406   0.258 0.797837    
## qspeed(15,19]   -3.888     11.848  -0.328 0.744313    
## qspeed(19,25]   -3.929     16.745  -0.235 0.815546    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.77 on 45 degrees of freedom
## Multiple R-squared:  0.6562, Adjusted R-squared:  0.6256 
## F-statistic: 21.47 on 4 and 45 DF,  p-value: 5.826e-10
lm_fit3 <- lm(dist~qspeed,data = cars)  # x为定性变量
summary(lm_fit3)
## 
## Call:
## lm(formula = dist ~ qspeed, data = cars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -37.33 -13.96  -3.75   9.30  50.67 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     18.200      4.551   3.999 0.000228 ***
## qspeed(12,15]   21.982      6.996   3.142 0.002933 ** 
## qspeed(15,19]   31.967      6.826   4.683 2.52e-05 ***
## qspeed(19,25]   51.133      6.826   7.491 1.68e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.62 on 46 degrees of freedom
## Multiple R-squared:  0.5609, Adjusted R-squared:  0.5322 
## F-statistic: 19.59 on 3 and 46 DF,  p-value: 2.517e-08

实践5(简单样本描述统计量)

x <- (round(rnorm(200,0,1),digits = 3))

sum(x)
## [1] -15.549
mean(x)
## [1] -0.077745
median(x)
## [1] -0.1465
sd(x)
## [1] 1.03166
rank(x)
##   [1] 146.0 185.0  78.0 110.0  81.0  56.0 166.0  85.0 121.0 159.0  30.0
##  [12] 128.0 129.0 127.0  13.0  99.5  35.0 196.0 115.0 140.0 194.0  58.0
##  [23]  33.0  48.0  89.5 158.0  68.0  91.0  41.0 124.0  63.0 193.0  22.0
##  [34]  66.5  74.5  16.0 187.0  43.0  12.0 112.0 188.0 149.0   4.0 133.0
##  [45] 123.0 195.0  96.0 126.0 131.0 118.0  97.0 181.0  65.0 119.0 163.0
##  [56] 106.0  46.0 102.0   6.0  99.5 164.0 107.0  89.5  40.0  61.0  23.0
##  [67]  39.0 145.0  10.0 190.0 171.0 153.0 114.0 176.0 148.0  27.0 104.0
##  [78]  15.0 199.0  72.0 150.0  32.0 151.0  57.0  98.0 167.0 156.0  51.0
##  [89] 143.0  82.0  42.0  79.0 168.0 111.0 178.0 108.0  74.5  80.0 177.0
## [100] 179.0 142.0  47.0  84.0 116.0 197.0  88.0 113.0 109.0  92.0 173.0
## [111] 189.0   8.0 182.0  36.0 103.0 198.0  70.0  64.0  21.0  38.0 157.0
## [122]   5.0  73.0 155.0   7.0 135.0 120.0 165.0 200.0  44.0 147.0  53.0
## [133]  37.0  62.0  45.0 174.0 144.0 184.0 125.0  69.0  17.0  52.0  11.0
## [144]  20.0  55.0 191.0 122.0 138.0  49.0  76.0  14.0 101.0  95.0 152.0
## [155]  77.0  71.0  87.0  25.0 186.0 130.0 141.0  54.0 175.0 161.0  18.0
## [166] 192.0 134.0 105.0  60.0  66.5 162.0 180.0  28.0  26.0 132.0 139.0
## [177]   9.0 172.0   3.0  31.0  24.0  93.0 154.0 169.0 183.0  29.0 136.0
## [188]  34.0  50.0 170.0   2.0 160.0   1.0 117.0 137.0  94.0  83.0  59.0
## [199]  19.0  86.0
range(x)
## [1] -3.541  3.021
summary(x)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -3.54100 -0.72500 -0.14650 -0.07774  0.50850  3.02100
order(x)
##   [1] 193 191 179  43 122  59 125 112 177  69 143  39  15 151  78  36 141
##  [18] 165 199 144 119  33  66 181 158 174  76 173 186  11 180  82  23 188
##  [35]  17 114 133 120  67  64  29  91  38 130 135  57 102  24 149 189  88
##  [52] 142 132 162 145   6  84  22 198 169  65 134  31 118  53  34 170  27
##  [69] 140 117 156  80 123  35  97 150 155   3  92  98   5  90 197 103   8
##  [86] 200 157 106  25  63  28 109 182 196 153  47  51  85  16  60 152  58
## [103] 115  77 168  56  62  96 108   4  94  40 107  73  19 104 194  50  54
## [120] 127   9 147  45  30 139  48  14  12  13 160  49 175  44 167 126 187
## [137] 195 148 176  20 161 101  89 137  68   1 131  75  42  81  83 154  72
## [154] 183 124  87 121  26  10 192 164 171  55  61 128   7  86  93 184 190
## [171]  71 178 110 136 163  74  99  95 100 172  52 113 185 138   2 159  37
## [188]  41 111  70 146 166  32  21  46  18 105 116  79 129
x[order(x)]  # 从小到大排列
##   [1] -3.541 -2.977 -2.913 -2.570 -2.274 -2.095 -1.861 -1.784 -1.687 -1.637
##  [11] -1.601 -1.591 -1.532 -1.466 -1.456 -1.451 -1.420 -1.416 -1.397 -1.299
##  [21] -1.216 -1.214 -1.213 -1.206 -1.201 -1.175 -1.160 -1.151 -1.110 -1.061
##  [31] -1.015 -0.999 -0.956 -0.931 -0.922 -0.919 -0.909 -0.862 -0.854 -0.841
##  [41] -0.836 -0.835 -0.814 -0.812 -0.792 -0.770 -0.746 -0.743 -0.736 -0.731
##  [51] -0.723 -0.716 -0.693 -0.643 -0.632 -0.610 -0.606 -0.605 -0.604 -0.585
##  [61] -0.582 -0.573 -0.569 -0.543 -0.516 -0.474 -0.474 -0.435 -0.423 -0.416
##  [71] -0.397 -0.358 -0.343 -0.332 -0.332 -0.326 -0.318 -0.304 -0.287 -0.285
##  [81] -0.279 -0.276 -0.242 -0.235 -0.220 -0.213 -0.205 -0.199 -0.196 -0.196
##  [91] -0.193 -0.188 -0.184 -0.183 -0.172 -0.168 -0.166 -0.160 -0.147 -0.147
## [101] -0.146 -0.132 -0.131 -0.123 -0.114 -0.098 -0.092 -0.060 -0.054 -0.053
## [111] -0.041 -0.009  0.000  0.001  0.005  0.013  0.014  0.021  0.039  0.105
## [121]  0.110  0.116  0.118  0.130  0.143  0.157  0.158  0.183  0.185  0.194
## [131]  0.212  0.220  0.229  0.265  0.267  0.284  0.286  0.291  0.346  0.364
## [141]  0.376  0.389  0.429  0.430  0.439  0.448  0.481  0.489  0.501  0.506
## [151]  0.516  0.522  0.532  0.557  0.560  0.597  0.625  0.642  0.677  0.679
## [161]  0.718  0.721  0.725  0.730  0.759  0.821  0.874  0.918  0.931  0.980
## [171]  0.989  1.027  1.033  1.054  1.118  1.125  1.152  1.235  1.305  1.366
## [181]  1.386  1.434  1.449  1.457  1.462  1.486  1.596  1.633  1.670  1.675
## [191]  1.678  1.733  1.926  1.937  1.938  1.960  2.183  2.406  2.413  3.021
sort(x)
##   [1] -3.541 -2.977 -2.913 -2.570 -2.274 -2.095 -1.861 -1.784 -1.687 -1.637
##  [11] -1.601 -1.591 -1.532 -1.466 -1.456 -1.451 -1.420 -1.416 -1.397 -1.299
##  [21] -1.216 -1.214 -1.213 -1.206 -1.201 -1.175 -1.160 -1.151 -1.110 -1.061
##  [31] -1.015 -0.999 -0.956 -0.931 -0.922 -0.919 -0.909 -0.862 -0.854 -0.841
##  [41] -0.836 -0.835 -0.814 -0.812 -0.792 -0.770 -0.746 -0.743 -0.736 -0.731
##  [51] -0.723 -0.716 -0.693 -0.643 -0.632 -0.610 -0.606 -0.605 -0.604 -0.585
##  [61] -0.582 -0.573 -0.569 -0.543 -0.516 -0.474 -0.474 -0.435 -0.423 -0.416
##  [71] -0.397 -0.358 -0.343 -0.332 -0.332 -0.326 -0.318 -0.304 -0.287 -0.285
##  [81] -0.279 -0.276 -0.242 -0.235 -0.220 -0.213 -0.205 -0.199 -0.196 -0.196
##  [91] -0.193 -0.188 -0.184 -0.183 -0.172 -0.168 -0.166 -0.160 -0.147 -0.147
## [101] -0.146 -0.132 -0.131 -0.123 -0.114 -0.098 -0.092 -0.060 -0.054 -0.053
## [111] -0.041 -0.009  0.000  0.001  0.005  0.013  0.014  0.021  0.039  0.105
## [121]  0.110  0.116  0.118  0.130  0.143  0.157  0.158  0.183  0.185  0.194
## [131]  0.212  0.220  0.229  0.265  0.267  0.284  0.286  0.291  0.346  0.364
## [141]  0.376  0.389  0.429  0.430  0.439  0.448  0.481  0.489  0.501  0.506
## [151]  0.516  0.522  0.532  0.557  0.560  0.597  0.625  0.642  0.677  0.679
## [161]  0.718  0.721  0.725  0.730  0.759  0.821  0.874  0.918  0.931  0.980
## [171]  0.989  1.027  1.033  1.054  1.118  1.125  1.152  1.235  1.305  1.366
## [181]  1.386  1.434  1.449  1.457  1.462  1.486  1.596  1.633  1.670  1.675
## [191]  1.678  1.733  1.926  1.937  1.938  1.960  2.183  2.406  2.413  3.021
order(x,decreasing = TRUE)
##   [1] 129  79 116 105  18  46  21  32 166 146  70 111  41  37 159   2 138
##  [18] 185 113  52 172 100  95  99  74 163 136 110 178  71 190 184  93  86
##  [35]   7 128  61  55 171 164 192  10  26 121  87 124 183  72 154  83  81
##  [52]  42  75 131   1  68 137  89 101 161  20 176 148 195 187 126 167  44
##  [69] 175  49 160  13  12  14  48 139  30  45 147   9 127  54  50 194 104
##  [86]  19  73 107  40  94   4 108  96  62  56 168  77 115  58 152  16  60
## [103]  85  51  47 153 196 182 109  28  25  63 106 157 200   8 103 197  90
## [120]   5  98  92   3 155 150  35  97 123  80 156 117 140  27  34 170  53
## [137] 118  31 134  65 169 198  22  84   6 145 162 132 142  88 189 149  24
## [154] 102  57 135 130  38  91  29  64  67 120 133 114  17 188  23  82 180
## [171]  11 186 173  76 174 158 181  66  33 119 144 199 165 141  36  78 151
## [188]  15  39 143  69 177 112 125  59 122  43 179 191 193
x[order(x,decreasing = TRUE)] # 从大到小排列
##   [1]  3.021  2.413  2.406  2.183  1.960  1.938  1.937  1.926  1.733  1.678
##  [11]  1.675  1.670  1.633  1.596  1.486  1.462  1.457  1.449  1.434  1.386
##  [21]  1.366  1.305  1.235  1.152  1.125  1.118  1.054  1.033  1.027  0.989
##  [31]  0.980  0.931  0.918  0.874  0.821  0.759  0.730  0.725  0.721  0.718
##  [41]  0.679  0.677  0.642  0.625  0.597  0.560  0.557  0.532  0.522  0.516
##  [51]  0.506  0.501  0.489  0.481  0.448  0.439  0.430  0.429  0.389  0.376
##  [61]  0.364  0.346  0.291  0.286  0.284  0.267  0.265  0.229  0.220  0.212
##  [71]  0.194  0.185  0.183  0.158  0.157  0.143  0.130  0.118  0.116  0.110
##  [81]  0.105  0.039  0.021  0.014  0.013  0.005  0.001  0.000 -0.009 -0.041
##  [91] -0.053 -0.054 -0.060 -0.092 -0.098 -0.114 -0.123 -0.131 -0.132 -0.146
## [101] -0.147 -0.147 -0.160 -0.166 -0.168 -0.172 -0.183 -0.184 -0.188 -0.193
## [111] -0.196 -0.196 -0.199 -0.205 -0.213 -0.220 -0.235 -0.242 -0.276 -0.279
## [121] -0.285 -0.287 -0.304 -0.318 -0.326 -0.332 -0.332 -0.343 -0.358 -0.397
## [131] -0.416 -0.423 -0.435 -0.474 -0.474 -0.516 -0.543 -0.569 -0.573 -0.582
## [141] -0.585 -0.604 -0.605 -0.606 -0.610 -0.632 -0.643 -0.693 -0.716 -0.723
## [151] -0.731 -0.736 -0.743 -0.746 -0.770 -0.792 -0.812 -0.814 -0.835 -0.836
## [161] -0.841 -0.854 -0.862 -0.909 -0.919 -0.922 -0.931 -0.956 -0.999 -1.015
## [171] -1.061 -1.110 -1.151 -1.160 -1.175 -1.201 -1.206 -1.213 -1.214 -1.216
## [181] -1.299 -1.397 -1.416 -1.420 -1.451 -1.456 -1.466 -1.532 -1.591 -1.601
## [191] -1.637 -1.687 -1.784 -1.861 -2.095 -2.274 -2.570 -2.913 -2.977 -3.541
sort(x,decreasing = TRUE)
##   [1]  3.021  2.413  2.406  2.183  1.960  1.938  1.937  1.926  1.733  1.678
##  [11]  1.675  1.670  1.633  1.596  1.486  1.462  1.457  1.449  1.434  1.386
##  [21]  1.366  1.305  1.235  1.152  1.125  1.118  1.054  1.033  1.027  0.989
##  [31]  0.980  0.931  0.918  0.874  0.821  0.759  0.730  0.725  0.721  0.718
##  [41]  0.679  0.677  0.642  0.625  0.597  0.560  0.557  0.532  0.522  0.516
##  [51]  0.506  0.501  0.489  0.481  0.448  0.439  0.430  0.429  0.389  0.376
##  [61]  0.364  0.346  0.291  0.286  0.284  0.267  0.265  0.229  0.220  0.212
##  [71]  0.194  0.185  0.183  0.158  0.157  0.143  0.130  0.118  0.116  0.110
##  [81]  0.105  0.039  0.021  0.014  0.013  0.005  0.001  0.000 -0.009 -0.041
##  [91] -0.053 -0.054 -0.060 -0.092 -0.098 -0.114 -0.123 -0.131 -0.132 -0.146
## [101] -0.147 -0.147 -0.160 -0.166 -0.168 -0.172 -0.183 -0.184 -0.188 -0.193
## [111] -0.196 -0.196 -0.199 -0.205 -0.213 -0.220 -0.235 -0.242 -0.276 -0.279
## [121] -0.285 -0.287 -0.304 -0.318 -0.326 -0.332 -0.332 -0.343 -0.358 -0.397
## [131] -0.416 -0.423 -0.435 -0.474 -0.474 -0.516 -0.543 -0.569 -0.573 -0.582
## [141] -0.585 -0.604 -0.605 -0.606 -0.610 -0.632 -0.643 -0.693 -0.716 -0.723
## [151] -0.731 -0.736 -0.743 -0.746 -0.770 -0.792 -0.812 -0.814 -0.835 -0.836
## [161] -0.841 -0.854 -0.862 -0.909 -0.919 -0.922 -0.931 -0.956 -0.999 -1.015
## [171] -1.061 -1.110 -1.151 -1.160 -1.175 -1.201 -1.206 -1.213 -1.214 -1.216
## [181] -1.299 -1.397 -1.416 -1.420 -1.451 -1.456 -1.466 -1.532 -1.591 -1.601
## [191] -1.637 -1.687 -1.784 -1.861 -2.095 -2.274 -2.570 -2.913 -2.977 -3.541
length(x)
## [1] 200
round(x,2) # 四舍五入
##   [1]  0.45  1.46 -0.30 -0.05 -0.28 -0.61  0.82 -0.22  0.11  0.68 -1.06
##  [12]  0.18  0.18  0.16 -1.53 -0.15 -0.92  1.96  0.00  0.36  1.94 -0.60
##  [23] -0.96 -0.74 -0.20  0.64 -0.44 -0.19 -0.84  0.13 -0.57  1.93 -1.21
##  [34] -0.47 -0.33 -1.45  1.60 -0.81 -1.59 -0.01  1.63  0.50 -2.57  0.23
##  [45]  0.12  1.94 -0.17  0.16  0.21  0.02 -0.17  1.39 -0.52  0.04  0.72
##  [56] -0.10 -0.77 -0.13 -2.10 -0.15  0.73 -0.09 -0.20 -0.84 -0.58 -1.21
##  [67] -0.85  0.44 -1.64  1.68  0.99  0.53  0.00  1.12  0.49 -1.16 -0.12
##  [78] -1.46  2.41 -0.36  0.51 -1.00  0.52 -0.61 -0.16  0.87  0.60 -0.72
##  [89]  0.43 -0.28 -0.84 -0.29  0.92 -0.04  1.24 -0.06 -0.33 -0.28  1.15
## [100]  1.30  0.39 -0.75 -0.24  0.01  2.18 -0.20  0.00 -0.05 -0.19  1.03
## [111]  1.67 -1.78  1.43 -0.92 -0.13  2.41 -0.42 -0.54 -1.22 -0.86  0.62
## [122] -2.27 -0.34  0.56 -1.86  0.27  0.10  0.76  3.02 -0.81  0.48 -0.69
## [133] -0.91 -0.57 -0.79  1.05  0.43  1.46  0.14 -0.42 -1.42 -0.72 -1.60
## [144] -1.30 -0.63  1.68  0.12  0.29 -0.74 -0.33 -1.47 -0.15 -0.17  0.52
## [155] -0.32 -0.40 -0.20 -1.20  1.49  0.19  0.38 -0.64  1.12  0.72 -1.42
## [166]  1.73  0.26 -0.11 -0.58 -0.47  0.72  1.37 -1.15 -1.18  0.22  0.35
## [177] -1.69  1.03 -2.91 -1.01 -1.21 -0.18  0.56  0.93  1.45 -1.11  0.28
## [188] -0.93 -0.73  0.98 -2.98  0.68 -3.54  0.01  0.29 -0.18 -0.24 -0.60
## [199] -1.40 -0.21
fivenum(x)
## [1] -3.5410 -0.7270 -0.1465  0.5110  3.0210
quantile(x) # 分位数
##      0%     25%     50%     75%    100% 
## -3.5410 -0.7250 -0.1465  0.5085  3.0210
mad(1:2)
## [1] 0.7413
# 累计函数
cummax(1:10)
##  [1]  1  2  3  4  5  6  7  8  9 10
cummin(1:10)
##  [1] 1 1 1 1 1 1 1 1 1 1
cumall(1:10<5)
##  [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
cumany(1:10>5)
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
cummean(1:10)
##  [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
cumprod(1:10)
##  [1]       1       2       6      24     120     720    5040   40320
##  [9]  362880 3628800
cor(x,sin(x/20)) # 线性相关系数
## [1] 0.999999
cbind(x,sin(x/20)) %>% head()
##           x             
## [1,]  0.448  0.022398127
## [2,]  1.462  0.073034914
## [3,] -0.304 -0.015199415
## [4,] -0.053 -0.002649997
## [5,] -0.279 -0.013949548
## [6,] -0.610 -0.030495271

实践6(简单图形)

x <- rnorm(100)
hist(x,col = "light blue")
rug(x)  # 加上实际点的大小

stem(x)
## 
##   The decimal point is at the |
## 
##   -2 | 753
##   -1 | 88543222100
##   -0 | 9877777655554444443333222222110
##    0 | 001111122223333334444444556778889
##    1 | 2222233344566679
##    2 | 134557
# 构造一个线性关系

x <- rnorm(200)
y <- rnorm(200) + x
plot(x,y)
a <- lm(y~x)
abline(a,col = "blue") # 散点图加拟合线

paste("x的最小值 = ",min(x)) # 打印
## [1] "x的最小值 =  -3.16613468813753"

Here is some code which illustrates some of the differences between R and S graphics capabilities. Note that colors are generally specified by a character string name (taken from the X11 rgb.txt file) and that line textures are given similarly. The parameter “bg” sets the background parameter for the plot and there is also an “fg” parameter which sets the foreground color.

# 仔细学习和实践demo
# demo(graphics)

require(datasets)
require(grDevices); require(graphics)

x <- stats::rnorm(50) # 数据

opar <- par(bg = "white")

plot(x, ann = FALSE, type = "n")

abline(h = 0, col = gray(.90))

lines(x, col = "green4", lty = "dotted")

points(x, bg = "limegreen", pch = 21)

title(main = "Simple Use of Color In a Plot",
       xlab = "Just a Whisper of a Label",
       col.main = "blue", col.lab = gray(.8),
       cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)

A little color wheel. This code just plots equally spaced hues in a pie chart. If you have a cheap SVGA monitor (like me) you will probably find that numerically equispaced does not mean visually equispaced. On my display at home, these colors tend to cluster at the RGB primaries. On the other hand on the SGI Indy at work the

## effect is near perfect.
 
par(bg = "gray")

pie(rep(1,24), col = rainbow(24), radius = 0.95)

title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)

title(xlab = "(Use this as a test of monitor linearity)",
       cex.lab = 0.8,
       font.lab = 3)

## We have already confessed to having these.  This is just showing off X11 color names (and the example (from the postscript manual) is pretty "cute".

pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)

names(pie.sales) <- c("Blueberry", "Cherry",
              "Apple", "Boston Cream", "Other", "Vanilla Cream")

pie(pie.sales,
     col = c("purple","violetred1","green3","cornsilk","cyan","white"))

title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)

title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3)

Boxplots: I couldn’t resist the capability for filling the “box”.The use of color seems like a useful addition, it focuses attention on the central bulk of the data.

par(bg="cornsilk")

n <- 10

g <- gl(n, 100, n*100)

x <- rnorm(n*100) + sqrt(as.numeric(g))

boxplot(split(x,g), col="lavender", notch=TRUE)

title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)

## An example showing how to fill between curves.

par(bg="white")

n <- 100

x <- c(0,cumsum(rnorm(n)))

y <- c(0,cumsum(rnorm(n)))

xx <- c(0:n, n:0)

yy <- c(x, rev(y))

plot(xx, yy, type="n", xlab="Time", ylab="Distance")

polygon(xx, yy, col="gray")

title("Distance Between Brownian Motions")

Colored plot margins, axis labels and titles. You do need to be careful with these kinds of effects. It’s easy to go completely over the top and you can end up with your lunch all over the keyboard.On the other hand, my market research clients love it.

x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02)

par(bg="lightgray")

plot(x, type="n", axes=FALSE, ann=FALSE)

usr <- par("usr")

rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")

lines(x, col="blue")

points(x, pch=21, bg="lightcyan", cex=1.25)

axis(2, col.axis="blue", las=1)

axis(1, at=1:12, lab=month.abb, col.axis="blue")

box()

title(main= "The Level of Interest in R", font.main=4, col.main="red")

title(xlab= "1996", col.lab="red")

A filled histogram, showing how to change the font used for the main title without changing the other annotation.

par(bg="cornsilk")

x <- rnorm(1000)

hist(x, xlim=range(-4, 4, x), col="lavender", main="")

title(main="1000 Normal Random Variates", font.main=3)

A scatterplot matrix

## The good old Iris data (yet again)

pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19)

pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21,
       bg = c("red", "green3", "blue")[unclass(iris$Species)])

## Contour plotting This produces a topographic map of one of Auckland's many volcanic "peaks".

x <- 10*1:nrow(volcano)

y <- 10*1:ncol(volcano)

lev <- pretty(range(volcano), 10)

par(bg = "lightcyan")

pin <- par("pin")

xdelta <- diff(range(x))

ydelta <- diff(range(y))

xscale <- pin[1]/xdelta

yscale <- pin[2]/ydelta

scale <- min(xscale, yscale)

xadd <- 0.5*(pin[1]/scale - xdelta)

yadd <- 0.5*(pin[2]/scale - ydelta)

plot(numeric(0), numeric(0),
      xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
      type = "n", ann = FALSE)

usr <- par("usr")

rect(usr[1], usr[3], usr[2], usr[4], col="green3")

contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE)

box()

title("A Topographic Map of Maunga Whau", font= 4)

title(xlab = "Meters North", ylab = "Meters West", font= 3)

mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
       at = mean(par("usr")[1:2]), cex=0.7, font=3)

## Conditioning plots

par(bg="cornsilk")

coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")

par(opar)

实践7(复数运算和求函数极值)

2+4i
## [1] 2+4i
2+4i-6i
## [1] 2-2i
(2+4i)/4i
## [1] 1-0.5i
# 构造一个10维复向量
(z <- complex(real = rnorm(10),imaginary = rnorm(10)))
##  [1] -1.9345231-1.1796093i -0.3199740+0.7578919i -0.3301140-0.7206507i
##  [4]  0.7011787-1.0638143i -2.1542085-1.5763669i -0.2570409-0.8030955i
##  [7] -0.1321132+0.9697036i -1.1736460-0.5693483i  1.3367468-0.1874468i
## [10]  1.2776321-0.8104987i
Re(z)
##  [1] -1.9345231 -0.3199740 -0.3301140  0.7011787 -2.1542085 -0.2570409
##  [7] -0.1321132 -1.1736460  1.3367468  1.2776321
Im(z)
##  [1] -1.1796093  0.7578919 -0.7206507 -1.0638143 -1.5763669 -0.8030955
##  [7]  0.9697036 -0.5693483 -0.1874468 -0.8104987
Mod(z)
##  [1] 2.2658017 0.8226686 0.7926618 1.2741085 2.6693720 0.8432273 0.9786618
##  [8] 1.3044548 1.3498253 1.5130274
Arg(z) # 辅角
##  [1] -2.5940221  1.9702840 -2.0003473 -0.9880382 -2.5098667 -1.8805562
##  [7]  1.7062035 -2.6899272 -0.1393177 -0.5653129
choose(3,2) # 组合
## [1] 3
factorial(10)
## [1] 3628800
# 解方程
f <- function(x){
  x^3 + 2*x + 1
}
uniroot(f,c(-2,2))
## $root
## [1] -0.4533812
## 
## $f.root
## [1] 4.302946e-05
## 
## $iter
## [1] 8
## 
## $init.it
## [1] NA
## 
## $estim.prec
## [1] 6.103516e-05

实践8(字符型向量)

a <- factor(letters[1:10])
# a[3] <- "w" 
a %>% as.numeric()
##  [1]  1  2  3  4  5  6  7  8  9 10

实践9(数据输入与输出)

# x <- scan()

y <- iris
y %>% str()
## '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 ...
write.table(y,file = "iris.txt")
read.table("iris.txt")
##     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
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica
write_csv(y,"iris.csv")
read_csv("iris.csv")
## Parsed with column specification:
## cols(
##   Sepal.Length = col_double(),
##   Sepal.Width = col_double(),
##   Petal.Length = col_double(),
##   Petal.Width = col_double(),
##   Species = col_character()
## )
## # A tibble: 150 x 5
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            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           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ... with 140 more rows
# read.table("clipboard") 读入剪贴板数据!!!

实践10(序列)

seq(1,10,length = 100)
##   [1]  1.000000  1.090909  1.181818  1.272727  1.363636  1.454545  1.545455
##   [8]  1.636364  1.727273  1.818182  1.909091  2.000000  2.090909  2.181818
##  [15]  2.272727  2.363636  2.454545  2.545455  2.636364  2.727273  2.818182
##  [22]  2.909091  3.000000  3.090909  3.181818  3.272727  3.363636  3.454545
##  [29]  3.545455  3.636364  3.727273  3.818182  3.909091  4.000000  4.090909
##  [36]  4.181818  4.272727  4.363636  4.454545  4.545455  4.636364  4.727273
##  [43]  4.818182  4.909091  5.000000  5.090909  5.181818  5.272727  5.363636
##  [50]  5.454545  5.545455  5.636364  5.727273  5.818182  5.909091  6.000000
##  [57]  6.090909  6.181818  6.272727  6.363636  6.454545  6.545455  6.636364
##  [64]  6.727273  6.818182  6.909091  7.000000  7.090909  7.181818  7.272727
##  [71]  7.363636  7.454545  7.545455  7.636364  7.727273  7.818182  7.909091
##  [78]  8.000000  8.090909  8.181818  8.272727  8.363636  8.454545  8.545455
##  [85]  8.636364  8.727273  8.818182  8.909091  9.000000  9.090909  9.181818
##  [92]  9.272727  9.363636  9.454545  9.545455  9.636364  9.727273  9.818182
##  [99]  9.909091 10.000000
seq(1,10,length.out = 100)
##   [1]  1.000000  1.090909  1.181818  1.272727  1.363636  1.454545  1.545455
##   [8]  1.636364  1.727273  1.818182  1.909091  2.000000  2.090909  2.181818
##  [15]  2.272727  2.363636  2.454545  2.545455  2.636364  2.727273  2.818182
##  [22]  2.909091  3.000000  3.090909  3.181818  3.272727  3.363636  3.454545
##  [29]  3.545455  3.636364  3.727273  3.818182  3.909091  4.000000  4.090909
##  [36]  4.181818  4.272727  4.363636  4.454545  4.545455  4.636364  4.727273
##  [43]  4.818182  4.909091  5.000000  5.090909  5.181818  5.272727  5.363636
##  [50]  5.454545  5.545455  5.636364  5.727273  5.818182  5.909091  6.000000
##  [57]  6.090909  6.181818  6.272727  6.363636  6.454545  6.545455  6.636364
##  [64]  6.727273  6.818182  6.909091  7.000000  7.090909  7.181818  7.272727
##  [71]  7.363636  7.454545  7.545455  7.636364  7.727273  7.818182  7.909091
##  [78]  8.000000  8.090909  8.181818  8.272727  8.363636  8.454545  8.545455
##  [85]  8.636364  8.727273  8.818182  8.909091  9.000000  9.090909  9.181818
##  [92]  9.272727  9.363636  9.454545  9.545455  9.636364  9.727273  9.818182
##  [99]  9.909091 10.000000
seq(10,-1,-1)
##  [1] 10  9  8  7  6  5  4  3  2  1  0 -1
rep(1:3,times = 2,each = 2)
##  [1] 1 1 2 2 3 3 1 1 2 2 3 3
rep(1:3,1:3)
## [1] 1 2 2 3 3 3
rep(c(4,5),c(4,5)) -> x
c(1:10,x) # 组合成一个向量
##  [1]  1  2  3  4  5  6  7  8  9 10  4  4  4  4  5  5  5  5  5
rev(1:10)
##  [1] 10  9  8  7  6  5  4  3  2  1
z <- letters[1:10]
z == "a"
##  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
z <- NULL
z[c(1,3,5)] <- c(1,3,5)
z
## [1]  1 NA  3 NA  5
z[-c(1:3)]
## [1] NA  5
rnorm(10)[2]
## [1] -0.1170526
which(1:10 == 10)
## [1] 10

实践11(矩阵)

# 逻辑符号的使用
(x <- sample(1:100,12))
##  [1] 92  3 93 25 16 35 45 14 96 26 27 64
all(x > 0)
## [1] TRUE
all(x!=0)
## [1] TRUE
any(x>0)
## [1] TRUE
(1:10)[x>0]
##  [1]  1  2  3  4  5  6  7  8  9 10 NA NA
diff(x);diff(x,lag = 2) # 差分
##  [1] -89  90 -68  -9  19  10 -31  82 -70   1  37
##  [1]   1  22 -77  10  29 -21  51  12 -69  38
# 矩阵的构造

(x <- matrix(x,nrow = 3,byrow = TRUE,dimnames = list(letters[1:3],LETTERS[1:4])))
##    A  B  C  D
## a 92  3 93 25
## b 16 35 45 14
## c 96 26 27 64
t(x)
##    a  b  c
## A 92 16 96
## B  3 35 26
## C 93 45 27
## D 25 14 64
z <- x%*%t(x)
z1 <- z %>% solve()
z1 %*% z # 单位阵
##               a            b            c
## a  1.000000e+00 1.110223e-16 0.000000e+00
## b -4.163336e-17 1.000000e+00 1.387779e-16
## c  0.000000e+00 0.000000e+00 1.000000e+00
solve(z,1:3) # 解方程
##             a             b             c 
## -0.0006055576  0.0010173572  0.0004246021

实践12(矩阵运算)

x;ncol(x);nrow(x);dim(x)
##    A  B  C  D
## a 92  3 93 25
## b 16 35 45 14
## c 96 26 27 64
## [1] 4
## [1] 3
## [1] 3 4
x[c(2,1),] %>% class()
## [1] "matrix"
x[,1] %>% class()
## [1] "integer"
x[1,2]
## [1] 3
# 第一列大于0的元素,两种方法
x <- matrix(rnorm(100),10)
x[x[,1]>0,1]
## [1] 0.82572866 0.06080122 0.03864979 0.51536886
x %>% as.data.frame() %>% filter(V1>0)
##           V1         V2         V3         V4         V5         V6
## 1 0.82572866  0.9226690  0.7971687 -1.2414369  0.1332955 -0.1383640
## 2 0.06080122 -2.3698740 -1.8478519 -0.6646502 -1.7985030  0.5123149
## 3 0.03864979  0.8334227 -1.5739552  0.5993034  1.3864398 -1.4113279
## 4 0.51536886 -0.7585988  1.2700295  0.1814678  0.7936110 -0.3098148
##           V7         V8         V9        V10
## 1  1.5077527 -0.6177932 -1.0978133 -0.9754438
## 2 -0.3398264 -1.7691710 -0.6621349  0.2874160
## 3 -0.3276125 -0.1484901  0.5699226 -0.2269017
## 4 -0.7273109  0.3501963 -0.6112226 -0.4958288
diag(x)
##  [1]  0.82572866  0.65706111 -0.44061484 -1.07213885  0.06931859
##  [6]  0.10825307 -0.33982642 -0.14849009  0.50518737 -0.49582879
diag(diag(x))
##            [,1]      [,2]       [,3]      [,4]       [,5]      [,6]
##  [1,] 0.8257287 0.0000000  0.0000000  0.000000 0.00000000 0.0000000
##  [2,] 0.0000000 0.6570611  0.0000000  0.000000 0.00000000 0.0000000
##  [3,] 0.0000000 0.0000000 -0.4406148  0.000000 0.00000000 0.0000000
##  [4,] 0.0000000 0.0000000  0.0000000 -1.072139 0.00000000 0.0000000
##  [5,] 0.0000000 0.0000000  0.0000000  0.000000 0.06931859 0.0000000
##  [6,] 0.0000000 0.0000000  0.0000000  0.000000 0.00000000 0.1082531
##  [7,] 0.0000000 0.0000000  0.0000000  0.000000 0.00000000 0.0000000
##  [8,] 0.0000000 0.0000000  0.0000000  0.000000 0.00000000 0.0000000
##  [9,] 0.0000000 0.0000000  0.0000000  0.000000 0.00000000 0.0000000
## [10,] 0.0000000 0.0000000  0.0000000  0.000000 0.00000000 0.0000000
##             [,7]       [,8]      [,9]      [,10]
##  [1,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [2,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [3,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [4,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [5,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [6,]  0.0000000  0.0000000 0.0000000  0.0000000
##  [7,] -0.3398264  0.0000000 0.0000000  0.0000000
##  [8,]  0.0000000 -0.1484901 0.0000000  0.0000000
##  [9,]  0.0000000  0.0000000 0.5051874  0.0000000
## [10,]  0.0000000  0.0000000 0.0000000 -0.4958288
diag(5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1
# 上三角阵(下三角阵)
x[lower.tri(x)] <- 0
x
##            [,1]      [,2]       [,3]       [,4]        [,5]       [,6]
##  [1,] 0.8257287 0.9226690  0.7971687 -1.2414369  0.13329551 -0.1383640
##  [2,] 0.0000000 0.6570611 -0.1817057  1.6865020 -0.37107637  0.4356777
##  [3,] 0.0000000 0.0000000 -0.4406148  0.7257203  0.18806940 -1.3619060
##  [4,] 0.0000000 0.0000000  0.0000000 -1.0721388 -0.83110466  2.0100132
##  [5,] 0.0000000 0.0000000  0.0000000  0.0000000  0.06931859  1.4872069
##  [6,] 0.0000000 0.0000000  0.0000000  0.0000000  0.00000000  0.1082531
##  [7,] 0.0000000 0.0000000  0.0000000  0.0000000  0.00000000  0.0000000
##  [8,] 0.0000000 0.0000000  0.0000000  0.0000000  0.00000000  0.0000000
##  [9,] 0.0000000 0.0000000  0.0000000  0.0000000  0.00000000  0.0000000
## [10,] 0.0000000 0.0000000  0.0000000  0.0000000  0.00000000  0.0000000
##             [,7]       [,8]       [,9]      [,10]
##  [1,]  1.5077527 -0.6177932 -1.0978133 -0.9754438
##  [2,]  0.2705779 -0.6128006 -1.1162094  1.2370579
##  [3,]  2.0774103 -1.4312077  1.1407166  1.3957363
##  [4,] -2.2624718 -0.7397351 -0.4495942 -0.4946008
##  [5,] -0.2175064  0.6601760 -0.6627306  2.1207408
##  [6,]  0.3569650  1.3147344  1.2117823  0.3072168
##  [7,] -0.3398264 -1.7691710 -0.6621349  0.2874160
##  [8,]  0.0000000 -0.1484901  0.5699226 -0.2269017
##  [9,]  0.0000000  0.0000000  0.5051874 -0.8652143
## [10,]  0.0000000  0.0000000  0.0000000 -0.4958288

实践13(数组)

x <- array(runif(24),c(4,3,2))
x
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5564733 0.1460640 0.2484502
## [2,] 0.6026462 0.5911484 0.2610301
## [3,] 0.5763511 0.8521872 0.3316857
## [4,] 0.4852481 0.1431292 0.2831816
## 
## , , 2
## 
##           [,1]      [,2]       [,3]
## [1,] 0.2434482 0.2892165 0.01170668
## [2,] 0.8920505 0.2316299 0.81766258
## [3,] 0.5485288 0.3982102 0.51182080
## [4,] 0.4713584 0.7291094 0.02427472
apply(x, 1, max)
## [1] 0.5564733 0.8920505 0.8521872 0.7291094
apply(x, 2, min)
## [1] 0.24344823 0.14312923 0.01170668
apply(x,3,mean)
## [1] 0.4231329 0.4307514
dim(x)
## [1] 4 3 2
x[,,1]
##           [,1]      [,2]      [,3]
## [1,] 0.5564733 0.1460640 0.2484502
## [2,] 0.6026462 0.5911484 0.2610301
## [3,] 0.5763511 0.8521872 0.3316857
## [4,] 0.4852481 0.1431292 0.2831816
x[,2,2]
## [1] 0.2892165 0.2316299 0.3982102 0.7291094
x[c(1,3),,]
## , , 1
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5564733 0.1460640 0.2484502
## [2,] 0.5763511 0.8521872 0.3316857
## 
## , , 2
## 
##           [,1]      [,2]       [,3]
## [1,] 0.2434482 0.2892165 0.01170668
## [2,] 0.5485288 0.3982102 0.51182080

实践14(矩阵与向量之间的运算)

x <- matrix(1:20,5,4)
x
##      [,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
sweep(x,1,1:5,"*") # 把向量1:5的每个元素乘以每一行
##      [,1] [,2] [,3] [,4]
## [1,]    1    6   11   16
## [2,]    4   14   24   34
## [3,]    9   24   39   54
## [4,]   16   36   56   76
## [5,]   25   50   75  100
sweep(x,1,1:5,"+")
##      [,1] [,2] [,3] [,4]
## [1,]    2    7   12   17
## [2,]    4    9   14   19
## [3,]    6   11   16   21
## [4,]    8   13   18   23
## [5,]   10   15   20   25
x*1:5
##      [,1] [,2] [,3] [,4]
## [1,]    1    6   11   16
## [2,]    4   14   24   34
## [3,]    9   24   39   54
## [4,]   16   36   56   76
## [5,]   25   50   75  100
x1 <- scale(x)             # 标准化
apply(x1, 2, mean)
## [1] 0 0 0 0
apply(x1,2,sd)
## [1] 1 1 1 1
x2 <- scale(x,scale = FALSE)  # 中心化
apply(x2,2,mean)
## [1] 0 0 0 0
apply(x2,2,sd)
## [1] 1.581139 1.581139 1.581139 1.581139
x3 <- scale(x,scale = TRUE,center = FALSE) # 观察结果
apply(x3,2,mean)
## [1] 0.8090398 0.8807710 0.8891812 0.8916793
apply(x3,2,sd)
## [1] 0.42640143 0.17407766 0.10814761 0.07832604

实践15(缺失值,数据的合并)

airquality %>% datatable()
airquality %>% str()
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
complete.cases(airquality) # 判断缺失值的行
##   [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
##  [12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [23]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE
##  [45] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
##  [67]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
##  [78]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [89]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE
## [100]  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [111]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
## [122]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [133]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [144]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
which(complete.cases(airquality) == FALSE) # 找到有缺失值的行
##  [1]   5   6  10  11  25  26  27  32  33  34  35  36  37  39  42  43  45
## [18]  46  52  53  54  55  56  57  58  59  60  61  65  72  75  83  84  96
## [35]  97  98 102 103 107 115 119 150
sum(complete.cases(airquality))  # 完整观测值得个数
## [1] 111
na.omit(airquality) %>% datatable()

附加和合并数据

x <- 1:10;x[12] = 3
x
##  [1]  1  2  3  4  5  6  7  8  9 10 NA  3
append(x,77,after = 5) # 在5之后添加77
##  [1]  1  2  3  4  5 77  6  7  8  9 10 NA  3
cbind(1:5,2:6)
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    3
## [3,]    3    4
## [4,]    4    5
## [5,]    5    6
rbind(1:5,2:6)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    2    3    4    5    6
(x <- rbind(1:5,rnorm(5),runif(5),1:5,7:11))
##           [,1]       [,2]      [,3]       [,4]       [,5]
## [1,] 1.0000000  2.0000000  3.000000  4.0000000  5.0000000
## [2,] 0.9727596 -0.6282083 -2.359541 -0.8124894 -0.4501734
## [3,] 0.8668731  0.7874362  0.910792  0.1363442  0.4790972
## [4,] 1.0000000  2.0000000  3.000000  4.0000000  5.0000000
## [5,] 7.0000000  8.0000000  9.000000 10.0000000 11.0000000
x %>% class()
## [1] "matrix"
unique(x)
##           [,1]       [,2]      [,3]       [,4]       [,5]
## [1,] 1.0000000  2.0000000  3.000000  4.0000000  5.0000000
## [2,] 0.9727596 -0.6282083 -2.359541 -0.8124894 -0.4501734
## [3,] 0.8668731  0.7874362  0.910792  0.1363442  0.4790972
## [4,] 7.0000000  8.0000000  9.000000 10.0000000 11.0000000
x %>% as.data.frame() %>% distinct()
##          V1         V2        V3         V4         V5
## 1 1.0000000  2.0000000  3.000000  4.0000000  5.0000000
## 2 0.9727596 -0.6282083 -2.359541 -0.8124894 -0.4501734
## 3 0.8668731  0.7874362  0.910792  0.1363442  0.4790972
## 4 7.0000000  8.0000000  9.000000 10.0000000 11.0000000

实践16(list)

z <- list(x = 1:3,Tom = c(1:2),a = list("R",letters[1:5],w = "Hi!") )
z$x
## [1] 1 2 3
z[[1]]  # 等价
## [1] 1 2 3
z$Tom[1]
## [1] 1
z[[2]][1] # 等价
## [1] 1
z$a$w
## [1] "Hi!"
z[[3]] %>% class()
## [1] "list"
z[[3]][1] %>% class()
## [1] "list"
z[[3]][[1]]
## [1] "R"
z[[3]][[2]][1]
## [1] "a"

实践17(条形图和表)

x <- c(3,3,3,4,1,4,2,1,3,2,5,3,1,2,5,2,3,4,2,2,5,3,1,4,2,2,4,3,5,2)
length(x)
## [1] 30
barplot(x)

barplot(table(x))  # 正确的图形

barplot(table(x)/length(x))

table(x)/length(x)
## x
##         1         2         3         4         5 
## 0.1333333 0.3000000 0.2666667 0.1666667 0.1333333

实践18(形成表格)

library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
quine %>% datatable()
table(quine$Age)
## 
## F0 F1 F2 F3 
## 27 46 40 33
table(quine$Age,quine$Sex)
##     
##       F  M
##   F0 10 17
##   F1 32 14
##   F2 19 21
##   F3 19 14
tab <- xtabs(~Sex+Age,data = quine)
tab
##    Age
## Sex F0 F1 F2 F3
##   F 10 32 19 19
##   M 17 14 21 14
tab %>% unclass()
##    Age
## Sex F0 F1 F2 F3
##   F 10 32 19 19
##   M 17 14 21 14
## attr(,"call")
## xtabs(formula = ~Sex + Age, data = quine)
tapply(quine$Days,quine$Age,mean)       # 按照Age求均值
##       F0       F1       F2       F3 
## 14.85185 11.15217 21.05000 19.60606
quine %>% group_by(Age) %>% summarise(Days_mean = mean(Days))
## # A tibble: 4 x 2
##   Age   Days_mean
##   <fct>     <dbl>
## 1 F0         14.9
## 2 F1         11.2
## 3 F2         21.0
## 4 F3         19.6
tapply(quine$Days,list(quine$Age,quine$Sex),mean) # 分组求均值
##           F        M
## F0 18.70000 12.58824
## F1 12.96875  7.00000
## F2 18.42105 23.42857
## F3 14.00000 27.21429
quine %>% group_by(Age,Sex) %>% summarise(Days_mean = mean(Days))
## # A tibble: 8 x 3
## # Groups:   Age [4]
##   Age   Sex   Days_mean
##   <fct> <fct>     <dbl>
## 1 F0    F          18.7
## 2 F0    M          12.6
## 3 F1    F          13.0
## 4 F1    M           7  
## 5 F2    F          18.4
## 6 F2    M          23.4
## 7 F3    F          14  
## 8 F3    M          27.2

实践19(如何写函数)

# 求n以内的素数
ss <- function(x){
  z = 2
  for (i in 2:x) {
    if (any(i%%2:(i-1) == 0) == FALSE){
      z = c(z,i)
    }
    
  }
  return(z)
}
t1 <- Sys.time()
ss(20000) %>% head()
## [1]  2  3  5  7 11 13
Sys.time()-t1
## Time difference of 1.635625 secs
system.time(ss(10000))  # 观察语句执行时间,函数可以不写return,此时返回最后一个return的值
##    user  system elapsed 
##    0.40    0.00    0.41

实践20(画图)

x <- seq(-3,3,length.out = 20);y <- dnorm(x)
data.frame(x,y) -> w
par(mfrow = c(2,2))
plot(y~x,data = w,main = "正态密度函数")
plot(y~x,data = w,type = "l",main = "正态密度函数")
plot(y~x,data = w,type = "o",main = "正态密度函数")
plot(y~x,data = w,type = "b",main = "正态密度函数")

par(mfrow = c(1,1))

实践21(色彩和符号等调节)

plot(1,1,xlim = c(1,7.5),ylim = c(0,5),type = "n")
# 在plot函数后追加点(points)或者线(lines)
points(1:7,rep(4.5,7),cex = seq(1,4,length.out = 7),pch = 0:6)
text(1:7,rep(3.5,7),
     labels = paste(0:6,letters[1:7]),
     cex = seq(1,4,length.out = 7),
     col = 1:7)

points(1:7,rep(2,7),pch = 0:6 + 7)
text(1:7+0.25,rep(2,7),
     labels = paste(0:6 + 7),
     col = 0:6 + 7
     )

points(1:7,rep(1,7),pch = 14:20)
text(1:7 + 0.25,rep(1,7),
     labels = paste(14:20),
     col = 14:20
     )