Introduction to R programming

Introduction to R programming



21 June 2019

R is a very impressive tool for statistics, graphics, and data science. It is used by thousands of people to perform serious statistical analysis and data science work on a daily basis. It is a free, open source system.

  • The two existing languages: Becker, Chambers and Wilks’ S and Sussman’s Scheme were highly influential while designing R. R was originally written by Ross Ihaka and Robert Gentleman at the Department of Statistics of the University of Auckland in New Zealand. Subsequently, a large group of people contributed to R by sending code and bug reports. John Chambers graciously contributed advice and inspiration in the early days of R, and later became a member of the core team. The current R is the result of a collaborative effort with contributions from all over the world.
  • R is the successor of ‘S’ programming language and its very similar to its appurtenance.
  • Integration with other programming languages like Python, C, C++, Java, etc. is allowed by R making it compatible and easy to use.

Getting Started and Getting Help in R

The simplest way to get help in R is to click on the Help button on the toolbar of the RGui window (this stands for R’s Graphic User Interface). However, if you know the name of the function you need help with, you just type a question mark ? at the command line prompt succeed by the name of the function. So to get guidance on read.table, just type ?read.table

Sometimes you are unable to remember the exact name of the function, but you know the subject on which you want help (e.g. data input in this case). Use the ? function (without a question mark) with your query in double quotes like this:

?"data input")

and (with any luck) you will see the names of the R functions associated with this query. Then you can use ?read.table to get detailed help.

R Programming Basics

Printing Something

Simply enter the variable name or statement at the command prompt, R will print its value. Use the print function for printing of any object. Use the cat function for producing formatted output. Here is the demonstration.

## [1] 3.141593
## [1] 3.141593
print (list("1", "2", "3"))
## [[1]]
## [1] "1"
## [[2]]
## [1] "2"
## [[3]]
## [1] "3"

This is helpful because you can always view your data: just print it. You needn’t write extra printing logic, even for complicated data structures.

The print function has limitations, however: it prints only one object at a time. Trying to print more items gives this mind-numbing error message:

print (“The zero occurs at”, 2pi, “radians.”) Error in print.default (“The zero occurs at”, 2 pi, “radians.”) : unimplemented type ‘character’ in ‘asLogical’

The cat function is a second option to print that lets you combine multiple items into a continuous output:

cat("The zero occurs at", 2*pi, "radians.", "\n")
## The zero occurs at 6.283185 radians.

Using cat gives you extra control over your output, which makes it especially useful in R scripts. A serious constraint, however, is that it cannot print compound data structures such as matrices and lists.

cat(list(“item1”,”item2″,”item3”)) Error in cat(list(…), file, sep, fill, labels, append) : argument 1 (type ‘list’) cannot be handled by ‘cat’


Datatypes are the format in which the data is stored as a variable. The variables are assigned to R-objects and data type of the R-object becomes the data type of the variable.

Following are the R-objects:

  1. Vectors
  2. Lists
  3. Matrices
  4. Arrays
  5. Factors
  6. Data Frames

And Following are the Datatypes which R supports:

  1. Numeric
    x <- c(5,3,7,8)
    ## [1] 5 3 7 8
    ## [1] "numeric"
  2. Integer
    x <- c(5,3,7,8)
    x <- as.integer(x)
    ## [1] 5 3 7 8
    ## [1] "integer"
  3. Complex
    x <- 4+7i
    ## [1] 4+7i
    ## [1] "complex"
  4. Character
    x <- 'myname'
    ## [1] "myname"
    ## [1] "character"
  5. Logical
    x <- TRUE
    ## [1] TRUE
    ## [1] "logical"
  6. Raw
    x <- charToRaw("Hello World")

R – Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides the following types of operators.

Types of Operators:

We have the following types of operators in R programming:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Miscellaneous Operators

Blog Categories
Related Blogs

None found

Request a quote