Browse By

R Language –Data Structures : Online Course

R Language –Data Structures : Online Course: So many programming language are available but R language is used for specific Statistical Computing and Data Analysis. In this article, Study the different types of data structures in R programming. We will also understand the concept and implementation with the help of examples.

R Data Structure

Understanding data structures is very important because they are objects in R. it provide a method to organize data in a desired format so that it can be used effectively.

It can be organized by their dimensional (1d, 2d, or nd) and whether they’re homogeneous or heterogeneous. In Homogeneous, all contents must be of the same format (numeric, integer, character, etc.) and In heterogeneous, the contents can be of different format.  The term data structure usually refers to how data is stored, that is, the containers (vector, list, etc.).

R has the following basic data structures:

  1. Vector
  2. List
  3. Array
  4. Matrix
  5. Data frame
  6. Factor

R Vector

A vector is a one of the basic data structure that plays a vital role in R programming.

Like all other things in R, a vector is an object that stands on your working environment. Vector Can contain numerical data, string, or mix values Can increase the size of the vector by adding “concatenating additional columns”. In short, it’s a data structure and the the simplest data structure in R.  Vectors are one-dimensional data structures.

Create a Vector

Vectors are created by using the c() function and separate all the items by a comma.

Example for: Create a vector variable called object using c() function.

Input: Create a vector variable called object using c() function.
# Vector of strings
objects <- c(“pen”, “pencil”, “eraser”)
# Print objects
objects
Output:
[1] “pen” “pencil” “eraser”

Use c() Function for Multiple Elements(Multiple Data Type) in R

Input: To create Multiple Elements(Multiple Data Type) in R
# Vector of numerical values
c(5, 8, 9, 5, 8)
# Vector of string values
c(‘h’, ‘e’, ‘l’, ‘l’, ‘o’)
# Vector with numerical values in a sequence
numbers = 1:10
numbers
# Vector of logical values
log_values = c(TRUE, FALSE, TRUE, FALSE)
log_values
Output:

To create a vector with numerical values in a sequence, use the : operator:

Input: Vector with numerical values in a sequence
y <- 1:10
y
z <- 2.5:10.5
z
Output:
[1] 1 2 3 4 5 6 7 8 9 10
[1] 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5

Note:

Function: R provides many functions to examine features of vectors and other objects, for example

  • class() – what kind of object is it (high-level)?
  • typeof() – what is the object’s data type (low-level)?
  • length() – how long is it? What about two dimensional objects?
  • attributes() – does it have any metadata?

The functions typeof()length()class() and str() provide useful information about your vectors and R objects in general.

Input: Using functions typeof()length()class() and str()
z <- c("sumit", "Amit", "Nicky")
typeof(z)
length(z)
class(z)
str(z)
Output:
[1] “character”
[1] 3
[1] “character”
chr [1:3] “sumit” “Amit” “Nicky”

Arithmetic Operations on Vector in R

If arithmetic operations are to be performed on a vector, then more than one vector used must have the same length.

Input: Using arithmetic operator
a = c(5, 8, 7, 2, 10)
b = c(4, 7, 1, 10, 14)
a + b
a – b
a * b
a / b
Output:
[1] 9 15 8 12 24
[1] 1 1 6 -8 -4
[1] 20 56 7 20 140
[1] 1.2500000 1.1428571 7.0000000 0.2000000 0.7142857

Vector’s Element Sorting in R

The sort() function is used to sort the elements of a vector. With the help of sort function, values arrange in ascending or descending order. lets see the example

Input: Using sorting function
a = c(5, 8, 7, 2, -2, 10, 9)
inc = sort(a)
inc
dec = sort(a, decreasing = TRUE)
dec
Output:
[1] -2 2 5 7 8 9 10
[1] 10 9 8 7 5 2 -2

Modify a vector in R

Modify a vector using the assignment operator (<-)  by using the indexing technique and also we can truncate the elements of a vector with the help of reassignments. lets see the example

Input: Modify a vector
W<-c(-5, -2, 0, 3, 6)
W[2] <- 1;
W
W<-c(-5, -2, 0, 3, 6)
W[W<0]<- 2;
W
W<-c(-5, -2, 0, 3, 6)
W<- W[1:4];
W
Output:
[1] -5 1 0 3 6
[1] 2 2 0 3 6
[1] -5 -2 0 3

R Language: Online Course

आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।

Thanking You………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..

Read More

Reference

R Tutorial