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是一个开放源代码计划,可免费获得,因此对于那些通过与各个学院或大学的联系而对统计程序的访问进行管理的学者,可能会产生很大的吸引力。

安装 (Installation)

The first thing you need to get started with R is to download it from its official site according to your operating system.

开始使用R的第一件事是根据您的操作系统从其官方网站下载它。

流行的R工具和软件包 (Popular R Tools and Packages)

  • 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应用程序的闪亮框架。

R中的数据类型 (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")

R中的功能 (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中,函数定义具有以下功能:

  1. The keyword function

    关键字function

  2. a function name函数名称
  3. input parameters (optional)输入参数(可选)
  4. some block of code to execute一些要执行的代码块
  5. 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 + breturn(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 founddouble = function(){
a * 2
}a = 3
double() # 6

R中的内置函数 (In-built functions in R)

  • R comes with many functions that you can use to do sophisticated tasks like random sampling.R附带了许多函数,可用于执行复杂的任务,例如随机采样。
  • 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查找函数的参数。
> 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

如何在R中创建对象? (How to create an Object in R?)

  1. 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将创建一个对象,为其命名,并将其存储在箭头后面的任何位置。

  2. When you ask R what’s in a, it tells you on the next line. For example:当您问R中的内容时,它会在下一行告诉您。 例如:
> die <- 1:6
> die
[1] 1 2 3 4 5 6
  1. 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 *

  2. R also understands capitalization (or is case-sensitive), so name and Name will refer to different objects.R还了解大小写(或区分大小写),因此name和Name将引用不同的对象。
  3. 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语言是高级编程语言

r语言是高级编程语言_R编程语言介绍相关推荐

  1. python和r语言做大数据_R和python大数据

    数据科学界华山论剑:R与Python巅峰对决 如果你是数据分析领域的新兵,那么你一定很难抉择--在进行数据分析时,到底应该使用哪个语言,R还是Python?在网络上,也经常出现诸如"我想学习 ...

  2. R语言图形用户界面数据挖掘包Rattle介绍、安装、启动、介绍(Using the rattle package for data mining)

    R语言图形用户界面数据挖掘包Rattle介绍.安装.启动.介绍(Using the rattle package for data mining) 目录

  3. r语言ggplot合并图形_R中带有ggplot2的图形

    r语言ggplot合并图形 介绍 (Introduction) R is known to be a really powerful programming language when it come ...

  4. r语言 面板数据回归_R语言——伍德里奇计量经济导论案例实践 第十三章 横截面与面板数据(一)...

    哈喽,停更了大概有三周的计量笔记又要重新开始啦!虽然美国的疫情没有停歇的迹象,可是依旧阻挡不了大学开学的热情.从8月3号开始上课到现在,也经历了很多事情,每天都是抱着死猪不怕开水烫的心情,暗地里安慰自 ...

  5. r语言 回归分析 分类变量_R语言 | 回归分析(二)

    R语言 语言学与R语言的碰撞 Xu & Yang PhoneticSan 学习参考 Discovering Statistics Using R Statistics for Linguist ...

  6. r语言 面板数据回归_R语言_018回归

    回归分析是统计学的核心.它其实是一个广义的概念,指那些用一个或多个预测变量来预测响应变量的方法.通常,回归分析可以用来挑选与响应变量相关的解释变量,可以描述两者的关系,也可以生成一个等式,通过解释变量 ...

  7. r语言读取excel数据_R语言 | 更快的表格文件读取方法!

    友情提示:蓝色下划线字体为引文,请保持警惕! 使用R语言读取 Affymetrix Human Exon 1.0 ST Array 芯片平台探针注释文件: https://www.affymetrix ...

  8. r语言 回归分析 分类变量_R语言进阶之广义线性回归

    广义线性回归是一类常用的统计模型,在各个领域都有着广泛的应用.今天我会以逻辑回归和泊松回归为例,讲解如何在R语言中建立广义线性模型. 在R语言中我们通常使用glm()函数来构建广义线性模型,glm实际 ...

  9. r语言中检测异常值_R中的异常值检测

    r语言中检测异常值 介绍 (Introduction) An outlier is a value or an observation that is distant from other obser ...

  10. R语言怎么写积分_R语言入门的基本操作(1)

    大家好,这是从知乎<一个大学生的日常笔记>中迁移过来的R语言教程的第一篇. 这一份笔记follow了两本非常优秀的R语言教材,分别是Robert I.Kabacoff的<R语言实战& ...

最新文章

  1. 屏幕录制工具(可录制GIF)
  2. 67 个节省开发者时间的实用工具、库与资源(前端向)
  3. yum安装php5.6 nginx,CentOS 7 yum安装 Nginx1.16 + MySQL5.5 PHP5.6
  4. rust为什么显示不了国服_捋捋 Rust 中的 impl Trait 和 dyn Trait
  5. hive 集成sentry
  6. Python类方法、实例方法、静态方法和属性方法详解
  7. 每日源码分析 - Lodash(remove.js)
  8. iis10.0 php多版本,IIS7 IIS8 中多个版本php共存的方法
  9. C# 用IrisSkin4.dll美化你的WinForm
  10. 数字格式化输出NumberFormat
  11. 科学计算matlab尔雅答案,科学计算与MATLAB语言超星尔雅最新答案大全
  12. Everything使用攻略和技巧
  13. oracle 文本日期相减,日期相减等于整数的问题
  14. python 残差图_利用matplotlib绘制多元自变量的回归残差
  15. 在VMware虚拟机上安装 Win7 操作系统
  16. 离职,我应该做什么?
  17. 康奈尔大学计算机交叉学专业,美国康奈尔大学EE专业设置的五大方向
  18. MFC如何让背景图随窗口大小改变
  19. MySQL 客户端安装
  20. GGS ERROR 160 Bad column index

热门文章

  1. 外包被裁能要n+1吗?签约软通动力,在滴滴工作,滴滴裁员,我要n+1,软通不认!...
  2. 滴滴裁员 多一个月补偿反转苦情戏
  3. verilog latch
  4. js当前日期倒推,向前倒推或往后推算
  5. C语言求卢卡斯序列,斐波那契序列和卢卡斯序列
  6. Hej Stylus for Mac(手写笔画图工具)
  7. mysql表文件被删除,MySQL数据表InnoDB引擎表文件误删恢复
  8. Jenkins集成动态salve报错 连接测试报错:
  9. 微信公众号怎么把网页链接地址添加
  10. 实现图片验证码与手机短信验证码