1. Simple arithmetic operations and vector operations
Command line start R, Exit Q (). You can save project progress on exit, and you can resume progress when you start R in the same directory.
Get function Help: Solve or Solve, Help.start () Start HTML Help
For an example of using a command, you can use Example (topic) to view
R case sensitive, commands can be separated, you can use ({}) to construct composite expressions, #开始到句尾为注释
If a batch of commands is saved in commands. R, you can use the source ("commands. R ") execution
Sink ("Log.txt") imports the output into a file, sink () outputs the output back to the console
R created and controlled entities are called objects, objects are created and saved by name. LS () and objects () are used to display all objects, RM (object) To delete objects
Assignments can be made using <-,= or both->, or with assign ("X", C (All)). C () to construct a vector of arbitrary parameter formation
Y=c (x,0,x) can form a new vector. 1/x can show the reciprocal of each element. Using vectors in an arithmetic expression causes each element to perform a vector operation
+-*/log (), exp (), sin (), cos (), tan (), sqrt (), Max (), Min (), mean (), sum (), VAR (computed variance), length (). Sort (Ascending)
Seq (2,10) is equivalent to 2:10. SEQ has more parameter seq (from=a,to=b,by=c,length=d) parameter order variable, by IS step.
Rep can duplicate Object Rep (C (1:3), times=2) get (1,2,3,1,2,3), Rep (C (1:3), each=2) get (1,1,2,2,3,3)
Logical vector: TRUE, FALSE, NA. Logical operation: <,<=,>,>=,==,!=. & Intersection operations with, | and set operations OR,! Non-arithmetic
Any operation that contains NA data is na,is.na (x) returns a vector equal to x length, the position of Na in X is true, and the rest is false
Nan is a defect value such as 0/0,na is not a given value
Paste () can have any number of arguments, putting them one by one into strings, such as paste ("X", "Y"), 1:6,sep= "") To Get ("1x", "2y", "3x", "4y", "5x", "6y")
A subset of a vector can be obtained by adding an index vector in []:
1. Logical vectors: The index vectors need to be consistent with the length of the logical vectors at this time. X[!is.na (x)] takes all non-NA elements as subsets, (x+1) [(!is.na (x)) & X>0] Adds 1 to all elements of X and generates a subset with no NA and negative numbers
2. A positive integer vector. The element corresponding to the index vector is selected, with the same length as the index vector, such as x[1:10]. The index vector length can be longer than the original vector, such as C ("X", "Y") [Rep (c (1,2,2,1), times=4)] will generate "X", "Y", "Y", "X" repeats four times
3. Negative integer vectors: Specify element exclusions, such as (x[-(1:5)]) excluding element 1-5
4. String vector: Used only to identify elements with the name attribute. such as: Fruit=c (1,2,3,4), names (fruit) =c ("A", "B", "C", "D"); Num=fruit[c ("A", "B")]
Vectors are the most important in R, and there are other objects such as matrices matrix, factor factor, List of lists, data frame, and function functions.
2. Objects and their patterns and properties
The types of objects in R include the numeric "numeric", the plural type "complex", the logical "logical", the character "character" and the original "raw". Mode (object) can get the object type. As.character (z), As.integer (z) can convert the object type, Class (object) to see what classes the object belongs to
Use Length (alpha) =3 to change the alpha to 3. E=numeric (), E is an empty object, e[3]=10 expands the e length to 3, and the first two elements are NA.
3. Order factor and Disorder factor
A factor (factor) is used to store categorical variables and ordered variables, which cannot be used for calculations, but only for classification and counting. Example:
Colour <-C (' G ', ' g ', ' r ', ' Y ', ' g ', ' y ', ' y ', ' r ', ' y ') Col <-factor (colour) col1 <-factor (colour, levels = C (' G ' R ', ' Y '), labels = c (' Green ', ' Red ', ' Yellow ')) #labels的内容替换colour相应位置对应levels的内容col2 <-factor (colour, levels = C ( ' G ', ' R ', ' Y '), labels = c (' 1 ', ' 2 ', ' 3 ')) Col_vec <-as.vector (col2) #转换成字符向量col_num <-as.numeric (col2) #转换成数字向量col 3 <-factor (colour, levels = C (' G ', ' R ')); col3 output is in the Y place for <NA>
4. Arrays and matrices
R starting from scratch, simple API collection