Thursday 27 August 2020

Data Structures In R

Data structures is used to identify and represent the data..

There are different types of data structures in R such as

1) vector:It is homogeneous(same) type and having one dimension.

2) list:It is heterogeneous(different) type and having one dimension..

3)matrix :It is homogeneous type and having 2 dimensions..

4)data frame:It is heterogeneous type and having 2 dimensions..

5) factors:It is homogeneous type and having one dimension..

6)Arrays:It is homogeneous type and having n number of dimensions..


1)vectors: one dimensional arrays are vectors,homogeneous

syntax:

variable name<-c()

Example:

a<-c(24,56,78)

a

b<-c("one","two","three")

b

x<-c(TRUE,FALSE,TRUE,FALSE)

x

y<-10   #element vector(scalars)

Where a,b,x is the variable name and c is used for combine the value..

Accessing vector elements

vec<-c("a","d","w","e","g")

vec

vec[3]  #output:[1] "w"

vec[c(2,5)]  #ouput: [1] "d" "g"

2)Matrix:These are two dimensions data structures 

mat<-matrix(c(1,2,3,4),nrow=2,ncol=2)  #by default column wise

mat

ouput: 1  3

            2  4


mat1<-matrix(c(1,2,3,4),nrow=2,ncol=2,byrow=TRUE) # if true matrix filed by rows

mat1

ouput: 1  2

            3 4

Note:

Compare both the output to know the result

Accessing matrix elements

mat<-matrix(c(1,2,3,4),nrow=2,ncol=2) 

or

mat<-matrix(c(1:4),nrow=2,ncol=2)

mat[1,]  #returns 1st  row in matrix

mat[,1]  #returns 1st column in matrix

mat[1,2] #returns the elements in the first row of second column

Arrays:are multidimensional..

#dim=c(row,column,number of matrix)

x1<-array(1:12,dim=c(2,2,3)) 

x1

Access elements in  in arrays

#print 2 rows of 2 matrix

print(x1[2,,2])

#print 2 column of 3 matrix

print(x1[,2,3])

#print all column and rows of 3 matrix

print(x1[,,3])


Note:

Basic data types in R are character, numeric, integer, complex, and logical.

Objects may have attributes, such as name, dimension, and class.

1 comment:

  1. You don't realize how quickly technology is changing. Data science is highly technical and is therefore in high demand. A career in data science will open up many lucrative job opportunities. So, if you have been wanting to start your career in Data Science, now is the best time to enroll in a data science program with one of the best data science training institute in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...