● Vector (X)
● Assignment
Assign (vector name, X ).
Vector name <-x
X-> vector name
● Operation
Add (+), subtract (-), multiply (*), divide (/), divide (%/%), and evaluate the remainder (%)
Exp (), log, cos, tan, SQRT...
Maximum Value: max (X)
Minimum value: min (X)
Range: range (X)
Sum: sum (X)
Product (factorial): prod (X)
Number: length (X)
Median: Media (X)
Average price: mean (X)
Variance: VAR (X)
Standard Deviation: SD (X)
Sort: Sort (X)
● Generation of Sequences
Sequence from A to B + (-) 1: A: B
Equi-interval function: seq (from = value1, To = value2, by = value3)
Repeated function: rep (x, times)
● Logical Vector
True: true (t) | false: false (f)
Operator: <, <=,>, >=, = (equal ),! = (Not equal)
● Missing Data
Na
Missing Value: Is. Na (X)
Uncertain check: Is. Nan (x), T: uncertain, F: OK
Limited: Is. Finite (x), t Limited, F Unlimited
Test infinity: Is. Infinite (x), t infinity, F finite
● Vector
String connection: paste (x, y)
● Plural vector
Generate plural: complex ()
● Object
Value: Is. Numeric (X)
Is it a character: Is. character (X)
Type conversion: As. character (x), as. Numeric (X)
Change length: length (x) <-New length. Fill or remove multiple values with null values
Attribute of the returned object: attributes (object)
Attribute of the Access Object: ATTR (object, "attribute name ")
● Factor
Encode a vector into a factor: factor ()
Level of factor (like different values): Levels ()
Frequency of various types of data: Table ()
Grouping operation function (f (x) group by index): tapply (x, index, fun = f)
Production Factor: Gl (horizontal number, repeated times, length, factor level, ordered mark)
● Array and matrix (m)
Define or obtain the dimension of the array: dim (m) <-C (Row, column ).
Directly construct multi-dimensional arrays: array (data, dim, dimnames ). Example: x <-array (, dim = C ))
Construction matrix: matrix (data, nrow, ncol, byrow, dimnames ). Example: x <-matrix (, nrow = 4, ncol = 5, byrow = false)
Transpose of the matrix: T (m ). For example, a <-matrix (, nrow = 2); A; t ();
The value of the matrix determinant: det (). Example: det (matrix (, ncol = 2 ))
Number of rows in the matrix: nrow (m)
Number of columns in the matrix: ncol (m)
Merge matrices: horizontal join cbind (); vertical join: rbind ()
Matrix straightening (changing the matrix vector): As. vector (). Example: A <-matrix (, nrow = 2); A; As. vector ();
Dimension name of the array: dimnames (m) <-List (C (...), C (....)). Example: x <-matrix (, ncol = 2, byrow = T); dimnames (x) <-List (C ("one", "two ", "three"), C ("first", "second"); X;
Data row | column name access: rownames (m) | colnames (m ).
Perform some operation on a dimension or several dimensions: Apply (m, a dimension, fun ). Example: A <-matrix (, nrow = 2); apply (A, 1, sum)
● List (l)
Structure: List (). For example: lst <-List (name = "Davy", wife = "taotao", no. children = 3, child. age = C (4, 7, 9); lst [[2]; lst [["child. age "]; lst [[4] [2]; lst $ name;
● Data box
Create a data frame: Data. Frame ().
Convert to data frame: As. Data. Frame ()
Data box to attribute: attach ()
Data box and attribute: Detach ()
● Read and write data files
Read a file in table format: Read. Table ("FILENAME" [, header = T | f,] [SEP,] [quote]...)
Files that read pure data: scan ()
Read the SPSS file: Read. SPSS ()
Read an Excel file: Read. delim ()
Read CSV file: read.csv ()
Write Data: Write ()
Write table data: Write. Table (); write.csv ()
● Read the database through ODBC:
1. Create link: conn <-odbcconnect ("odbcname", uid, PWD)
2. query data: sqlquery (Conn, "SQL ")
3. Close the link: odbcclose (conn)
● Control Flow
Branch: If (condition) {}/ else if (condition) {}/ else {}
Loop: For (name in expr_1) {expr_2} | while (condition) {expr} | repeat {expr; break}
Abort statement: Break
Null statement: Next