DEV Community

Cover image for R for beginners

Posted on

R for beginners

R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman.

Features of R

  • R is a well-developed, simple and effective programming language which includes conditionals, loops, user defined recursive functions and input and output facilities

  • R has an effective data handling and storage facility

  • R provides a suite of operators for calculations on arrays, lists, vectors and matrices.

  • R provides a large, coherent and integrated collection of tools for data analysis.

A simple program in R commandline

> myString <- "Hello, World!"
> print ( myString)
[1] "Hello, World!"

Enter fullscreen mode Exit fullscreen mode

Comments in R

# My first program in R Programming


In contrast to other programming languages like C and java in R, the variables are not declared as some data type.

The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable.

  • Vectors
  • Lists
  • Matrices
  • Arrays
  • Factors
  • Data Frames


When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector.

Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.

# Atomic vector of type character.

# Atomic vector of type double.

# Atomic vector of type integer.

# Atomic vector of type logical.

# Atomic vector of type complex.

# Atomic vector of type raw.
Enter fullscreen mode Exit fullscreen mode
# Create a vector.
apple <- c('red','green',"yellow")

# Get the class of the vector.
Enter fullscreen mode Exit fullscreen mode


A list is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it.

# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.
Enter fullscreen mode Exit fullscreen mode


A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix function.

byrow is a logical clue. If TRUE then the input vector elements are arranged by row.

dimname is the names assigned to the rows and columns.

# Create a matrix.

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))


M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
Enter fullscreen mode Exit fullscreen mode


While matrices are confined to two dimensions, arrays can be of any number of dimensions

# Create an array. an array with two elements which are 3x3 matrices each.

a <- array(c('green','yellow'),dim = c(3,3,2))
Enter fullscreen mode Exit fullscreen mode


Factors are the r-objects which are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always character irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.

Factors are created using the factor() function. The nlevels functions gives the count of levels.

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.
factor_apple <- factor(apple_colors)

# Print the factor.
Enter fullscreen mode Exit fullscreen mode


Data frames are tabular data objects. Unlike a matrix in data frame each column can contain different modes of data. The first column can be numeric while the second column can be character and third column can be logical. It is a list of vectors of equal length.

Data Frames are created using the data.frame() function.

# Create the data frame.
BMI <-  data.frame(
   gender = c("Male", "Male","Female"), 
   height = c(152, 171.5, 165), 
   weight = c(81,93, 78),
   Age = c(42,38,26)
Enter fullscreen mode Exit fullscreen mode

Types of Operators

We have the following types of operators in R programming −

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Miscellaneous Operators

Decision Making

if, if-else and switch statement


  • repeat
  • while
  • for loop


A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions and the user can create their own functions.

Function Definition

An R function is created by using the keyword function. The basic syntax of an R function definition is as follows −

function_name <- function(arg_1, arg_2, ...) {
   Function body 
Enter fullscreen mode Exit fullscreen mode

Built-in Function

# Create a sequence of numbers from 32 to 44.

# Find mean of numbers from 25 to 82.

# Find sum of numbers frm 41 to 68.
Enter fullscreen mode Exit fullscreen mode

User defined function

new.function <- function(a) {
   for(i in 1:a) {
      b <- i^2
Enter fullscreen mode Exit fullscreen mode


  • The quotes at the beginning and end of a string should be both double quotes or both single quote. They can not be mixed.
  • Double quotes can be inserted into a string starting and ending with single quote.
  • Single quote can be inserted into a string starting and ending with double quotes.
  • Double quotes can not be inserted into a string starting and ending with double quotes.
  • Single quote can not be inserted into a string starting and ending with single quote.
a <- 'Start and end with single quote'

b <- "Start and end with double quotes"

c <- "single quote ' in between double quotes"

d <- 'Double quotes " in between single quote'
Enter fullscreen mode Exit fullscreen mode

R packages

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation.

Thanks for reading...

Top comments (0)