Below are the solutions to these exercises on R Data Structures.
#################### # # # Exercise 1 # # # #################### charactertype<-'A' integertype<-10 logicaltype<-TRUE complextype<-2+3i rawtype<-charToRaw('x') print(charactertype)
## [1] "A"
print(integertype)
## [1] 10
print(logicaltype)
## [1] TRUE
print(complextype)
## [1] 2+3i
print(rawtype)
## [1] 78
#################### # # # Exercise 2 # # # #################### is.vector(charactertype)
## [1] TRUE
is.vector(integertype)
## [1] TRUE
is.vector(logicaltype)
## [1] TRUE
is.vector(complextype)
## [1] TRUE
is.vector(rawtype)
## [1] TRUE
class(charactertype)
## [1] "character"
class(integertype)
## [1] "numeric"
class(logicaltype)
## [1] "logical"
class(complextype)
## [1] "complex"
class(rawtype)
## [1] "raw"
#################### # # # Exercise 3 # # # #################### list1<-list(No = 1:10,Chr="Hello",Flag=TRUE) print(list1)
## $No ## [1] 1 2 3 4 5 6 7 8 9 10 ## ## $Chr ## [1] "Hello" ## ## $Flag ## [1] TRUE
print(length(list1))
## [1] 3
#################### # # # Exercise 4 # # # #################### Mat <- matrix(3:14, nrow = 4,ncol=3,byrow = TRUE) print(Mat)
## [,1] [,2] [,3] ## [1,] 3 4 5 ## [2,] 6 7 8 ## [3,] 9 10 11 ## [4,] 12 13 14
#################### # # # Exercise 5 # # # #################### Mat <- matrix(c(2,3,6,7,8,9,12,22,-5,98,56,-77), nrow = 3,ncol=4) print(Mat)
## [,1] [,2] [,3] [,4] ## [1,] 2 7 12 98 ## [2,] 3 8 22 56 ## [3,] 6 9 -5 -77
#################### # # # Exercise 6 # # # #################### matrix1<-matrix(sample(1:10,4),nrow=2,ncol=2,byrow=TRUE) print("Matrix 1 with random value between 1 and 10")
## [1] "Matrix 1 with random value between 1 and 10"
print(matrix1)
## [,1] [,2] ## [1,] 9 1 ## [2,] 2 3
atrix2<-matrix(sample(1:10,4),nrow=2,ncol=2,byrow=TRUE) print("Matrix 2 with random value between 1 and 10")
## [1] "Matrix 2 with random value between 1 and 10"
print(matrix2)
## [,1] [,2] ## [1,] 5 3 ## [2,] 1 4
summatrix<-matrix1+Matrix2 submatrix<-matrix1-Matrix2 multmatrix<-matrix1*Matrix2 divmatrix<-matrix1/matrix2 print("sum of the Matrices")
## [1] "sum of the Matrices"
print(summatrix)
## [,1] [,2] ## [1,] 13 7 ## [2,] 4 6
print("Difference of the Matrices")
## [1] "Difference of the Matrices"
print(submatrix)
## [,1] [,2] ## [1,] 5 -5 ## [2,] 0 0
print("Multiplaction of the Matrices")
## [1] "Multiplaction of the Matrices"
print(multmatrix)
## [,1] [,2] ## [1,] 36 6 ## [2,] 4 9
print("Division of the Matrices")
## [1] "Division of the Matrices"
print(divmatrix)
## [,1] [,2] ## [1,] 1.8 0.3333333 ## [2,] 2.0 0.7500000
#################### # # # Exercise 7 # # # #################### matrix1<-matrix(sample(1:1000,9),nrow=3,ncol=3,byrow=TRUE) print("Matrix 1 with random value between 1 and 1000")
## [1] "Matrix 1 with random value between 1 and 1000"
print(matrix1)
## [,1] [,2] [,3] ## [1,] 955 721 532 ## [2,] 578 830 496 ## [3,] 901 799 166
print("Transposed Matrix")
## [1] "Transposed Matrix"
print(t(matrix1))
## [,1] [,2] [,3] ## [1,] 955 578 901 ## [2,] 721 830 799 ## [3,] 532 496 166
#################### # # # Exercise 8 # # # #################### Employees<-data.frame(Name=c("ALAN S","RYAN S","SERAH S", "CHRISTY S","THOMAS MARTIN"), Gender=c("Male","Male","Female","Female","Male"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","CTO"), SSN=c("134-34-2345","349-44-789","556-34-443","898-98-987","679-67-676") ) print(Employees)
## Name Gender Age Designation SSN ## 1 ALAN S Male 23 Clerk 134-34-2345 ## 2 RYAN S Male 22 Manager 349-44-789 ## 3 SERAH S Female 25 Exective 556-34-443 ## 4 CHRISTY S Female 26 CEO 898-98-987 ## 5 THOMAS MARTIN Male 32 CTO 679-67-676
#################### # # # Exercise 9 # # # #################### Employees<-data.frame(Name=c("ALAN S","RYAN S","SERAH S", "CHRISTY S","THOMAS MARTIN"), Gender=c("Male","Male","Female","Female","Male"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","CTO"), SSN=c("134-34-2345","349-44-789","556-34-443","898-98-987","679-67-676") ) print(summary(Employees))
## Name Gender Age Designation SSN ## ALAN S :1 Female:2 Min. :22.0 CEO :1 134-34-2345:1 ## CHRISTY S :1 Male :3 1st Qu.:23.0 Clerk :1 349-44-789 :1 ## RYAN S :1 Median :25.0 CTO :1 556-34-443 :1 ## SERAH S :1 Mean :25.6 Exective:1 679-67-676 :1 ## THOMAS MARTIN:1 3rd Qu.:26.0 Manager :1 898-98-987 :1 ## Max. :32.0
#################### # # # Exercise 10 # # # #################### Employees<-data.frame(Name=c("ALAN S","RYAN S","SERAH S", "CHRISTY S","THOMAS MARTIN"), Gender=c("Male","Male","Female","Female","Male"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","CTO"), SSN=c("134-34-2345","349-44-789","556-34-443","898-98-987","679-67-676") ) print("Employee 1 Details")
## [1] "Employee 1 Details"
print(Employees[1,])
## Name Gender Age Designation SSN ## 1 ALAN S Male 23 Clerk 134-34-2345
print("Employee 5 Details")
## [1] "Employee 5 Details"
print(Employees[5,])
## Name Gender Age Designation SSN ## 5 THOMAS MARTIN Male 32 CTO 679-67-676
print("Name of all employees")
## [1] "Name of all employees"
print(Employees[1])
## Name ## 1 ALAN S ## 2 RYAN S ## 3 SERAH S ## 4 CHRISTY S ## 5 THOMAS MARTIN
Thanks for the exercises.