如何安装r和rstudio?

Antoine Soetewey 2019-12-17 10 minute read

什么是r和rstudio?

R

统计计划 R 只不过是一种编程语言,主要用于数据操纵并进行统计分析。在撰写本文时,这种语言是(之一)统计中的领先程序,尽管不是统计学家使用的唯一编程语言。

要使用R,我们需要两件事:

  • 编写代码的文本编辑器
  • 运行此代码的地方

你可以下载r //cran.r-project.org/mirrors.html。选择最接近您的Cran镜像站点,然后在标有“下载并安装R”的框中,单击与操作系统对应的链接。

rstudio.

这是Rstudio方便的地方。 Rstudio是R. R和RStudio的综合开发环境(IDE)。 R是运行所有代码的程序,Rstudio是另一个程序,允许您以更舒适和更友好的方式控制R. RStudio具有提供用于编写​​代码的强大文本编辑器的优势,并在此编辑器中运行编写的代码。出于这些原因,我强烈建议使用RStudio而不是R.

你可以下载rstudio www.rstudio.com..

Rstudio的主要组成部分

既然这两个程序都安装在您的计算机上,让我们深入进入RStudio的主要组件。

默认情况下,Rstudio窗口有三个窗格:

  1. 控制台(红色窗格)
  2. 环境(绿色窗格)
  3. 文件,绘图,帮助等(蓝色窗格)

rstudio窗口

控制台(红色窗格)是您可以执行代码的位置(稍后有红色和蓝色窗格的更多信息)。默认情况下,文本编辑器不会自动打开。要打开它,请单击文件> New File >R脚本或单击表示白色左上角的小绿色十字架的白纸的按钮,然后在R脚本上:

Rstudio的新r脚本

一个新的窗格(下面的橙色),也称为文本编辑器,打开,您将能够编写代码。将执行代码,并在控制台(红色窗格)中显示的结果。

在rstudio中写下并执行您的代码

请注意,您还可以在控制台(红色窗格)中编写代码。但是,我强烈建议在文本编辑器(橙色窗格)中编写代码,因为您可以保存在文本编辑器中写入的代码(因此稍后再执行它),而无法保存在控制台中写入的代码。

要执行在文本编辑器(橙色窗格)中编写的代码,您有两个选项:

  • 键入代码,然后按“RUN”按钮(见下文)或使用键盘快捷键CTRL + ENTER(CMD +在MAC上输入)。 只有块 然后将执行光标所在的代码。
  • 键入您的代码,然后在文本编辑器中选择要执行的部件,然后按“运行”按钮或使用键盘快捷键Ctrl + Enter(CMD +在Mac上输入)。所有的 选择 代码将被执行

Rstudio运行代码

For example, try typing 1+1 in the text editor and execute it by clicking on “Run” (or CTRL/cmd + Enter). You should see the result 2 in the console, as shown in the screenshot below:

在rstudio中执行的代码示例

文本编辑器和控制台是您最常使用的窗格。然而,在使用RStudio时,另外两个窗格(早期推出的蓝色和绿色窗格)仍然非常有用。

环境(绿色窗格) displays all values stored by RStudio. For example, if you type and execute the code a = 1, RStudio will store the value 1 for a, as shown in the screenshot below:

Rstudio的环境窗格

This means that you can now perform any computations with a, such that if you execute a + 1, RStudio will render 2 in the console. In this pane you can also see a tab with a history of the code executed and a button to import a dataset (more on 在RStudio导入数据集)。

最后一个窗格(蓝色)是您将在其中找到其他一切,例如您的文件,绘图,软件包,帮助文档等。我更详细地讨论了文件选项卡 这里 所以让我们讨论其他选项卡:

  • 阴谋: where you will see the rendered plots. For instance, run plot(1:10) and you should see it in this tab. If you plotted more than one plots, you can navigate between them by clicking on the arrows. You can open the plot in a new window by clicking on Zoom and export your plot by clicking on Export. Those buttons are located just under the Plot tab (see figure below)
  • Packages: where you see all your installed packages. Only fundamental functionalities come with R. Everything else must be installed from packages. Remind that R is open source; everyone can write code and publish it as a package. You are then able to use this package (and all functions built inside this package) for free. Some packages are installed by default, all others must be installed by running install.packages("name of the package") (do not forget "" around the name of the package!). Once the package is installed, you must load the package and only after it has been loaded you can use all the functions it contains. To load a package, run library(name of the package) (this time "" around the name of the package are optional, but can still be used if you wish). You also have the possibility to install and load packages via the buttons under the Packages tab. For this, click on the button Install under Packages, type the name of the package you want to install and then click on Install. You will see that the code appears in the console. To load the package, find the package you want to load in the Packages window (you can use the search box), then click on the checkbox next to the name of the package. Again, the code is run in the console. See the figures below if needed. Note that you will need to install packages 只有一次,1 但是加载包 每一次 你打开rstudio。此外,请注意,需要Internet连接来安装包,而不需要加载包
  • Help: documentation about all functions written for R. To access the help of a function, run help("name of the function") 或者 simply ?name of the function. For example, to see the help about the mean function, run ?mean. You can also press F1 while having your cursor on a function

