[introduction to R language] R language environment construction


R language is a very powerful tool. Almost most of the data analysis work can be completed in R, and it has strong drawing function support. It can make the data in your hand visually present in various postures. It also supports Windows, Mac OS and Linux systems, and is relatively simple and convenient to use.

If you want to start learning data analysis, or just want to make a cool data analysis diagram, R language will be a good choice.

R download and installation

Open https://cran.r-project.org/mirrors.html , select the corresponding mirror station according to your location, usually the mirror station under China.

According to the platform you use, select the corresponding installation package to download and install.

If it is Windows, select the base version to download and install. You can select all default options during the installation process.

If you are using a Mac, select Download R for (Mac) OS X, download the latest version of the installation package and install it by default.

After installation, you will see a plain icon. Yes, this is the R language itself.

R studio download and installation

Open https://www.rstudio.com/products/rstudio/download/ , select the Free version to download.

The corresponding download link will be displayed here according to your platform. Click download.

During installation, select the default option except the installation location.

After installation, you can harvest a new icon, which should be a little more mellow this time.

Simple example of R language

The main work has been completed. Let's move our hands and gracefully click (or double-click) the R Studio icon to feel the charm of R language.

When you open RStudio, you will see the R language version, copyright information and some useful tips on the Consule panel.

R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)

R It is free software without any guarantee.
Under certain conditions, you can spread it freely.
use'license()'or'licence()'Let's look at the detailed conditions of dispersion.

R It's a cooperative program, and many people have contributed to it.
use'contributors()'Look at the details of the partners
 use'citation()'Will tell you how to quote correctly in the publication R or R Package.

use'demo()'Let's look at some demonstration programs'help()'To read the online help file, or
 use'help.start()'adopt HTML View the help file in the browser.
use'q()'sign out R.

The overall interface is shown in the figure below:

Enter: example(plot) in the consumption panel and tap enter several times to see some examples of the plot function.

> example(plot)

plot> Speed <- cars$speed

plot> Distance <- cars$dist

plot> plot(Speed, Distance, panel.first = grid(8, 8),
plot+      pch = 0, cex = 1.2, col = "blue")
Press<Return>Click to see the next figure: 

plot> plot(Speed, Distance,
plot+      panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
plot+      pch = 0, cex = 1.2, col = "blue")
Press<Return>Click to see the next figure: 

plot> ## Show the different plot types
plot> x <- 0:12

plot> y <- sin(pi/5 * x)

plot> op <- par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))

plot> for (tp in c("p","l","b",  "c","o","h",  "s","S","n")) {
plot+    plot(y ~ x, type = tp, main = paste0("plot(*, type = \"", tp, "\")"))
plot+    if(tp == "S") {
plot+       lines(x, y, type = "s", col = "red", lty = 2)
plot+       mtext("lines(*, type = \"s\", ...)", col = "red", cex = 0.8)
plot+    }
plot+ }
Press<Return>Click to see the next figure: 

plot> par(op)

plot> ##--- Log-Log Plot  with  custom axes
plot> lx <- seq(1, 5, length = 41)

plot> yl <- expression(e^{-frac(1,2) * {log[10](x)}^2})

plot> y <- exp(-.5*lx^2)

plot> op <- par(mfrow = c(2,1), mar = par("mar")-c(1,0,2,0), mgp = c(2, .7, 0))

plot> plot(10^lx, y, log = "xy", type = "l", col = "purple",
plot+      main = "Log-Log plot", ylab = yl, xlab = "x")
Press<Return>Click to see the next figure: 

plot> plot(10^lx, y, log = "xy", type = "o", pch = ".", col = "forestgreen",
plot+      main = "Log-Log plot with custom axes", ylab = yl, xlab = "x",
plot+      axes = FALSE, frame.plot = TRUE)

plot> my.at <- 10^(1:5)

plot> axis(1, at = my.at, labels = formatC(my.at, format = "fg"))

plot> e.y <- -5:-1 ; at.y <- 10^e.y

plot> axis(2, at = at.y, col.axis = "red", las = 1,
plot+      labels = as.expression(lapply(e.y, function(E) bquote(10^.(E)))))

plot> par(op)

The following is the output picture:

This is a few examples of the basic drawing function plot. It can be seen that it can support many graphics. A special article will introduce the detailed use of the plot function later.


So far, R language has been successfully included in the bag. Congratulations, you have mastered another language (Hello world). [Applause here]

Next, we will continue to introduce the basic usage of R language and the more important function usage methods. The goal is to use R language to conduct preliminary analysis of data, so that it can be applied in life and work.

The reason why I started to write articles related to R language is that I realized the importance of data in my work. Although professional people should be allowed to do professional things, if I know nothing about it, how can I know what professional people can do? Besides, it is not appropriate to ask students who need data for little things. I may be more efficient after I have a clue and preliminary verification. And it doesn't weigh on me. In addition, I was interested in data analysis when I was in college and once learned R language. Now it's a review and review.

Tags: Data Analysis

Posted by xerodefect on Fri, 06 May 2022 00:17:56 +0300