R – Tutorial I

[This article was first published on R – StudyTrails, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

NOTE : This tutorial has been superseded by the exhaustive R tutorials Here


  • Start R in Windows using the program menu.
  • To quit :  q().
  • to call help for a function. help([function]) or ?[function]. use double quotes to escape special characters and tokens. e.g. ?”for”.
  • objects() or ls() to obtain list of objects stored. rm([object]) to remove object.
  • x= 1:4 create sequence of numbers. 1 2 3 4. colon has highest priority in expressions.
  • seq() function can also be used to generate sequences. it has five arguments to, from, by (increment), length.out (length of seq), along.with (take length from length of this argument).
  • R objects have mode i.e type for “atomic” objects ( numeric, complex, logical, character and row ). Mode for list objects is list. Other modes are functions and expressions. Objects also have a property called length. 
  • as.integer(x) coerces x to integer. There are numerous othe functions of type as.* for different coercions. Type apropos(“as.*”) in R windows to know more.
  • Each object has a class. for vectors it is the same as mode. class can be used for object oriented type of programming. method dispatch is based on the class of the first variable passed to the method. an example of a class is the value returned from fitting linear model using ‘lm’. The output of the method is an object of class ‘lm’.
  • Factors can by used to prepare categorical values for statistical analysis. the function factor(a) assigns integers to unique values (levels) in the vector a and stores the variable factor(a) as a vector of integers and also stores mapping between the integer values to the actual values in a. factor(a) can then be used in statistical analysis (summary function etc). the factors are stored in natural order of the elements in a. To explicitely provide another order use the ordered function. Read more at: link1 , link2


  • Create a vector named a. a = c(1,2,3,4). c is the generic function to combine arguments. output type is the highest of NULL < raw < logical < integer < real < complex < character < list < expression

b=c(“!”,2,a) = “!” “2” “1” “2” “3” and b=c(2,a) = 2 1 2 3.

  • Vector arguments can have names. names(a) = c(“first”,”second”,”third”) 

> a
first second  third
1      2      3

  • +,-,*,/ : Individual elements are added. shorter vector are recycled

> a=c(1,2,3)
> b=c(4,5)
> a+b
[1] 5 7 7
Warning message:
In a + b : longer object length is not a multiple of shorter object length

  • Functions – max (maximum value), min (minimum value), range =c(min(x).max(x)), length (length of vector), sum (total of all elements), prod (product of all elements), mean = sum/length, var(simple variance), sort (sorting in ascending order), pmax (vector or higest elements in individual vectors of the argument)
  • Logical vectors contain TRUE, FALSE and NA.
  • missing values are given as NA. is.na(a) returns a vector of same length as a and values FALSE if a contains ‘NA’ or ‘NaN’ , and TRUE otherwise.
  • function paste() can be used to combine strings of two or more vectors one by one. vectors are recycled if required

>  paste(c(“X”,”Y”), 1:10,2:5)
[1] “X 1 2”  “Y 2 3”  “X 3 4”  “Y 4 5”  “X 5 2”  “Y 6 3”  “X 7 4”  “Y 8 5”  “X 9 2”  “Y 10 3”

  • Index Vector: in the example below, b is the index vector in various forms.

> a=c(1,2,3,4,5,6,7,8,9,10)
> a[b]
[1]  1  4  5  6  8 10
> b=c(1:5)
> a[b]
[1] 1 2 3 4 5
> b=-(1:5)
> a[b]
[1]  6  7  8  9 10

To leave a comment for the author, please follow the link and comment on their blog: R – StudyTrails.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)