R language Learning-functions

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.