R Language Learning Notes: Basics

Source: Internet
Author: User

1. Data Analysis Pyramid

2. "File"-"Change Working directory"

3. "Package"-"Set Cran mirror"

"Package"-"Install package"

4. Vector C ()

Example: X=c (2,5,8,3,5,9)

Example: X=c (1:100) shows that all 1-100 of the numbers are given to the X variable

5. View the type of x: >mode (x)

6. See Length of x: >length (x)

7. A matrix of two vectors is formed:

>rbind (x1, x2) Note: R is the meaning of row, and a matrix is formed by row.

>cbind (x1, x2) Note C is the meaning of column, that is, columns, which form the matrix.

8. Averaging: >mean (x) Note: R language Case Sensitive

Averaging columns: Colmeans (x)

Apply (x, 2, mean)-averages the direction of X in the column, and if 2 changes to 1, the row is averaged. Very powerful.

Summation: >sum (x)

Minimum value: >max (x)

Maximum value: >min (x)

Variance: >var (x) Action: When the reaction data is discrete, the greater the variance, the greater the discrete situation.

Standard deviation: >SD (x) Action: Similar to the variance reaction

Connect by: >prod (x)

9. Help: >help (mode) or? mode

10, equal sign: The function of <-equals =, is the meaning of assignment.

11, the generation of vectors

1) forming a linear vector: >1:10

2) Each of the vectors that are formed is reduced 1:>1:10-1

3) The vectors that are formed are multiplied by 2:>1:10*2.

4) The resulting vector is multiplied by 2 and then 1:>1:10-2+1.

5) Display the nth element of vector x: >x[n]

6) vector x is not displayed except for nth elements, others are displayed: >x[-n]

7) Display the 1th to 5th element in vector x: >x[1:5]

8) vector x except for the 1th to 5th element is not displayed, others are displayed: >x[-(1:5)]

9) display the elements less than 20 in vector x: >x[x<20]

12. Form a vector according to the specified law: >seq (5, 20)-form a vector from 5 to 20, with a step of 1.

>seq (5, 2, by=2)--Growth in step size: >seq (5, 20, 2)

>seq (5, 10, length=10)-a vector from 2 to 200 is formed.

13. A vector of 26 letters: >LETTERS[1:30]

14. which () function

>x= (2,4,5,6,8,4,5,8,6,9,0,7,1)

>which.max (x)-Gets the subscript of the largest value in vector x.

>x[which.max (x)]-This can take out the maximum correction in vector x.

>which (x==2)-takes out the subscript of an element with a value of 2 in vector x.

>which (x>5)

15. Reverse the vector: >rev (x)

16, Ascending: >sort (x)

De-sequencing: >rev (sort (x))

17. Function of generating matrix: >matrix ()

>x= (1:20)

>matrix (x, nrow=4, ncol=5)--Generates a matrix of 4 rows and 5 columns by the vector x, which is generated by column by default.

>matrix (x, nrow=4, ncol=5, byrow=t)-generates a 4-row, 5-column matrix of Vector x, generated by rows.

18, matrix column conversion function: >t ()

Example: Matrix x 4 row 5 column

>t (x)--x into 5 rows and 4 columns

When a matrix is added and minus, two matrices have the same number of rows.

19, matrix multiplication: >x%*%y

20, to find the diagonal of the Phalanx: >diag (x)--x is a matrix, square.

Generates a matrix with a vector x diagonal: >diag (x)--x is a vector

Formation of a 4-order unit matrix: >diag (4)

21. A vector of 16 numbers is formed by normal state step: >rnorm (16)

22, Matrix inversion: >solve (x)--the result of the operation is the solution of linear equations Ax = b, if b default, then the system defaults to the unit matrix, so it can be used for matrix inversion.

Example: >solve (2, 6) The result is: 3, note: A parameter can be a vector or a matrix.

23, the eigenvalues of the Matrix: >eigen ()

24, judge whether it is a vector: >is.vector (x)

To determine whether an array is: >is.array (x)

Determine if it is a matrix: >is.matrix (x)

25. Convert a vector into a matrix (array)

>x=c (3:8)

>dim (x) <-c (2,3)-a matrix of 2 rows and 3 columns, which is a multidimensional array.

26. Data frame and matrix

The data in the matrix is a numeric type, but the data frame is a variety of shapes. In the actual application, the data frame is more fit for us.

Rows are called observations, and columns are called variables.

Generate Data frame: >data.frame (x1, x2)--x1, X2 is two equal-length vectors, which is two columns.

27, scatter chart function: >plot (x)--x is the data frame formed above.

Plot () is a broad and profound function.

28, read the text data. The first thing to do is to set the working directory, and then put the file in that directory.

> (x=read.table ("Abc.txt"))

Text or Excel data can be manipulated through the clipboard.

>y<-read.table ("clipboard", header=f)

29, read the Excel data method one

Save first as a CSV file, then: >read.csv ("1234.csv", header=t)

30. Read Excel data Method two

First install the RODBC package, installation method see above;

Then load rodbc:>library (RODBC)

