r语言是高级编程语言
R is an open source programming language and software environment for statistical computing and graphics. It is one of the primary languages used by data scientists and statisticians alike. It is supported by the R Foundation for Statistical Computing and a large community of open source developers. Since R utilized a command line interface, there can be a steep learning curve for some individuals who are used to using GUI focused programs such as SPSS and SAS so extensions to R such as RStudio can be highly beneficial. Since R is an open source program and freely available, there can a large attraction for academics whose access to statistical programs are regulated through their association to various colleges or universities.
R是用于统计计算和图形的开源编程语言和软件环境。 它是数据科学家和统计学家使用的主要语言之一。 它由R的统计计算基金会和一个大型的开源开发者社区提供支持。 由于R使用命令行界面,对于习惯使用GUI集中程序(例如SPSS和SAS)的某些人来说,学习曲线可能会很陡峭,因此对R的扩展(例如RStudio)可能会非常有益。 由于R是一个开放源代码计划,可免费获得,因此对于那些通过与各个学院或大学的联系而对统计程序的访问进行管理的学者,可能会产生很大的吸引力。
The first thing you need to get started with R is to download it from its official site according to your operating system.
开始使用R的第一件事是根据您的操作系统从其官方网站下载它。
RStudio is an integrated development environment (IDE) for R. It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management. RStudio是R的集成开发环境(IDE)。它包含一个控制台,语法突出显示的编辑器,支持直接执行代码,以及用于绘图,历史记录,调试和工作区管理的工具。
The Comprehensive R Archive Network (CRAN) is a leading source for R tools and resources. 综合R存档网络(CRAN)是R工具和资源的主要来源。
Tidyverse is an opinionated collection of R packages designed for data science like ggplot2, dplyr, readr, tidyr, purr, tibble. Tidyverse是为数据科学而设计的R软件包的自有集合,例如ggplot2,dplyr,readr,tidyr,purr,tibble。
data.table is an implementation of base data.frame focused on improved performance and terse, flexible syntax. data.table是基础data.frame的实现,专注于提高性能和简洁灵活的语法。
Shiny framework for building dashboard style web apps in R. 用于在R中构建仪表板样式的Web应用程序的闪亮框架。
(Data Types in R)
(Vector)
It is a sequence of data elements of the same basic type. For example:
它是相同基本类型的数据元素序列。 例如:
> o <- c(1,2,5.3,6,-2,4) # Numeric vector
> p <- c("one","two","three","four","five","six") # Character vector
> q <- c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE) # Logical vector
> o;p;q
[1] 1.0 2.0 5.3 6.0 -2.0 4.0
[1] "one" "two" "three" "four" "five" "six"
[1] TRUE TRUE FALSE TRUE FALSE
(Matrix)
It is a two-dimensional rectangular data set. The components in a matrix also must be of the same basic type like vector. For example:
它是一个二维矩形数据集。 矩阵中的成分也必须是相同的基本类型,例如向量。 例如:
> m = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
> m
>[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"
(Data Frame)
It is more general than a matrix, in that different columns can have different basic data types. For example:
它比矩阵更通用,因为不同的列可以具有不同的基本数据类型。 例如:
> d <- c(1,2,3,4)
> e <- c("red", "white", "red", NA)
> f <- c(TRUE,TRUE,TRUE,FALSE)
> mydata <- data.frame(d,e,f)
> names(mydata) <- c("ID","Color","Passed")
> mydata
(Lists)
It is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it. For example:
它是一个R对象,其中可以包含许多不同类型的元素,例如矢量,函数,甚至其中的另一个列表。 例如:
> list1 <- list(c(2,5,3),21.3,sin)
> list1
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function (x) .Primitive("sin")
(Functions in R)
A function allows you to define a reusable block of code that can be executed many times within your program.
函数允许您定义一个可重用的代码块,该代码块可以在程序中多次执行。
Functions can be named and called repeatedly or can be run anonymously in place (similar to lambda functions in python).
函数可以重复命名和调用,也可以在本地匿名运行(类似于python中的lambda函数)。
Developing full understanding of R functions requires understanding of environments. Environments are simply a way to manage objects. An example of environments in action is that you can use a redundant variable name within a function, that won’t be affected if the larger runtime already has the same variable. Additionally, if a function calls a variable not defined within the function it will check the higher level environment for that variable.
全面理解R功能需要了解环境。 环境只是管理对象的一种方式。 实际环境的一个示例是,您可以在函数内使用冗余变量名,如果较大的运行时已经具有相同的变量,则该变量名不会受到影响。 此外,如果函数调用该函数中未定义的变量,则它将检查该变量的高级环境。
句法 (Syntax)
In R, a function definition has the following features:
在R中,函数定义具有以下功能:
- The keyword function 关键字function
- a function name
- input parameters (optional)
- some block of code to execute
- a return statement (optional)
# a function with no parameters or returned values
sayHello() = function(){
"Hello!"
}
sayHello() # calls the function, 'Hello!' is printed to the console
# a function with a parameter
helloWithName = function(name){
paste0("Hello, ", name, "!")
}
helloWithName("Ada") # calls the function, 'Hello, Ada!' is printed to the console
# a function with multiple parameters with a return statement
multiply = function(val1, val2){
val1 * val2
}
multiply(3, 5) # prints 15 to the console
Functions are blocks of code that can be reused simply by calling the function. This enables simple, elegant code reuse without explicitly re-writing sections of code. This makes code both more readable, makes for easier debugging, and limits typing errors.
函数是可以简单地通过调用函数重用的代码块。 这样就可以简单,优雅地重用代码,而无需显式重写代码部分。 这使代码更易读,调试更轻松,并减少了键入错误。
Functions in R are created using the function keyword, along with a function name and function parameters inside parentheses.
R中的function是使用function关键字以及括号内的函数名称和函数参数创建的。
The return() function can be used by the function to return a value, and is typically used to force early termination of a function with a returned value. Alternatively, the function will return the final printed value.
函数可以使用return()函数来返回值,并且通常用于强制使用返回的值提前终止函数。 或者,该函数将返回最终的打印值。
# return a value explicitly or simply by printing
sum = function(a, b){
c = a + b
return(c)
}
sum = function(a, b){
a + b
}
result = sum(1, 2)
# result = 3
You can also define default values for the parameters, which R will use when a variable is not specified during function call.
您还可以定义参数的默认值,当在函数调用期间未指定变量时,R将使用该默认值。
sum = function(a, b = 3){
a + b
}
result = sum(a = 1)
# result = 4
You can also pass the parameters in the order you want, using the name of the parameter.
您还可以使用参数名称以所需顺序传递参数。
result = sum(b=2, a=2)
# result = 4
R can also accept additional, optional parameters with ’…’
R还可以接受带有“…”的其他可选参数
sum = function(a, b, ...){
a + b + ...
}
sum(1, 2, 3) #returns 6
Functions can also be run anonymously. These are very useful in combination with the ‘apply’ family of functions.
函数也可以匿名运行。 这些功能与“应用”功能系列结合使用非常有用。
# loop through 1, 2, 3 - add 1 to each
sapply(1:3,
function(i){
i + 1
})
笔记 (Notes)
If a function definition includes arguments without default values specified, values for those values must be included.
如果函数定义包含未指定默认值的参数,则必须包含这些值的值。
sum = function(a, b = 3){
a + b
}
sum(b = 2) # Error in sum(b = 2) : argument "a" is missing, with no default
Variables defined within a function only exist within the scope of that function, but will check larger environment if variable not specified
在函数中定义的变量仅存在于该函数的范围内,但如果未指定变量,则将检查较大的环境
double = function(a){
a * 2
}
double(x) # Error in double(x) : object 'x' not found
double = function(){
a * 2
}
a = 3
double() # 6
(In-built functions in R)
- R comes with many functions that you can use to do sophisticated tasks like random sampling.
- For example, you can round a number with the round(), or calculate its factorial with the factorial(). 例如,您可以使用round()将数字round() ,或者使用factorial()计算其阶乘。
> round(4.147)
[1] 4
> factorial(3)
[1] 6
> round(mean(1:6))
[1] 4
- The data that you pass into the function is called the function’s argument.
- You can simulate a roll of the die with R’s sample()function. The sample() function takes two arguments:a vector named x and a number named size. For example: 您可以使用R的sample()函数模拟模具的滚动。 sample()函数采用两个参数:一个名为x的向量和一个名为size的数字。 例如:
> sample(x = 1:4, size = 2)
[] 4 2
> sample(x = die, size = 1)
[] 3
>dice <- sample(die, size = 2, replace = TRUE)
>dice
[1] 2 4
>sum(dice)
[1] 6
- If you’re not sure which names to use with a function, you can look up the function’s arguments with args.
> args(round)
[1] function(x, digits=0)
R中的对象 (Objects in R)
R allows to save the data by storing it inside an R object.
R允许通过将数据存储在R对象中来保存数据。
(What’s an object?)
It is just a name that you can use to call up stored data. For example, you can save data into an object like a or b.
它只是一个名称,可用于调用存储的数据。 例如,您可以将数据保存到a或b这样的对象中。
> a <- 5
> a
[1] 5
(How to create an Object in R?)
- To create an R object, choose a name and then use the less-than symbol, <, followed by a minus sign, -, to save data into it. This combination looks like an arrow, <-. R will make an object, give it your name, and store in it whatever follows the arrow. 要创建R对象,请选择一个名称,然后使用小于号< ,后跟减号-将数据保存到其中。 这种组合看起来像箭头<- 。 R将创建一个对象,为其命名,并将其存储在箭头后面的任何位置。
- When you ask R what’s in a, it tells you on the next line. For example:
> die <- 1:6
> die
[1] 1 2 3 4 5 6
- You can name an object in R almost anything you want, but there are a few rules. First, a name cannot start with a number. Second, a name cannot use some special symbols, like ^, !, $, @, +, -, /, or *: 您几乎可以在R中为对象命名,但是有一些规则。 首先,名称不能以数字开头。 其次,名称不能使用某些特殊符号,例如^, !, $, @, +, -, /, or * :
- R also understands capitalization (or is case-sensitive), so name and Name will refer to different objects.
- You can see which object names you have already used with the function ls(). 您可以看到函数ls()已经使用了哪些对象名称。
(More Information:)
- Learn R programming language basics in just 2 hours with this free course on statistical programming 这项免费的统计编程课程仅需2个小时即可学习R编程语言基础知识
- An introduction to web scraping using R 使用R进行网页抓取的简介
- An introduction to aggregates in R: a powerful tool for playing with data R中聚合的简介:强大的数据处理工具
翻译自: https://www.freecodecamp.org/news/r-programming-language-explained/
r语言是高级编程语言