Getting Started with R

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.

Installing R

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/.

IDE

  1. RCommander.
  2. RStudio
    1. support for C++, SQL, D3, and Python

The R Packages

  1. 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.
  2. Install packages
    1. from CRAN –> install.packages(packages_name) .
      or you can also install multiple packages at once:  install.packages(list_of_packages) .
      Example:
      install.packages(c("cowplot", "googleway", "ggplot2", "ggrepel", "ggspatial", "libwgeom", "sf", "rworldmap", "rworldxtra"))
    2. from github
      1. use   devtools::install_github('msperlin/PkgsFromFiles') .
  3. update packages:
    type update.packages()  from your R console to get the latest version of all your installed packages.
  4. load package –>
    1. using library ()    –>    library(package_name)      or        library("package_name")
    2. using require()    –>    require(package_name)     or        require("package_name")
  5. See the list of attached packages/libraries  –> search()
  6. accessing a function directly form its package  –> using double colons :: .
    Example:
    dplyr::filter(df, x > threshold)  –> accessing the filter function directly from dplyr package.
  7. There are 3 types of package:
    1. contains function only
    2. contains data only –> e.g: openintro, etc.
    3. contains data and function

Things about R that you have to be aware of

  1. the = symbol for assignment is replaced with <-
  2. index of an array starts from 1, not 0 as in other languages.
  3. comment: #
  4. R is case sensitive
  5. separated commands
    Commands are normally separated using a newline, but it can also be separated using ; .
  6. A pair of backticks ( ) is a way to refer to names or combinations of symbols that are otherwise reserved or illegal.
    Example:  `c a t` <- 1 # is valid R
  7. managing object –> similar to that of Linux.
    1. ls() –> list the object in your workspace
    2. rm()  –> remove the object in your workspace.
  8. Microsoft supports R: Microsoft R open
  9. Conference related to R:
    1. Use of R in Official Statistics (UROS)
    2. useR!
  10. 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.

Need Help?

  1. 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.
  2. The keyword search
    Use apropos(keyword)  to find more detail about certain keyword you are interested in.
    Example:
    Let’s say I want to search for functions related to ‘plot’.
  3. Examples
    See the usage example of certain function.
  4. 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: 

  1. free course and tutorials:
    1. https://www.coursera.org/courses?query=r
    2. https://www.pluralsight.com/courses/r-programming-fundamentals
    3. https://www.rstudio.com/online-learning/
    4. https://www.datacamp.com/courses/free-introduction-to-r
  2. community support:
    1. https://community.rstudio.com/
    2. https://support.bioconductor.org/
    3. https://resources.rstudio.com/webinars/help-me-help-you-creating-reproducible-examples-jenny-bryan
  3. Tips for help
    1. https://www.r-project.org/help.html
    2. Google! – stackoverflow, biostars
    3. https://www.rstudio.com/resources/cheatsheets/

Now that everything is all set, you can proceed to start writing your first R code.

See you in next post 🙂

@erikaris

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons