DEV Community

Cover image for R for beginners
Sakshi
Sakshi

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

Variables

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

Vectors

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.
print("abc");

# Atomic vector of type double.
print(12.5)

# Atomic vector of type integer.
print(63L)

# Atomic vector of type logical.
print(TRUE)

# Atomic vector of type complex.
print(2+3i)

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

# Get the class of the vector.
print(class(apple))
Enter fullscreen mode Exit fullscreen mode

Lists

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)
print(list_data)


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

# Print the list.
print(list1)
Enter fullscreen mode Exit fullscreen mode

Matrices

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))

print(P)

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

Arrays

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))
print(a)
Enter fullscreen mode Exit fullscreen mode

Factors

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.
print(factor_apple)
print(nlevels(factor_apple))
Enter fullscreen mode Exit fullscreen mode

Dataframes

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)
)
print(BMI)
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

Loops

  • repeat
  • while
  • for loop

functions

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.
print(seq(32,44))

# Find mean of numbers from 25 to 82.
print(mean(25:82))

# Find sum of numbers frm 41 to 68.
print(sum(41:68))
Enter fullscreen mode Exit fullscreen mode

User defined function

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

Strings

  • 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'
print(a)

b <- "Start and end with double quotes"
print(b)

c <- "single quote ' in between double quotes"
print(c)

d <- 'Double quotes " in between single quote'
print(d)
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)