R and Data analysis old notes (i) Use of basic mathematical functions

Source: Internet
Author: User

    • Creating a vector matrix
> X1=c (2,3,6,8) > X2=c (1,2,3,4) > a1= (1:100) > length (A1) [1] 100> Length (x1) [1] 4> mode (x1) [1] "Numeric" > Rbind (x1,x2)   [, 1] [, 2] [, 3] [, 4]x1    2    3    6    8x2    1    2    3    4> cbind (x1,x2)     X1 x2[1,]  2  1[2,]  3  2[3,]  6  3[4,]  8  4
    • Averaging, and, multiplication, max, variance, standard deviation
> Mean (x1) [1] 4.75> sum (x1) [1] 19> max (x1) [1] 8> min (x1) [1] 2> var (x1) [1] 7.583333> prod (x1) [1] 288> SD (x1) [1] 2.753785
    • Generating vectors
> 1:10 [1] 1 2 3 4 5 6 7 8 9 10> 1:10-1 [1] 0 1 2 3 4 5 6 7 8 9> 1:10*2 [1] 2 4 6 8 10 12 14 16 18 2   0> a=2:60*2+1> A [1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41[20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79[39] 81 83 85 87 89 91 93 95 97 99 101 103 105 107 1 111 113 117[58] 119 121> a[5][1] 13> a[-5] [1] 5 7 9 11 15 17 19 21 23 25 27 29 31 33 35 3  7 39 41 43[20] 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81[39] 83 85 87 89 91 93 103 107 109 111 113 101 117 119[58] 121> a[c (2,3,8)][1] 7 9 19> a[a<20][1] 5 7 9 11 13 1   5 19> a[a[3]][1] 21> seq (6,20) [1] 6 7 8 9 ten each (20>) [1] 5 7   9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41[20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79[About] Bayi-----The 101 103 107 109 111 113, 117[58] 119 121> seq (5,121,length=10) [1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333 [8] 95.22222 108.11111 121.00000
    • New Vector
> A=c (2,3,4,2,3,2,1,4,3,2,1) > Which.max (a) [1] 3> A[which.max (a)][1] 4> which (a==2) [1]  1  4  6 10> A[which (a==2)][1] 2 2 2 2> which (a>5) integer (0) > A[which (a>5)]numeric (0) > A=1:20> A [1]  1<  C5/>2  3  4  5  6  7  8 9  Ten (a) 20> rev (a) [1] 20 19 18 17 16 15 14 13  9  8  7  6  5  4 3  2  1> a=c (2,3,4,5,6,6,7,8,3,2) > sort (a) [1 ] 2 2 3 3 4 5 6 6 7 8> Rev (sort (a)) [1] 8 7 6 6 5 4 3 3 2 2
    • Build matrix
> A1=c (1:12) > Matrix (a1,nrow=3,ncol=4)     [, 1] [, 2] [, 3] [, 4][1,]    1    4    7   10[2,]    2    5    8   11[3,]    3    6    9   12> Matrix (a1,nrow=4,ncol=3)     [, 1] [, 2] [, 3][1,]    1    5    9[2,]    2    6   10[3,]    3    7   11[4,]    4    8   12> Matrix (a1,nrow=4,ncol =3,byrow=t)     [, 1] [, 2] [, 3][1,]    1    2    3[2,]    4    5    6[3,]    7    8    9[4,]   12
    • Matrix operations
Matrix addition
> A=matrix (1:12,nrow=3,ncol=4) > t (a)     [, 1] [, 2] [, 3][1,]    1    2    3[2,]    4    5    6[3 ,]    7    8    9[4,]   ten   12> A=b=matrix (1:12,nrow=3,ncol=4) > A+b     [, 1] [, 2] [, 3] [, 4] [1,]    2    8   20[2,]    4   22[3,] 6 24> A-b     [, 1 ] [, 2] [, 3] [, 4][1,]    0    0    0    0[2,]    0    0    0    0[3,]    0    0    0    0
Matrix multiplication
> A=matrix (1:12,nrow=3,ncol=4) > B=matrix (1:12,nrow=4,ncol=3) > A%*%b     [, 1] [, 2] [, 3][1,]  158  246[2,]  184  288[3,]  330> a=matrix (1:16,nrow=4,ncol=4) > A     [, 1] [ , 2] [, 3] [, 4][1,]    1    5    9   13[2,]    2    6   14[3,]    3    7   15[4,]    4    8   16> diag (a) [1]  1  6 16> diag (Diag (a))     [, 1] [, 2] [, 3] [, 4][1, ]    1    0    0    0[2,]    0    6    0    0[3,]    0    0    0[4,]    0    0    0   16> diag (4)     [, 1] [, 2] [, 3] [, 4][1,]    1    0    0    0[2,]    0    1    0    0[3,]    0    0    1    0[4,]    0    0    0    1
Matrix inversion
> A=matrix (rnorm) > A             [, 1]        [, 2]       [, 3]       [, 4][1,] -1.604650746-2.22482987  1.5094439  1.0070701[2,]  0.006409861-0.01506928-0.6651050-1.9342548[3,] -1.606959408-0.49430092-0.9376593  0.1979031[4,]  0.422441416-0.33201336  0.3848287  1.1256368> Solve (a)           [, 1]       [, 2]       [, 3 ]       [, 4][1,] -0.1426715  0.5944611-0.1676185  1.1786143[2,] -0.1804919-0.9604913-0.2055298-1.4528592[ 3,]  0.3168603-0.5776493-0.6252734-1.1661647[4,] -0.1080209-0.3089139  0.2160497  0.4162172
Solving systems of linear equations
> A=matrix (Rnorm (16), BIS)
> A
[, 1]       [, 2]       [, 3] [, 4]
[1,] -1.604650746-2.22482987 1.5094439 1.0070701
[2,] 0.006409861-0.01506928-0.6651050-1.9342548
[3,] -1.606959408-0.49430092-0.9376593 0.1979031
[4,] 0.422441416-0.33201336 0.3848287 1.1256368
> Solve (a)
[, 1]       [, 2]       [, 3] [, 4]
[1,]-0.1426715 0.5944611-0.1676185 1.1786143
[2,] -0.1804919-0.9604913-0.2055298-1.4528592
[3,] 0.3168603-0.5776493-0.6252734-1.1661647
[4,] -0.1080209-0.3089139 0.2160497 0.4162172
> A=matrix (Rnorm (16), BIS)
> A
[, 1]        [, 2]        [, 3] [, 4]
[1,] 1.0451867-0.2426553-0.51232551-0.12062549
[2,] -1.5518006-0.1333096 0.03677731-0.10715366
[3,] -1.0620249-1.3160312 0.01713207 0.09320016
[4,]-0.6664664 2.2398778 1.94861889 0.01788447
> B=c (1:4)
> b
[1] 1 2 3 4
> Solve (A, B)
[1] 0.9840158-4.6924392 8.0064010-24.3295023
Eigenvalues and eigenvectors of matrices
> A=diag (4) +1> a     [, 1] [, 2] [, 3] [, 4][1,]    2    1    1    1[2,]    1    2    1    1[3,]    1    1    2    1[4,]    1    1    1    2> A.e=eigen (a,symmetric=t) > A.e$values[1] 5 1 1 1$vectors     [, 1]       [, 2]       [, 3]       [, 4] [1,] -0.5  0.8660254  0.0000000  0.0000000[2,] -0.5-0.2886751-0.5773503-0.5773503[3,] -0.5-0.2886751- 0.2113249  0.7886751[4,] -0.5-0.2886751  0.7886751-0.2113249> a.e$vectors%*%diag (a.e$values)%*%t (a.e$ Vectors)     [, 1] [, 2] [, 3] [, 4][1,]    2    1    1    1[2,]    1    2    1    1[3,]    1    1    2    1[4,]    1    1    1    2
    • Data frame
> X1=c (10,13,14,23,43) > X2=c (12,35,35,67,54) > X=data.frame (x1,x2) > x  x1 x21 10 122 13 353 14 354 23 675 4 3 54> Plot (x) #散点图

    • Read text file
(X=read.table ("Abc.txt")) #读剪贴板y =read.table ("clipboard", Header=f) yz=read.table ("clipboard", header=t) Z
    • Looping statements
For statement
> for (i-in 1:59) {a[i]=1*2+3}> a [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5[39] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5> > b=0> for (i in 1:59) {a[i]=i*2+3;b[i]=i*5-4}> b [1]   1   6
   11-----  -  Bayi  91[20]  96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176 181 186[39] 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266 271 276 281[58] 286 291
While statement
A[1]=5> i=1> while (a[i]<121) {i=i+1;a[i]=a[i-1]+2}> A [1]   5   7   9  17 43 of the above-the-  41[20]  All in all, a  .  79[39-------  97  99 101 103 105 107 109 111 113 115 117[58] 119 121
    • R Script Reference



Source () print ()

R and Data analysis old notes (i) Use of basic mathematical functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.