Quick Start with R Programming

This tutorial helps you quickly learn how to do R programming.

Table of Contents


1  Must-know Prelimibaries for Learning R

1.1 Get helps in R

  • To get help on a function or a dataset:
    ?function_name <==> help(function_name)
    For example, ?mean is equivalent to help(mean) 
  • To find functions using a keyword
    ??keyword <==> help.search(keyword)
    Note that search term of multiwords shall be quoted.
    For example, ??plotting is equivalent to help.search(plotting)
    ??”nonlinear regression” is equivalent to help.search(“nonlinear regression”)

  • The apropos(keyword) function can be used to find variables and functions containing the keyword and available for your usage, including the ones you defined.

    A fancier usage of apropos() function is to do matching with regular expressions.

  • Use example() function to hearn a function by examples
    For example: example(plot)
  • Use demo() function to get demonstrations for some concepts. For example
    demo() lists all demonstrations.
    demo(colors) demonstrates R’s predefined colors() function.
  • Use the browseVignettes() function to view HTML materials for the packages installed on local machine, or use the vignette() function to view specific HTML material.

  • The RSiteSearch() function can be used to run a query at http://search.r-project.org to look for any package. 


1.2  Install packages and extra related software

To install packages 

There are some software for extending functionalities of R.

  • Under Linux, they can be easily installed by package manager. 
  • Under Windows, these software can be installed via the installr package


1.3  R as a scientific calculator

  • Under R command line, x does purely assignment, but (x ) does both assignment and print out.
  • All the arithmetic operators in R are vectorized: an operator or a function will act on each element of a vector without the need for you to explicitly write a loop. 
  • You can use either ‘^’ or ‘**’ for exponentiation, but using ‘^’ is more common.
  • Different divisions
  • The functions log1p(x) and expm1(x) calculate more accurately for very small value of x than log(1+x) and exp(x-1), respectively. 

  • You can use ‘==’ to check equality between integers, but use this for nonintegers will be problemmatic.
    • The all.equal(x,y) function can be used to check if two objects x and y are (nearly) equal, with tolerance level, by default 1.5e-8, specified. 
      If the objects are different, comparison is still made to some extent, and a report of the differences is returned. To suppress report and enforce TRUE/FALSE output, the isTRUE() function can be used to wrap the all.equal() function.
    • Do not use all.equal() directly in if expressions—either use isTRUE(all.equal(….)) or identical() if appropriate.
    • The safe and reliable way to test whether two objects are exactly same is to use the identical(x,y) function
  • Global assignment x < can alternatively be done by assign(x, 5, globalenv()).
  • Unlike TRUE and FALSE that are reserved by R, T and F are defined, but not reserved, by R. Therefore, it is highly recommended to use TRUE and FALSE, instead of T and F.
    The three vectorized logical operators are ‘!‘, ‘&‘, and ‘|‘, representing not, and, and or, respectively. 

    The three possible logical values are TRUE, FALSE, and NA, we can do combination as following

    The any() and all() functions are usefull for dealing with logical vectors

1.4  Some notes about object classes

  • Type .Machine gives you some information about the properties of R’s numbers.

    Note that R has three classes of numerical values: numeric (for floating), integer, and complex

  • R does not distinguish single-character and multiple-character strings.
  • Factors are, in essence, integers with labels, since factor values are stored as intergers rather than characters, for the purpose of efficient storage in memory.
    Sometimes, in order to manipulate contents of factors, we need to use as.character() to convert factors to strings in order to use string manipulation functions. 

  • To chack whether an object is of a specific class, we can equivalently use either of following
    However, the latter way is much more recommended than the former one. In an following example it can be seen that the first way is not as robust as you might imagine.  
    – To test a floating number, you shall use is.double() function, since is.numeric() returns TRUE for both integer and floating numbers. 

    – We can list all is.xxx() functions in the base package as following

    Note that the assertive package contains more is functions with a consistent maning scheme. 
    – For every checking is.xxx() function, there almost always exist a corresponding class-converting function as.xxx().

    Note that, in above example, as.xxx() is more robust than as(x,”xxx”)
    – We can directly change the class of an object by assigning a new class to it, but this is not recommended.

  • Some functions are available for examining variables, such as summary(), head(), tail(), and str().
    Sometimes the print method of a class might obscure some internal structure of the object. To bupass this, we can use unclass() function to see how the object is constructed. 
    The attributes() function gives you a list of all the attributes belonging to an object.

  • For visualizing 2-D variables such as matrix and data frame, we can display the object in a (popup) spreadsheet, by one of following ways
    • view(): read only
    • edit(): changible, stored to a new object
    • fix(): changible, stored to original object


1.5  Objects in the workspace: list and remove

Some examples:

  • ls(): list all objects in the workspace
  • ls(pattern=”ea“): list objects whose names contain “ea”.
  • ls(all.names=TRUE): list all variables including those hidden ones whose name start with “.”.

  • rm(a,b,gender): remove specific variables from the workspace.
  • rm(list=ls()): remove everything from the workspace. 


Leave a Reply

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