Then read the Excel file: >z<-odbcconnectexcel ("Test.xls")

Last read data: (W<-sqlfetch (Z, "Sheet1"))

31. For Loop statement

>for (i in 1:59) {a[i]=i}

>a

32. While loop statement

>while (a[i]<121) {i=i+1; a[i]=a[i-1]+2}

33. Run R script function: >source ("D:\\H.R")

Output the print () function in the script, otherwise you will not see the result. This is not the same as the console output.

34. Several common functions

Normal step function: Rnorm ()

Poisson step function: Rpois ()

Exponential step function: Rexp ()

Gamma step function: Rgamma ()

Uniform step function: runif ()

Two Step function: Rbinom

Geometric step function: Rgeom ()

Rounding function: Round ()

35. Write the data frame to a text file

>write.table (x, file= "D:\\mark.txt", Col.name=f, Row.name=f, sep= "")

36. Draw the Histogram function: hist ()

To draw a histogram for the X1 column in the X data frame: >hist (x$x1)

37, reflect the relationship between the two columns, draw the scatter plot of x1,x2 column: >plot (x$x1, x$x2)

38. List Analysis

First column analysis: >table (X$X1)

Then draw the bar chart: >barplot (table (X$X1))

39. Pie chart function: <pie ()

Example: Pie (Table (X$X1))--also advanced

40, the end of the box: >boxplot (x$x1, X$X2, x$x3)-reflects the concentration of data, and the effect of the variance is similar.

Outliers, outliers to be ruled out.

Horizontal box End diagram: >boxplot (x$x1, x$x2, x$x3, horizontal=t)

41. Astrological Chart: >stars (x[c ("x1", "X2", "x3")])

Other styles:> stars (x[c ("x1", "X2", "x3")], full=t, draw.segment=t)

42, Facebook Map: >faces (x[c ("x1", "X2", "x3")])

The first thing to install is the Aplpack package.

43, Stem Leaf diagram: >stem (x$x1)

Show how many people are in each interval.

44. QQ Map

>qqnorm (x1)

>qqline (x1)

Used to determine whether a normal step, a straight line slope, etc.

45. Scatter chart

46. Wiring Diagram

47, Draw line function: >lines (x$x1, type= "L", col= "Blue", lwd=2)--LWD: line width

48. Density function: >density ()

49. R language built-in data set: >data ()

50, Heat force diagram function: >heatmap ()

51. Scatter Atlas: >pairs ()

52. Output multiple scatter plots in the same device: >par (Mfrow=c (3,1))

53. See how many Colors: >colors ()

54. About Drawing parameters

55. Three-dimensional scatter plot

First install the Scatterplot3d package

>scatterplot3d (X[2:4])

56, harmonic curve, the purpose is to be used for clustering judgment is very convenient

>source ("D:\\unison. R ")

>unison (X[2:4])

57. Map

Install the maps package first

>map ("World", Fill=true, Col=head.colors (10))

58. Visualization of Social data

First install the maps package and the Geosphere package and load it.

Draw the map of the United States: >map ("state")

More......

59, the probability of the relevant knowledge (self-study)

Probability:

Inevitable events:

Impossible events:

Opposing events:

Mutex event:

Probability density:

Discrete step: Two-point step, two-step, poisson step

Continuous step: Uniform step, exponential step, normal step

60. Vectors can be represented visually as line segments with arrows. Arrows: Indicates the direction of the vector; Segment length: Represents the size of the vector.

61. All kinds of step functions of R language

62. Numerical characteristics of random variables

1) expectation (average)

2) Variance, which can be understood as the distance between the sample point distance and the mean point.

Standard deviation

63. General and sampling

The significance of the large number theorem and the central limit theorem

Common statistics: Sample mean, sample variance, standard deviation, majority, Min, max, percentile, median median (), percentile quantile (), up and down four percentile.

64, five sum: >fivenum (x$x1, Na.rm=true)

Median, lower four, four min, Min, and Max

65. Correlation coefficient: >cor (x$x1, x$x2), Cor.test (x$x1, x$x2)

Covariance: >cov (x$x1, x$x2)

66, one-dimensional linear regression, to resolve the forecast.

The most important thing is to find the return line.

>LM (w~1+h)--Linear model function

>summary (a)--view more information

Predictive functions:

>z=data.frame (x=185)

>priedict (A, Z)

Premise: The data must be normal step, dependent variable and independent variable must be linear relationship.

67. Establishing multivariate linear regression model

>S=LM (fertility ~., Data=swiss)--the meaning of the point is that all arguments except the dependent variable

>print (s)

>summary (s)--View model summary information

Note: If it is not suitable, it will be fixed continuously until the best. Here's how:

--stepwise regression

--Forward Introduction method

--Backward culling method

--Stepwise Screening method

>step (S, direction= "forward")--the above method does not require manual action and the formula is automatically completed.

68. Normal Step Test function: >shapiro.test ()

69, R decimation Random number:> sample (0:1, size=30, Replace=true)

R Language Learning Notes: Basics

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.