5.4 Control Flow
? Statement (statement) is a separate R statement or a set of compound statements (a set of R words contained in curly braces {})
Sentences, separated by semicolons);
? A condition (cond) is an expression that is eventually parsed to true (true) or False (false);
? An expression (expr) is an evaluation statement of a numeric value or a string;
The q sequence (seq) is a numeric or string sequence.
Q 5.4.1 Repeat and cycle
Q 1.for Structure Loops repeatedly executes a statement until the value of a variable no longer contains the sequence SEQ
Q Syntax: for (var in seq) statement
Q 2.while Structure Loop executes a statement repeatedly, knowing that the condition is not true
Q Syntax: while (COND) statement
Q 5.4.2 Condition execution
Q 1.if-else Structure control structure If-else executes a statement when a given condition is true. You can also execute additional statements at the same time when the condition is false.
Q Syntax: if (COND) statement
Q If (cond) Statement1 else Statement2
Q 2.ifelse structure is a relatively compact vectorization version of the IF-ELSE structure.
Q Syntax: IfElse (cond,statement1,statement2) #cond =true execute statement1;false when executing statement2
Q 3.switch structure based on the value of an expression SELECT statement execution
Q Syntax: switch (expr, ...)
Q 5.5 User-defined functions
Q Structure: myfunction<-function (arg1,arg2,...) {
Q statements
Q return
Q}
Q Note: Once you start writing functions of any length and complexity, the importance of good debugging tools will be highlighted. There are many useful built-in debugging functions in R, and there are many user contributors that provide additional functionality. On this topic, a good reference is Duncanmurdoch "debugging in R" (Http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR).
Q 5.6 Integration and reorganization (aggregate & reshape)
Q 5.6.1 Transpose
Use the function t () to transpose a matrix or data frame. For the latter, the row name becomes the variable (column) name.
5.6.2 Consolidating data
It is easier to collapse (collapse) data using one or more by variables and a pre-defined function in R.
Call Format: Aggregate (X,by,fun)
where x is the data object to be folded, by is a list of variable names, which are removed to form a new observation,
Fun is a scalar function for calculating descriptive statistics that will be used to calculate values in new observations.
5.6.3reshape Pack
The reshape package is a great, versatile tool for refactoring and consolidating data sets.
1. Integration
The fusion of datasets is to refactor it into a format where each measurement variable is exclusively a row with the identifier variable that is required to uniquely determine the measurement. Cases:
Library (Reshape)
Md<-melt (Mydata,id= (c ("id", "" Time ")))
2. Re-casting
The cast () function reads the fused data and reshapes it with the formula you provided and an (optional) function for consolidating the data. Call format: Newdata<-cast (Md,formula,fun)
The MD is the fused data, formula describes the desired final result, and fun is the (optional) data integration function.
R in Action reading notes (2)-fifth chapter: Advanced data Management (bottom)