There are two ways to run codes in R. These are:
Interactive Mode.
Batch Mode.
Sometimes it’s preferable to automate the process of running R. For example, we may wish to run an R script that generates a graph output file, and not have to bother with manually running R. Here’s how it could be done.
In RStudio, Go to File > New File > R Scipt. Then type the following in the script.
We start R from our R console command line, and get the greeting message and the > prompt:
R : Copyright 2005,
The R Foundation for Statistical Computing
Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0
...
Type ‘q()’ to quit R.
>Now let’s make a simple data set, a vector in R parlance, consisting of the numbers 1, 2 and 4, and name it x:
The standard assignment operator in R is <-.
However, there are also ->, = and even the assign() function.
The “c” stands for “concatenate.”
Thus we can also do, for instance,
which would set q to (1,2,4,1,2,4,8).
Try to print the vector x to the screen by typing its name.
## [1] 1 2 4
Yep, sure enough, x consists of the numbers 1, 2 and 4.
Let’s take a closer look to our results.
The “[1]” is an indicator that this is the first row or output. If there were two rows of outputs with six items per row, then the second row would be labeled [7]. Our output in this case consists of only one row, but this notation helps users read voluminous output consisting of many rows.
In interactive mode, you can print an object in R by simply typing its name, let us try to print the third element of the vector x.
## [1] 4
## [1] 2.333333
## [1] 1.527525
## [1] 2.333333
Note that we use # to write comments.
We can work with one of R’s internal datasets, which is intended for demos. You can get a list of these datasets by typing
The output is as follow:
Figure
One of the datasets is Nile, containing data on the flow of the Nile River. Let’s again find the mean, standard deviation,
## [1] 919.35
## [1] 169.2275
We can also plot the histogram.
To read more about a given function, for example mean, the R function help() can be used as follow:
Or use this:
The output look like this:
Figure 2
If you want to see some examples of how to use the function, type this: example(function_name).
##
## sum> ## Pass a vector to sum, and it will add the elements together.
## sum> sum(1:5)
## [1] 15
##
## sum> ## Pass several numbers to sum, and it also adds the elements.
## sum> sum(1, 2, 3, 4, 5)
## [1] 15
##
## sum> ## In fact, you can pass vectors into several arguments, and everything gets added.
## sum> sum(1:2, 3:5)
## [1] 15
##
## sum> ## If there are missing values, the sum is unknown, i.e., also missing, ....
## sum> sum(1:5, NA)
## [1] NA
##
## sum> ## ... unless we exclude missing values explicitly:
## sum> sum(1:5, NA, na.rm = TRUE)
## [1] 15
Note that, typical R help files contain the following sections:
If you want to read the general documentation about R, use the function help.start():
The output look like this:
Figure 3
## [1] "elNamed" "elNamed<-" "median" "median.default"
## [5] "medpolish" "runmed"
There are many excellent resources on R on the Internet. Here are a few:
• The R Project’s own manuals are available from the R home page,http://www.r-project.org/. Click Manuals.
• Various R search engines are listed on the R home page. Click Search.
• The sos package offers highly sophisticated searching of R materials.
• I use the RSeek search engine quite often: http://www.rseek.org/.
• You can post your R questions to r-help, the R list server. You can obtain information about this and other R list servers at http://www.r-project.org/mail.html. You can use various interfaces. I like Gmane http://www.gmane.org/.
Because of its single-letter name, R is difficult to search for using general purpose search engines such as Google. But there are tricks you can employ. One approach is to use Google’s filetype criterion. To search for R scripts (files having a .R suffix) pertaining to, say, permutations, enter this:
The -rebol asks Google to exclude pages with the word “rebol,” as the REBOL programming language uses the same suffix.
An R package is an extension of R containing data sets and specific functions to solve specific questions.
R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work.
There are also thousands other R packages available for download and installation from CRAN, Bioconductor and GitHub repositories.
After installation, you must first load the package for using the functions in the package.
The function install.packages() is used to install a package from CRAN. The syntax is as follow:
For example, to install the package named readr, type this:
Note that every time you install an R package, R may ask you to specify a CRAN mirror (or server). Choose one that’s close to your location, and R will connect to that server to download and install the package files.
It’s also possible to install multiple packages at the same time, as follow:
or you can use the RStudio point and click options to do this: Packages > Install > type the name of the package you want to install.
Figure 4
Bioconductor contains packages for analyzing biological related data. In the following R code, we want to install the R/Bioconductor package limma, which is dedicated to analyse genomic data.
To install a package from Bioconductor, use this:
GitHub is a repository useful for all software development and data analysis, including R packages. It makes sharing your package easy. You can read more about GitHub here: Git and GitHub, by Hadley Wickham.
To install a package from GitHub, the R package devtools (by Hadley Wickham) can be used. You should first install devtools if you don’t have it installed on your computer.
For example, the following R code installs the latest version of survminer R package developed by A. Kassambara (https://github.com/kassambara/survminer).
To view the list of the already installed packages on your computer, type :
or you can go to Packages tab to check the install packages.
Figure 5
R packages are installed in a directory called library. The R function .libPaths() can be used to get the path to the library.
## [1] "C:/Users/Roel Ceballos/Documents/R/win-library/4.0"
## [2] "C:/Program Files/R/R-4.0.2/library"
To use a specific function available in an R package, you have to load the R package using the function library().
In the following R code, we want to import a file into R using the R package readr, which has been installed in the previous section.
The function read_tsv() [in readr] can be used to import a tab separated .txt file:
# Import my data
library("readr")
my_data <- read_tsv("http://www.sthda.com/upload/decathlon.txt")
# View the first 6 rows and thee first 6 columns
# syntax: my_data[row, column]
my_data[1:6, 1:6]To view the list of loaded (or attached) packages during an R session, use the function search():
If you’re done with the package readr and you want to unload it, use the function detach():
To remove an installed R package, use the function remove.packages() as follow: