1. Variable transformation
As.array (x), As.data.frame (x), As.numeric (x), as.logical (x), As.complex (x), As.character (x),... Convert variable type; Use the following command to get the full list, methods (AS)
Factor (): Converts a vector into a factor
2. Variable Information
Is.na (x), Is.null (x), Is.array (x), Is.data.frame (x), Is.numeric (x), Is.complex (x), Is.character (x),... Verify the type of the variable; Use the following command to get the full list, methods (IS)
Length (x): number of elements in X
Dim (x): View the dimension of a variable, the number of dimensions that are reset, such as Dim (x) =c (3,2)
Dimnames (x): Reset the name of an object
Nrow (x): Number of rows
Ncol (x): Number of columns
Class (X): Gets or sets the classes of X; Class (X) <-c (3,2)
Unclass (x): delete Class of X
attr (X,which): Gets or sets the properties of X which
Attributes (obj): Gets or sets the list of attributes for obj
Fix,edit: Edit the Data box data in tabular form
3. Data selection and operation
Which.max (x): Returns the metric for the largest element in X
Which.min (x): Returns the index of the smallest element in X
Rev (x): Flip all elements in X
Sort (x): An ascending arrangement of the elements in X; descending order use: Rev (sort (x))
Cut (X,breaks): divides x into segments (or factors), breaks as segments or split point vectors
Match (X,y): Returns a vector that is equal to the length of x and the element in Y equals
Which (X==a): Returns a pointer to a vector x if the comparison is true (true)
Choose (n,k): Calculation of combinatorial numbers
Na.omit (x): Remove missing Value (NA) (remove related rows if x is Matrix or data box)
Na.fail (x): Returns an error message if X contains at least one NA
Unique (x): If x is a vector or a data box, returns a unique value
Table (x): Returns a table (usually used for integers or factors) that consists of the number of different values of x, i.e. frequency tables
Subset (x,...): According to the conditions (...) Select elements in x, such as x$v1<10, and if X is a data box, option Select uses the minus sign to give the reserved or removed variable subset (x, subset, select, drop = FALSE, ...)
Sample (X,size): The option replace=true allows the extraction of the size element in vector x, which is not put back.
Prop.table (x,margin=): According to margin use fractions to represent tables, wumargin when all elements and 1
* R Data creation and indexing
Operation of the * * vector
The creation of a vector
C (...)
The abbreviation for concatenate;
A common function that converts a series of arguments to vectors by recursive=true the list in descending order and combining all the elements as vectors
From:to produces a sequence
":" has a higher priority; 1:4+1 get "2,3,4,5"
SEQ () produces a vector sequence
Seq (from = 1, to = 1, by = ((To-from)/(Length.out-1)), length.out = null, Along.with = NULL, ...)
Where length.out can be abbreviated to Len.
Rep () repeats an object
Rep (X,times): X is the object to be duplicated (for example, Vector C (1,2,3)), the number of times for each element in the object repeated (such as Times=c (9,7,3) is the x vector 1 repeat 9 times, 2 repeat 7 times, 3 repeat 3 times).
In addition to the above main usage, there is a special use: Rep (x,times) Repeat x times, use Each= to repeat the x element each time, Rep (c (1,2,3), 2) Get 1 2 3 1 2 3;rep (c (1,2,3), each=2) Get 1 1 2 2 3 3.
Defining piecewise functions by using the index implementation of vectors
By combining the technique of changing some element values with the subscript of logical values, you can define piecewise functions of vectors, for example, to define
You can use the following statement to implement:
> x = C ( -2.3,4,-5,7);
> y = numeric (length (x));
> y[x<0] = 1-x[x<0];
> y[x>0] = 1-x[x>0];
> y
[1] 3.3-3.0 6.0-6.0
4.1. Arrays (including matrices, vectors) to create
C (...)
A common function that converts a series of arguments to vectors by recursive=true the list in descending order and combining all the elements as vectors
From:to produces a sequence
":" has a higher priority; 1:4+1 get "2,3,4,5"
SEQ () produces a vector sequence
Seq (from = 1, to = 1, by = ((To-from)/(Length.out-1)), length.out = null, Along.with = NULL, ...)
Where length.out can be abbreviated to Len.
Rep () repeats an object
Rep (X,times): X is the object to be duplicated (for example, Vector C (1,2,3)), the number of times for each element in the object repeated (such as Times=c (9,7,3) is the x vector 1 repeat 9 times, 2 repeat 7 times, 3 repeat 3 times).
In addition to the above main usage, there is a special use: Rep (x,times) Repeat x times, use Each= to repeat the x element each time, Rep (c (1,2,3), 2) Get 1 2 3 1 2 3;rep (c (1,2,3), each=2) Get 1 1 2 2 3 3.
Matrix (...)
Create matrix matrices (data = NA, Nrow = 1, Ncol = 1, Byrow = False,dimnames = NULL)
Array (x,dim= ...)
Produces an array of x, uses a similar dim=c (3,4,2) to specify the dimensions, and x automatically loops if x is not long enough
Data.frame (...)
Create a data box, the variable may be named or not named
Data.frame (V=1:4,ch=c ("A", "B", "C", "D"), n=10);
A relatively short vector is populated to the maximum vector length.
Note the number of elements in the data box must be the same for each property
List (...)
Create a list of variables that may be named;
List (A=c (1,2), b= "Hi", c=3i)
Factor (x,levels=) the vector x code as a factor
Factor (x = character (), levels, labels = Levels,exclude = NA, ordered = is.ordered (x))
GL () generating factor variable
GL (n,k,lenth=n*k,labels=)
The horizontal factor is generated by the specified horizontal method; K is the number of levels, n is the number of repetitions
Rbind () Cbind ()
Rbind (...) Combines a matrix, data box, or other form in a row
Cbind (...) Grouped by column, others with Rbind ()
Data cutting and separation
Vector index
X[n]: Nth element
X[-N]: Except for the nth element of X
X[1:N]: Top n elements
x[-(1:N)]: n+1 to the last element
X[c (1,4,2)]: Specifying elements
x[' name ': An element named ' name '
X[X>3]: All elements greater than 3
X[x>3 & X<5]: Elements of the interval (3,5)
X[x%in%c ("A", "and", "the")]: elements in a given group
List metrics
X[n]: List shows element n
X[n]: The nth element of the list
x[' name ': An element named ' name '
X$name: Ditto
Matrix Index
X[I,J]: The following table is an element of (I,J)
X[i,]: line I
X[,J]: Column J
X[,c (1,3)]: Columns 1th and 3
x[' name ': line named ' name '
X$name: Ditto
Data box Metrics
x[' name ': column named ' Name '
X$name: Ditto
4.2.R mathematical function
4.2.1. Generic function Apply:
Description: Lets different functions act on arrays of different edges.
Usage: Apply (X, MARGIN, FUN, ...)
Parameter description:
X is an array of arrays, of course, including matrices.
MARGIN: For the always amount, it specifies the subscript that the function is to function. For example, for a matrix, 1 represents rows, 2 represent columns, and C (1,2) represents rows and columns.
Fun functions on an array of arrays. This function can be written by itself.
Attention:
When X is a data.frame, not array, the function apply is forced to convert it to Maxtrix or array via As.matrix or As.array.
Example:
# # Compute row and column sums for a matrix:
x <-cbind (x1 = 3, x2 = C (4:1, 2:5))
Col.sums <-Apply (x, 2, sum)
Row.sums <-Apply (x, 1, sum)
4.2.2. Basic symbols:
+: Add
-: Minus
*: Multiply
/: Except
^: Power
4.2.3. Trigonometric function and other basic mathematical functions
Sin
Cos
Tan
Asin
Atan,
ATAN2,
Log
LOG10,
Exp
Max (x):
Returns the largest element in X when x is always measured.
When x is a matrix, returns the maximum value of all elements in the matrix. You can use the Apply function (apply (X, MARGIN, FUN, ...) when you want to return the maximum value of the matrix X for each row or column. , where MARGIN represents the way to act, 1 represents rows, 2 represents columns, and C (1,2) represents rows and columns; For example, apply (Data,margin=1,max)
When x is Data.frame, 1 still represents the row, and 2 still represents the column.
When x is an array, 1 still represents the row, 2 still represents the column, and the other higher dimensions are represented by the corresponding number number, such as 3 to represent the page of the array.
Other details refer to the Apply function
Min (x): Returns the smallest element in X, the same as Max
Rev (x) flips all the elements in X
Sort (x) ascending arrangement of elements in X; descending order using Rev (sort (x)), to get the small order of the arrangement ()
Choose (n,k) combined number calculation
which () returns the following table that satisfies the criteria
Sample (X,size) is not put back randomly in vector x to extract the size element option, replace+true allow the sample to be put back
Rank (): rank of the calculated vector
Range (x): Returns C (min (x), Max (x))
SUM (x): the tax of the elements in X
diff (x): Difference of vector x
Prod (x): the continuous product of elements in X
Mean (x): mean value of X
Median (x): The median of X
Quantile (x,probs=): sample number that satisfies a given probability (default is 0,0.25,0.75,1)
Weight.mean (x,w): Weighted average, W is the weight, that is, the weight value.
Rank (x): ranks of elements in X
var (x): The sample variance of vector x; if x is a matrix or a data box, the covariance matrix is computed
Cor (x): If x is a matrix or a data box, the correlation coefficient matrix will be computed
SD (x): standard deviation of x; SD (x) =sqrt (var (x))
var (x,y) or cov (x,y): covariance between x and Y, or covariance of columns of x and y if X,y is a matrix or data box
Cor (x,y): linear correlation coefficients of x and y; or correlation matrices, if x and Y are matrices or data boxes
Round (x,n): An approximate divisor of x, accurate to n-bit
Log (X,base): Calculates the base-based logarithm of x, and the default base is exp (1)
Scale (x): If x is a matrix, center and standardize the data, and if only standardize the data, use the option Center=false if only the Scale=false is centered (default Center=true,
Scale=true)
Pmin (X,y,...): x[i],y[i] form a new vector compared to the smaller ones
Pmax (X,y,...): x[i],y[i] to form a new vector compared to the larger ones
Re (x): The real part of a complex number
Im (x): Imaginary part of a complex number
ABS (x): Absolute value
ARG (x): Complex angle (in radians)
Conj (x): Conjugate complex
FFT (x): Fast Fourier transform of array x
Mvfft (x): Fourier transform for each column of matrix X
Factorial (): Calculating n!
Cumsum (): Cumulative sums
Cumprod (): Cumulative Products
D (Expression (exp (x^2)), "X"): Derivation
Integrate (function (x) x^2,0,1): integral
Note: Most mathematical functions use logical parameter Na.rm=false to specify whether to remove the missing value (NA)
4.3.R character operation
Paste (...): Convert to character after the connection vector; seq= is the dividing boundary (one space is the default); Select Collapse= can split "collapse" result
SUBSTR (x,start,stop): Extracts the child fields of the character vector; can also assign values, using substr (x,start,stop) <-value
Strsplit (x,split): Split x in Split position, for example:
X<-strsplit ("A R matrix corresponds to the mathematical concept");
Length (x[1]); #注意: Objects are stored in the first item of List x and can be extracted with x[1]
grep (pattern,x): Search for X to satisfy the pattern condition;
Gsub (PATTERN,REPLACEMENT,X): Replaces a field that satisfies a regular expression, and a sub () is similar, but replaces only the first occurrence of the field
ToLower (x): Convert to lowercase
ToUpper (x): Convert to uppercase
Match (x,table): A vector that matches the position of an X element in a table
A vector in x%in%table:table that matches the position of the X element, and the return value is a logical value
Pmatch (x,table): Partial match x element in table
NCHAR (x): Number of characters
Turn from: http://www.douban.com/note/294226439/