绘制rstudio的按钮

第1步:单击“安装”按钮

第2步:键入包的名称,然后单击“安装”

第3步:通过单击包名称旁边的框来加载您的包

代码的例子

现在您已安装R和RStudio,并且您知道其主要组件,以下是基本代码的一些示例。

提供了更高级的代码和分析 其他关于r的文章,特别是在本文中 r中的数据操纵.

计算器

计算 \(5 * 5 \)

5 * 5
## [1] 25

计算 \(\ frac {1} {\ sqrt {50 \ pi}} \,e ^ { - \ frac {(10 - 11)^ 2} {50}} \)

1 / sqrt(50 * pi) * exp(-(10 - 11)^2 / 50)
## [1] 0.07820854

正如您所看到的,某些值如 \(\ pi \) are stored by default so you do not need to specify its value. Note that RStudio is case sensitive, but not space sensitive. This means that pi is different than Pi but 5*5 gives the same result than 5 * 5.

评论

To add comments in your code, use # before the code:

# A comment
# Another comment
1 + 1
## [1] 2

存储和打印值

Note that in order to store a value inside an object, using = 或者 <- is equivalent. I however recommend using <- to follow the guidelines of R programming. You can name your objects (A and B in our case) as you like. However, it is recommended to use short and concise names (as you will most likely type them several times) and avoid special characters.

A <- 5
B <- 6

存储值时,RStudio不会在控制台上显示它。要在控制台中存储值并将其打印,请使用:

(A <- 5)
## [1] 5

或者:

A <- 5
A
## [1] 5

vectors.

It is also possible to store more than one value inside an object via the function c() (c stands for combine).

A <- c(1 / 2, -1, 0)
A
## [1]  0.5 -1.0  0.0

矩阵

Or create a matrix via matrix():

my_mat <- matrix(c(-1, 2, 0, 3), ncol = 2, nrow = 2)
my_mat
##      [,1] [,2]
## [1,]   -1    0
## [2,]    2    3

You can access the help of this function via ?matrix 或者 help("matrix"). Note that inside a function, you can have multiple arguments separated by a comma. Inside matrix(), the first argument is the vector c(-1, 2, 0, 3), the second is ncol = 2 and the third is nrow = 2. For all functions in RStudio, you can specify an argument by its order inside the function or by the name of the argument. If you specify the name of the argument, the order does not matter anymore, so matrix(c(-1, 2, 0, 3), ncol = 2, nrow = 2) is equivalent to matrix(c(-1, 2, 0, 3), nrow = 2, ncol = 2):

my_mat2 <- matrix(c(-1, 2, 0, 3), nrow = 2, ncol = 2)
my_mat2
##      [,1] [,2]
## [1,]   -1    0
## [2,]    2    3
my_mat == my_mat2 # is my_mat equal to my_mat2?
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE

生成随机值

基于正常分布生成10个值,其平均值 \(\ mu = 400 \) 和标准偏差 \(\ sigma = 10 \):

my_vec <- rnorm(10, mean = 400, sd = 10)
# Display only the first 5 values:
head(my_vec, 5)
## [1] 401.8291 396.2402 415.8425 398.3830 378.2698
# Display only the last 5 values:
tail(my_vec, 5)
## [1] 387.0264 407.8640 395.6606 392.5291 402.0448

You will have different values than mines due to the fact that they are randomly generated. If you want to make sure to have always the same random values, use set.seed() (with any numeric inside the brackets). For instance, with the following code, you should have the exact same values, no matter where and when you run it:

set.seed(42)
rnorm(3, mean = 10, sd = 2)
## [1] 12.741917  8.870604 10.726257

阴谋

plot(my_vec,
  type = "l", # "l" stands for line
  main = "Plot title",
  ylab = "Y-axis label",
  xlab = "X-axis label"
)

这只是对Rstudio可能性的概论非常有限。如果您想了解更多,我建议您阅读其他 与r相关的文章, 从...开始 如何导入Excel文件 或者 如何在r中操纵数据集.

谢谢阅读。我希望这篇文章有助于您安装R和RStudio。

一如既往,如果您有问题或与本文所涵盖的主题相关的建议,请将其添加为评论,以便其他读者可以从讨论中受益。


  1. 实际上,您需要重新安装每个新r更新的软件包。但是,如果您在相同的R版本上工作,则需要只安装软件包一次,而是每次打开RStudio时加载它们。↩︎



喜欢这篇文章?

获取更新 每次发布新文章。
任何垃圾邮件都没有任何垃圾邮件。
分享: