一些R命令(1)

> x=1:100
> sample(x,20)
 [1] 24 10 15 26 77 42 49 36 57 44 55 59  8 76
[15] 72 27 84 41 47 94
> set.seed(0)
> sample(1:10,3)
[1]  9  3 10
> z=sample(1:200000,10000)
> z[1:10]
 [1] 114571 181641  40336 179676 188932 132157
 [7] 125820  12357  41194  35310
> y=c(1,3,7,3,4,2)
> z[y]
[1] 114571  40336 125820  40336 179676 181641
> (z=sample(x,100,rep=T))
  [1] 74 12  5 14 89 84 87  4 11 55 11 73 73 79 62 51
 [17] 46  4 21 35 55 77 43 69 53 80 91 44 34 92 21 82
 [33] 31 58 39 28 27 96  3  9 93 90 92 72 69 56 41 87
 [49]  8 29 17 77 12 17 48  1 98 12  9 33 12 99 41 25
 [65] 36 16 29 26 16  3 82 58 58 71  7 51 39 43 55 65
 [81] 80 85  8 87 96 93 63 38 49 11 20 22 21  3 24 11
 [97] 28 70 27 67
> (z=sample(x,2,rep=T))
[1] 22  1
> z1=unique(z)
> length(z1)
[1] 2
> xz=setdiff(x,z)
> sort(union(xz,z))
  [1]   1   2   3   4   5   6   7   8   9  10  11  12
 [13]  13  14  15  16  17  18  19  20  21  22  23  24
 [25]  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60
 [61]  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84
 [85]  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100
> setequal(union(xz,z),x)
[1] TRUE
> intersect(1:10,7:50)
[1]  7  8  9 10
> sample(1:100,20,prob = 1:100)
 [1]  95  84  74  54 100  20  43  29  82  81  92  19
[13]  87  98  96  25  97  41  52  72
> savehistory(file = ".Rhistory")
> savehistory(file = ".Rhistory")
> pi*10^2
[1] 314.1593
> pi*(1:10)^-2.3
 [1] 3.14159265 0.63794154 0.25105622 0.12954239
 [5] 0.07753876 0.05098025 0.03576221 0.02630528
 [9] 0.02006283 0.01574526
> pi*(1:10)
 [1]  3.141593  6.283185  9.424778 12.566371
 [5] 15.707963 18.849556 21.991149 25.132741
 [9] 28.274334 31.415927
> (pi*(1:10))^(-2.3)
 [1] 0.0718712656 0.0145944019 0.0057434971
 [4] 0.0029635845 0.0017738802 0.0011662923
 [7] 0.0008181441 0.0006017947 0.0004589840
[10] 0.0003602096
> x
  [1]   1   2   3   4   5   6   7   8   9  10  11  12
 [13]  13  14  15  16  17  18  19  20  21  22  23  24
 [25]  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60
 [61]  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84
 [85]  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100
> pi^(1:5)
[1]   3.141593   9.869604  31.006277  97.409091
[5] 306.019685
> print(x,digits = 12)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12
 [13]  13  14  15  16  17  18  19  20  21  22  23  24
 [25]  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60
 [61]  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84
 [85]  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100
> x=pi*(1:5)
> print(x,digits = 12)
[1]  3.14159265359  6.28318530718  9.42477796077
[4] 12.56637061436 15.70796326795
> x=pi*10^2
> x
[1] 314.1593
> print(x,digits = x)
Error in print.default(x, digits = x) : invalid 'digits' argument
> print(x,digits = 20)
[1] 314.15926535897933
> print(x,digits = 200)
Error in print.default(x, digits = 200) : invalid 'digits' argument
> print(x,digits = 50)
Error in print.default(x, digits = 50) : invalid 'digits' argument
> print(x,digits = 30)
Error in print.default(x, digits = 30) : invalid 'digits' argument
> print(x,digits = 20)
[1] 314.15926535897933
> class(x)
[1] "numeric"
> typeof(x)
[1] "double"
> calss(cars)
Error in calss(cars) : could not find function "calss"
> class(cars)
[1] "data.frame"
> typeof(cars)
[1] "list"
> names(cars)
[1] "speed" "dist" 
> summary(cars)
     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  
> head(cars)
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10
> tail(cars)
   speed dist
45    23   54
46    24   70
47    24   92
48    24   93
49    24  120
50    25   85
> cars
   speed dist
1      4    2
2      4   10
3      7    4
4      7   22
5      8   16
6      9   10
7     10   18
8     10   26
9     10   34
10    11   17
11    11   28
12    12   14
13    12   20
14    12   24
15    12   28
16    13   26
17    13   34
18    13   34
19    13   46
20    14   26
21    14   36
22    14   60
23    14   80
24    15   20
25    15   26
26    15   54
27    16   32
28    16   40
29    17   32
30    17   40
31    17   50
32    18   42
33    18   56
34    18   76
35    18   84
36    19   36
37    19   46
38    19   68
39    20   32
40    20   48
41    20   52
42    20   56
43    20   64
44    22   66
45    23   54
46    24   70
47    24   92
48    24   93
49    24  120
50    25   85
> str(cars)
'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 ...
> row.names(cars)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9" 
[10] "10" "11" "12" "13" "14" "15" "16" "17" "18"
[19] "19" "20" "21" "22" "23" "24" "25" "26" "27"
[28] "28" "29" "30" "31" "32" "33" "34" "35" "36"
[37] "37" "38" "39" "40" "41" "42" "43" "44" "45"
[46] "46" "47" "48" "49" "50"
> col.names(cars)
Error in col.names(cars) : could not find function "col.names"
> attributes(cars)
$`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] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
[33] 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
[49] 49 50

> calss(dist~speed)
Error in calss(dist ~ speed) : could not find function "calss"
> class(dist~speed)
[1] "formula"
> plot(dist~speed,cars)
> plot(dist~speed,cars)
> plot(speed~dist,cars)
> plot(dist~speed,cars)
> plot(cars$speed,cars$dist)
> plot(dist~speed,cars)
> plot(cars$speed,cars$dist)
> ncol(cars)
[1] 2
> nrow(cars)
[1] 50
> dim(cars)
[1] 50  2
> lm(dist~speed,data=cars)

Call:
lm(formula = dist ~ speed, data = cars)

Coefficients:
(Intercept)        speed  
    -17.579        3.932  

> cars$qspeed=cut(cars$speed,breaks=quantile(cars$speed),include.lowest = TRUE)
> View(cars)
> View(cars)
> names(carsa)
Error: object 'carsa' not found
> names(cars)
[1] "speed"  "dist"   "qspeed"
> cars[3]
    qspeed
1   [4,12]
2   [4,12]
3   [4,12]
4   [4,12]
5   [4,12]
6   [4,12]
7   [4,12]
8   [4,12]
9   [4,12]
10  [4,12]
11  [4,12]
12  [4,12]
13  [4,12]
14  [4,12]
15  [4,12]
16 (12,15]
17 (12,15]
18 (12,15]
19 (12,15]
20 (12,15]
21 (12,15]
22 (12,15]
23 (12,15]
24 (12,15]
25 (12,15]
26 (12,15]
27 (15,19]
28 (15,19]
29 (15,19]
30 (15,19]
31 (15,19]
32 (15,19]
33 (15,19]
34 (15,19]
35 (15,19]
36 (15,19]
37 (15,19]
38 (15,19]
39 (19,25]
40 (19,25]
41 (19,25]
42 (19,25]
43 (19,25]
44 (19,25]
45 (19,25]
46 (19,25]
47 (19,25]
48 (19,25]
49 (19,25]
50 (19,25]
> table(cars[3])

 [4,12] (12,15] (15,19] (19,25] 
     15      11      12      12 
> is.factor(car$qspeed)
Error in is.factor(car$qspeed) : object 'car' not found
> is.factor(cars$qspeed)
[1] TRUE
> plot(dist~qspeed,data=cars)
> a=lm(dist ~ qspeed,data=cars)
> View(a)
> View(a)
> summary(a)

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
(Intercept)     18.200      4.551   3.999
qspeed(12,15]   21.982      6.996   3.142
qspeed(15,19]   31.967      6.826   4.683
qspeed(19,25]   51.133      6.826   7.491
              Pr(>|t|)    
(Intercept)   0.000228 ***
qspeed(12,15] 0.002933 ** 
qspeed(15,19] 2.52e-05 ***
qspeed(19,25] 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

> x<-round(runif(20,0,20),dists=2)
Error in round(runif(20, 0, 20), dists = 2) : unused argument (dists = 2)
> x<-round(runif(20,0,20),digits=2)
> summary(x)
   Min. 1st Qu.  Median    Mean 3rd Qu. 
  1.820   6.317  11.415  10.808  15.588 
   Max. 
 19.510 
> min(x)
[1] 1.82
> max(x)
[1] 19.51
> helo(runif())
Error in helo(runif()) : could not find function "helo"
> helo(runif)
Error in helo(runif) : could not find function "helo"
> helo runif
Error: unexpected symbol in "helo runif"
> median(x)
[1] 11.415
> mean(x)
[1] 10.808
> var(x)
[1] 29.09153
> sd(x)
[1] 5.393657
> sqrt(var(x))
[1] 5.393657
> rank(x)
 [1] 18  7  5 10 11 19 20 14  1 15  9 12  2
[14]  4 16  6  8  3 13 17
> order(x)
 [1]  9 13 18 14  3 16  2 17 11  4  5 12 19
[14]  8 10 15 20  1  6  7
> order(x,decreasing = T)
 [1]  7  6  1 20 15 10  8 19 12  5  4 11 17
[14]  2 16  3 14 18 13  9
> x[order(x)]
 [1]  1.82  1.91  2.68  5.84  6.07  6.40
 [7]  7.81  9.79 10.35 10.68 12.15 13.05
[13] 13.07 13.53 15.56 15.67 15.68 16.70
[19] 17.89 19.51
> sum(x)
[1] 216.16
> length(x)
[1] 20
> round(x)
 [1] 17  8  6 11 12 18 20 14  2 16 10 13  2
[14]  6 16  6 10  3 13 16
> fivenum(x)
[1]  1.820  6.235 11.415 15.615 19.510
> x
 [1] 16.70  7.81  6.07 10.68 12.15 17.89
 [7] 19.51 13.53  1.82 15.56 10.35 13.05
[13]  1.91  5.84 15.67  6.40  9.79  2.68
[19] 13.07 15.68
> sort(X)
Error in sort(X) : object 'X' not found
> SORT
Error: object 'SORT' not found
> sort(x)
 [1]  1.82  1.91  2.68  5.84  6.07  6.40
 [7]  7.81  9.79 10.35 10.68 12.15 13.05
[13] 13.07 13.53 15.56 15.67 15.68 16.70
[19] 17.89 19.51
> quantile(x)
     0%     25%     50%     75%    100% 
 1.8200  6.3175 11.4150 15.5875 19.5100 
> quantile(x,c(0,.33,.66,1))
     0%     33%     66%    100% 
 1.8200  8.3446 13.3184 19.5100 
> mad(x)
[1] 6.315876
> mad(x) #median average distance
[1] 6.315876
>  x=[1:10]
Error: unexpected '[' in " x=["
> x
 [1] 16.70  7.81  6.07 10.68 12.15 17.89
 [7] 19.51 13.53  1.82 15.56 10.35 13.05
[13]  1.91  5.84 15.67  6.40  9.79  2.68
[19] 13.07 15.68
> x=1:10
> x=1:10
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> mad(x)
[1] 3.7065
> mean(x)
[1] 5.5
> x-5,5
Error: unexpected ',' in "x-5,"
> x-5.5
 [1] -4.5 -3.5 -2.5 -1.5 -0.5  0.5  1.5
 [8]  2.5  3.5  4.5
> abs(x)
 [1]  1  2  3  4  5  6  7  8  9 10
> sum(abs(x-5.5))/10
[1] 2.5
> abs(x-5.5)
 [1] 4.5 3.5 2.5 1.5 0.5 0.5 1.5 2.5 3.5
[10] 4.5
> sum(abs(x-5.5))
[1] 25
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> cummax(x)
 [1]  1  2  3  4  5  6  7  8  9 10
> cummin(x)
 [1] 1 1 1 1 1 1 1 1 1 1
> cumprod(x)
 [1]       1       2       6      24
 [5]     120     720    5040   40320
 [9]  362880 3628800
> cor(x,sin(x/20))
[1] 0.999837

猜你喜欢

转载自blog.csdn.net/Feynman1999/article/details/81135613
今日推荐