PrintLine <-function () {
Print ("--------------------------------------------");
}
#函数的调用
PrintLine ();
#错误: No parameter function, parameter call
PrintLine ("parameter");
Printnlines <-function (n) {
For (i in 1:n) {
Print ("--------------------------------------------");
}
}
#错误: Parameter function, no parameter call
Printnlines ()
Printnlines (3)
Printnlines <-Function (n=1) {
For (i in 1:n) {
Print ("--------------------------------------------");
}
}
#正确: Parameter function, because there is a default value, can be called without a parameter
Printnlines ()
#也可以有参数调用
Printnlines (3)
Printinfo <-function (name, age) {
Print (Paste ("name is:", "Name,", ' Age ': ", Age, Sep=" "))
}
Printinfo ("KEN", 18)
Printinfo (age=18, name= "KEN")
... Represents an unknown number of arguments later, you can use names () to get each name of the list, and then get the value by List[key] Way
Printinfo <-function (name, age, ...) {
Print (Paste ("name is:", "Name,", ' Age ' is: ', age, Sep= ""));
Otherparames = List (...);
For (Key in Names (Otherparames)) {
Print (Paste (key, "is:", Otherparames[key], sep= ""));
}
}
Printinfo ("KEN", height= "178CM", weight= "75KG", sex= "male");
Summary is the statistical information that gets the data, including: Min. 1st Qu. Median Mean 3rd Qu. Max. Minimum, maximum, four, default summary is no variance and standard deviation, can be enhanced by the following method, in which the value of the variance and standard deviation
Data <-read.csv ("Data.csv", sep= ",", fileencoding= ' UTF8 ');
Summary (data[,3])
#summary加强版
Summaryex <-function (column) {
S <-Summary (column)
#方差
S[[' var '] <-var (column);
#标准差
s[[' SD ']] <-SD (column);
#s #直接写个s也可以替代下面的语句
return (s);
}
Summaryex (data[, 3])
Output:
Min. 1st Qu. Median Mean 3rd Qu. Max. Var
96.00 115.00 120.00 121.08 131.00 140.00 154.91
Sd
12.45
R language Learning-functions