What is R?
R is a powerful and flexible tool/language which is free and can be used from calculating data sets to creating graphs and maps. Some other advantages of using R is that it has an interactive language, data structures, graphics availability, a developed community, and the advantage of adding more functionalities through an entire ecosystem of packages. R is a scriptable language that allows the user to write out a code in which it will execute the commands specified.
On Linux, you can just type: sudo apt-get install r-base .
On Windows, you can just download R for Windows from http://www.r-project.org/.
The R Packages
- Like other open-source programming languages, R has a bunch of useful packages to make our life easier. All these packages are available on CRAN (Comprehensive R Archive Network) repository.
- Install packages
- from CRAN –>
or you can also install multiple packages at once: install.packages(list_of_packages) .
install.packages(c("cowplot", "googleway", "ggplot2", "ggrepel", "ggspatial", "libwgeom", "sf", "rworldmap", "rworldxtra"))
- from github
- use devtools::install_github('msperlin/PkgsFromFiles') .
- from CRAN –> install.packages(packages_name) .
- update packages:
type update.packages() from your R console to get the latest version of all your installed packages.
- load package –>
- using library () –> library(package_name) or library("package_name")
- using require() –> require(package_name) or require("package_name")
- See the list of attached packages/libraries –> search()
- accessing a function directly form its package –> using double colons
dplyr::filter(df, x > threshold) –> accessing the filter function directly from dplyr package.
- There are 3 types of package:
- contains function only
- contains data only –> e.g: openintro, etc.
- contains data and function
Things about R that you have to be aware of
=symbol for assignment is replaced with <-
- index of an array starts from 1, not 0 as in other languages.
- comment: #
- R is case sensitive
- separated commands
Commands are normally separated using a newline, but it can also be separated using ; .
- A pair of backticks (
Example: `c a t` <- 1 # is valid R
- managing object –> similar to that of Linux.
- ls() –> list the object in your workspace
- rm() –> remove the object in your workspace.
- Microsoft supports R: Microsoft R open
- Conference related to R:
- Don’t dread the error messages:
Error messages are actually your friend—they help you find and identify bugs and edge cases where your code is failing.
- The help function
As a learner myself, I always take advantage of the extensive built-in help system which is an essential part of finding solutions to your R programming problems.
You can get help by typing help(your_query) or ?your_query .
Let’s say you want to know what
str()is in R. You can type ?str or help(str) in your console.
- The keyword search
Use apropos(keyword) to find more detail about certain keyword you are interested in.
Let’s say I want to search for functions related to ‘plot’.
R1234567891011121314> apropos('plot') "assocplot" "barplot" "barplot.default" "biplot" "boxplot" "boxplot.default" "boxplot.matrix" "boxplot.stats" "cdplot" "coplot" "fourfoldplot" "interaction.plot" "lag.plot" "matplot" "monthplot" "mosaicplot" "plot" "plot.default" "plot.design" "plot.ecdf" "plot.function" "plot.new" "plot.spec.coherency" "plot.spec.phase" "plot.stepfun" "plot.ts" "plot.window" "plot.xy" "preplot" "qqplot" "recordPlot" "replayPlot" "savePlot" "screeplot" "spineplot" "sunflowerplot" "termplot" "ts.plot"
See the usage example of certain function.
R123456789101112131415161718192021> example(sum)sum> ## Pass a vector to sum, and it will add the elements together.sum> sum(1:5) 15sum> ## Pass several numbers to sum, and it also adds the elements.sum> sum(1, 2, 3, 4, 5) 15sum> ## In fact, you can pass vectors into several arguments, and everything gets added.sum> sum(1:2, 3:5) 15sum> ## If there are missing values, the sum is unknown, i.e., also missing, ....sum> sum(1:5, NA) NAsum> ## ... unless we exclude missing values explicitly:sum> sum(1:5, NA, na.rm = TRUE) 15
- Get help online and offline
Take the advantage of using Google and StackOverflow. You can also ask friends who are expert in R or post the questions in various R forums.
Also make sure to stay informed by following the hashtag #rstat on Twitter.
Online material that might be useful:
- free course and tutorials:
- community support:
- Tips for help
Now that everything is all set, you can proceed to start writing your first R code.
See you in next post 🙂