bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!
×
UPTO
50%
OFF!

Data Structures in R Programming

Data structure can be defined as the specific form of organizing and storing the data. R programming supports five basic types of data structure namely vector, matrix, list, data frame and factor. This chapter will discuss these data structures and the way to write these in R Programming.

  1. Vector – This data structures contain similar types of data, i.e., integer, double, logical, complex, etc. In order to create a vector in R Programming, c() function is used.
For example, 

> x <- 1:7; x[1] 1 2 3 4 5 6 7 > y <- 2:-2; y[1]  2  1  0 -1 -2
  1. Matrix – Matrix is a two-dimensional data structure and can be created using matrix () function. The values for rows columns can be defined using nrow and ncol arguments. However providing both is not required as other dimension is automatically taken with the help of length of matrix.
For example,

> matrix(1:9, nrow = 3, ncol = 3)    [,1] [,2] [,3][1,]    1    4    7[2,]    2    5    8[3,]    3    6    9 > # same result is obtained by providing only one dimension> matrix(1:9, nrow = 3)    [,1] [,2] [,3][1,]    1    4    7[2,]    2    5    8[3,]    3    6    9
  1. List – This data structure includes data of different types. It is similar to vector but a vector contains similar data but list contains mixed data. A list is created using list ().
For example,  > x <- list("a" = 2.5, "b" = TRUE, "c" = 1:3)

> str(x)List of 3$ a: num 2.5$ b: logi TRUE$ c: int [1:3] 1 2 3
  1. Data frame – This data structure is a special case of list where each component is of same length. Data frame is created using frame() function.

For example,

> x <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John","Dora"))

> str(x)    # structure of x

'data.frame':   2 obs. of  3 variables:

$ SN  : int         1          2

$ Age : num  21           15

$ Name: Factor w/ 2 levels "Dora","John": 2 1
  1. Factor – Factors are used to store predefined and categorical data. It can be created using factor() function.
For example,

> x <- factor(c("single", "married", "married", "single"));

> x

[1] single  married married single

Levels: married single

> x <- factor(c("single", "married", "married", "single"), levels = c("single", "married", "divorced"));

> x

[1] single  married married single

Levels: single married divorced
  1. String – Any value written inside a single quote or double quotes is referred to as String.
For example, 

x <- “This is a valid proper ‘ string”

print(x)

y <- ‘this is still valid as this one” double quote is used inside single quotes”

print(y)

Output:

This is a valid proper ‘ string

this is still valid as this single” double quote is used inside single quotes

"0 Responses on Data Structures in R Programming"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

  • To avail this offer, enroll before 27th May 2017.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Data Structures in R Programming.