0.1 A. Creating Vectors

In this section, you are expected to be able to shape data in vectors, perform basic mathematical operations, and also manipulate vectors.

0.1.1 Exercise 1

Create a vector A containing numeric values, starting from the last 2 digits of your student id up to 30.

A <- c(12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30)
A
##  [1] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

0.1.2 Exercise 2

Create a vector B containing 12 character values; all names of your classmate including yourself.

B <- c("Ardifo","Angel","Kefas","Jeff","Julian","Jocelyn","Nikita","Sherly","Sofia","Vanessa","Lala","Siana")
B
##  [1] "Ardifo"  "Angel"   "Kefas"   "Jeff"    "Julian"  "Jocelyn" "Nikita" 
##  [8] "Sherly"  "Sofia"   "Vanessa" "Lala"    "Siana"

0.1.3 Exercise 3

Create a vector C containing 12 numeric values, random number between 60 and 100.

C <- runif(12, 60, 100)
C
##  [1] 64.31809 99.41352 97.16774 85.45021 69.19936 71.79157 62.15566 85.59065
##  [9] 76.46794 85.47215 70.28781 78.68802

0.2 B. Creating Matrices

In this section, you are expected to be able to shape data in Matrices, perform basic mathematical operations, and also manipulate Matrices.

0.2.1 Exercise 4

Create a matrices M1 order by \(rows \times columns \space (4 \times 4)\) containing 16 numeric values, random number between 60 and 100.

M1 <- runif(16,60,100)
M1
##  [1] 73.22768 89.90501 87.37758 63.63238 90.54779 93.38068 99.59681 72.29616
##  [9] 78.83465 71.08710 60.01049 65.27166 87.56298 93.47375 77.36047 76.96264
M1 <- matrix(c(M1), nrow = 4, ncol = 4)
M1
##          [,1]     [,2]     [,3]     [,4]
## [1,] 73.22768 90.54779 78.83465 87.56298
## [2,] 89.90501 93.38068 71.08710 93.47375
## [3,] 87.37758 99.59681 60.01049 77.36047
## [4,] 63.63238 72.29616 65.27166 76.96264

0.2.2 Exercise 5

Create a matrices M2 order by \(rows \times columns \space (4 \times 4)\) containing 16 numeric values, random number between 30 and 60. Find out the following tasks:

  • 3 * M1, give your opinion about the result.
  • M1 + M2, give your opinion about the result.
  • M1 - M2, give your opinion about the result.
  • M1 * M2, give your opinion about the result.
  • M1 / M2, give your opinion about the result.
  • determinan of M1, give your opinion about the result.
  • invers of M1, give your opinion about the result.
M2 <- runif(16, 30, 60)
M2
##  [1] 47.35183 57.37912 55.19112 37.66094 49.97846 30.38580 30.25718 43.21047
##  [9] 35.84144 39.95684 38.84405 50.12870 45.13034 57.45925 59.46796 58.21389
M2 <- matrix(c(M2), nrow = 4, ncol = 4)
M2
##          [,1]     [,2]     [,3]     [,4]
## [1,] 47.35183 49.97846 35.84144 45.13034
## [2,] 57.37912 30.38580 39.95684 57.45925
## [3,] 55.19112 30.25718 38.84405 59.46796
## [4,] 37.66094 43.21047 50.12870 58.21389
3*M1            #Matrix M1 di kali 3
##          [,1]     [,2]     [,3]     [,4]
## [1,] 219.6830 271.6434 236.5039 262.6890
## [2,] 269.7150 280.1420 213.2613 280.4213
## [3,] 262.1328 298.7904 180.0315 232.0814
## [4,] 190.8972 216.8885 195.8150 230.8879
M1+M2           #Matrix M1 + Matrix M2
##          [,1]     [,2]      [,3]     [,4]
## [1,] 120.5795 140.5262 114.67609 132.6933
## [2,] 147.2841 123.7665 111.04394 150.9330
## [3,] 142.5687 129.8540  98.85455 136.8284
## [4,] 101.2933 115.5066 115.40036 135.1765
M1-M2           #Matrix M1 - Matrix M2
##          [,1]     [,2]     [,3]     [,4]
## [1,] 25.87585 40.56934 42.99321 42.43265
## [2,] 32.52589 62.99488 31.13025 36.01450
## [3,] 32.18646 69.33962 21.16644 17.89251
## [4,] 25.97144 29.08570 15.14295 18.74875
M1*M2           #Matrix M1 x Matrix M2
##          [,1]     [,2]     [,3]     [,4]
## [1,] 3467.465 4525.439 2825.547 3951.747
## [2,] 5158.670 2837.446 2840.416 5370.931
## [3,] 4822.467 3013.519 2331.051 4600.469
## [4,] 2396.455 3123.951 3271.984 4480.295
M1/M2           #Matrix M1 / Matrix M2
##          [,1]     [,2]     [,3]     [,4]
## [1,] 1.546459 1.811736 2.199539 1.940225
## [2,] 1.566859 3.073169 1.779097 1.626783
## [3,] 1.583182 3.291675 1.544908 1.300876
## [4,] 1.689612 1.673117 1.302081 1.322067
det(M1)         #Mencari determinan Matrix M1
## [1] 8494.845
library(matlib)
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl.init' failed, running with 'rgl.useNULL = TRUE'.
inv(M1)         #Mencari Inverse Matrix M1
##           [,1]      [,2]       [,3]      [,4]
## [1,]  1.714650  1.536276 -0.8461913 -2.966109
## [2,] -1.096803 -1.012285  0.5845415  1.889762
## [3,]  1.896410  1.556850 -0.9064361 -3.137335
## [4,] -1.995701 -1.639636  0.9192726  3.350939

0.2.3 Exercise 6

Create a matrix data that is contain the following vectors:

  • B that you has been created in the exercise 2. Name it as a ‘names’ variable
  • C that you has been created in the exercise 3. Name it as a ‘scores’ variable.
Names <- c(B)
matrix(c(Names))
##       [,1]     
##  [1,] "Ardifo" 
##  [2,] "Angel"  
##  [3,] "Kefas"  
##  [4,] "Jeff"   
##  [5,] "Julian" 
##  [6,] "Jocelyn"
##  [7,] "Nikita" 
##  [8,] "Sherly" 
##  [9,] "Sofia"  
## [10,] "Vanessa"
## [11,] "Lala"   
## [12,] "Siana"
Scores <- c(C)
matrix(c(Scores))
##           [,1]
##  [1,] 64.31809
##  [2,] 99.41352
##  [3,] 97.16774
##  [4,] 85.45021
##  [5,] 69.19936
##  [6,] 71.79157
##  [7,] 62.15566
##  [8,] 85.59065
##  [9,] 76.46794
## [10,] 85.47215
## [11,] 70.28781
## [12,] 78.68802
Data <- cbind(Names, Scores)
Data
##       Names     Scores            
##  [1,] "Ardifo"  "64.3180898018181"
##  [2,] "Angel"   "99.4135153200477"
##  [3,] "Kefas"   "97.1677404735237"
##  [4,] "Jeff"    "85.4502072371542"
##  [5,] "Julian"  "69.1993619594723"
##  [6,] "Jocelyn" "71.7915718350559"
##  [7,] "Nikita"  "62.1556605026126"
##  [8,] "Sherly"  "85.5906545929611"
##  [9,] "Sofia"   "76.4679422508925"
## [10,] "Vanessa" "85.4721510969102"
## [11,] "Lala"    "70.2878140285611"
## [12,] "Siana"   "78.6880231183022"

0.3 C. Lists

In this section, you are expected to be able to shape data by using the list() function, perform some basic manipulations.

0.3.1 Exercise 7

Please create a data set as the List variable by using the list() function, contain the following vectors:

  • a variable name, the values including your classmate and yourself
  • a variable age, the values including your classmate and yourself
  • a variable gender, the values including your classmate and yourself
Name <- c("Ardifo","Angel","Kefas","Jeff","Julian","Jocelyn","Nikita","Sherly","Sofia","Vanessa","Lala","Siana")
list(Name)
## [[1]]
##  [1] "Ardifo"  "Angel"   "Kefas"   "Jeff"    "Julian"  "Jocelyn" "Nikita" 
##  [8] "Sherly"  "Sofia"   "Vanessa" "Lala"    "Siana"
Age <- c(19,19,19,19,19,19,19,19,19,18,18,19)
list(Age)
## [[1]]
##  [1] 19 19 19 19 19 19 19 19 19 18 18 19
Gender <- c("Male", "Female", "Male", "Male", "Male", "Female", "Female", "Female", "Female", "Female", "Female", "Female")
list(Gender)
## [[1]]
##  [1] "Male"   "Female" "Male"   "Male"   "Male"   "Female" "Female" "Female"
##  [9] "Female" "Female" "Female" "Female"
list(Name, Age, Gender)
## [[1]]
##  [1] "Ardifo"  "Angel"   "Kefas"   "Jeff"    "Julian"  "Jocelyn" "Nikita" 
##  [8] "Sherly"  "Sofia"   "Vanessa" "Lala"    "Siana"  
## 
## [[2]]
##  [1] 19 19 19 19 19 19 19 19 19 18 18 19
## 
## [[3]]
##  [1] "Male"   "Female" "Male"   "Male"   "Male"   "Female" "Female" "Female"
##  [9] "Female" "Female" "Female" "Female"

0.4 D. Factors

In this section, you are expected to be able to shape data by using the factor() function, perform some basic manipulations.

0.4.1 Exercise 8

Please create a data set as the Factor variable as you have done at Exercise 7. Here, you add one more variable called marital_status by using the factor() function, as the following code:

marital_status <- factor(c("yes","no","yes","no", until 12 students))
Martial_status <- factor(c("Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No"))
factor(Martial_status)
##  [1] Yes No  Yes No  Yes No  Yes No  Yes No  Yes No 
## Levels: No Yes

0.5 E. Data Frames

In this section, you are expected to be able to shape data by using the data.frame() function, perform some basic manipulations.

0.5.1 Exercise 9

Please create a data set as the DF1 variable, contain the following vectors:

  • id, assume 1 up to 6
  • name the values according to your classmate and yourself
  • gender the values according to your classmate and yourself
  • age the values according to your classmate and yourself
  • marital_status the values according to your classmate and yourself
  • address_by_city the values according to your classmate and yourself
Df1 <- data.frame(Id = c (1:6), 
                  Name = c("Siana","Lala","Vanessa","Angel","Nikita","Sherly"),
                  Gender = c("Female","Female","Female","Female","Female","Female"), 
                  Age = c(19, 18, 18, 19, 19, 19),
                  Martial_status = c("Yes","Yes","Yes","Yes","Yes","Yes"),
                  Addres_By_City = c("Tangerang","Tangerang","Manado","Tangerang","Tangerang Selatan","Jakarta"), 
                  stringsAsFactors = F)
Df1
##   Id    Name Gender Age Martial_status    Addres_By_City
## 1  1   Siana Female  19            Yes         Tangerang
## 2  2    Lala Female  18            Yes         Tangerang
## 3  3 Vanessa Female  18            Yes            Manado
## 4  4   Angel Female  19            Yes         Tangerang
## 5  5  Nikita Female  19            Yes Tangerang Selatan
## 6  6  Sherly Female  19            Yes           Jakarta

Please create a data set as the DF2 variable, contain the following vectors:

  • id, assume 7 up to 12
  • name the values according to your classmate and yourself
  • gender the values according to your classmate and yourself
  • age the values according to your classmate and yourself
  • marital_status the values according to your classmate and yourself
  • address_by_city the values according to your classmate and yourself
Df2 <- data.frame(Id = c (7:12), 
                  Name = c("Ardifo","Julian","Irene","Jeff","Kefas","Sofia"),
                  Gender = c("Male","Male","Female","Male","Male","Female"), 
                  Age = c(19, 19, 19, 19, 19, 19),
                  Martial_status = c("No","No","Yes","No","No","Yes"),
                  Addres_By_City = c("Tangerang","Tangerang","Tangerang","Tangerang","Tangerang","Tangerang"), 
                  stringsAsFactors = F)
Df2
##   Id   Name Gender Age Martial_status Addres_By_City
## 1  7 Ardifo   Male  19             No      Tangerang
## 2  8 Julian   Male  19             No      Tangerang
## 3  9  Irene Female  19            Yes      Tangerang
## 4 10   Jeff   Male  19             No      Tangerang
## 5 11  Kefas   Male  19             No      Tangerang
## 6 12  Sofia Female  19            Yes      Tangerang

0.5.2 Exercise 10

In this final exercise, please consider the following tasks:

  • Combine DF1 and DF2, assign it as SB19 variable!
  • Print the result of data frame SB19!
  • Print first 3 rows of the SB19 dataset!
  • How can you preview the SB19 dataset like an Excel file on your Rstudio?
  • Review the structure of the data frame SB19!
  • Check the dimension of the data.
  • Please apply piping functions to the data frame SB19, filter it by their gender accordingly! (as you have learn last week)
SB19 <- rbind(Df1,Df2)
print(SB19)
##    Id    Name Gender Age Martial_status    Addres_By_City
## 1   1   Siana Female  19            Yes         Tangerang
## 2   2    Lala Female  18            Yes         Tangerang
## 3   3 Vanessa Female  18            Yes            Manado
## 4   4   Angel Female  19            Yes         Tangerang
## 5   5  Nikita Female  19            Yes Tangerang Selatan
## 6   6  Sherly Female  19            Yes           Jakarta
## 7   7  Ardifo   Male  19             No         Tangerang
## 8   8  Julian   Male  19             No         Tangerang
## 9   9   Irene Female  19            Yes         Tangerang
## 10 10    Jeff   Male  19             No         Tangerang
## 11 11   Kefas   Male  19             No         Tangerang
## 12 12   Sofia Female  19            Yes         Tangerang
head(SB19, 3)
##   Id    Name Gender Age Martial_status Addres_By_City
## 1  1   Siana Female  19            Yes      Tangerang
## 2  2    Lala Female  18            Yes      Tangerang
## 3  3 Vanessa Female  18            Yes         Manado
#View(SB19)
str(SB19)
## 'data.frame':    12 obs. of  6 variables:
##  $ Id            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Name          : chr  "Siana" "Lala" "Vanessa" "Angel" ...
##  $ Gender        : chr  "Female" "Female" "Female" "Female" ...
##  $ Age           : num  19 18 18 19 19 19 19 19 19 19 ...
##  $ Martial_status: chr  "Yes" "Yes" "Yes" "Yes" ...
##  $ Addres_By_City: chr  "Tangerang" "Tangerang" "Manado" "Tangerang" ...
dim(SB19)
## [1] 12  6
library(dplyr) 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Data <- SB19 %>% 
filter(Gender=="Female") %>% 
print()
##   Id    Name Gender Age Martial_status    Addres_By_City
## 1  1   Siana Female  19            Yes         Tangerang
## 2  2    Lala Female  18            Yes         Tangerang
## 3  3 Vanessa Female  18            Yes            Manado
## 4  4   Angel Female  19            Yes         Tangerang
## 5  5  Nikita Female  19            Yes Tangerang Selatan
## 6  6  Sherly Female  19            Yes           Jakarta
## 7  9   Irene Female  19            Yes         Tangerang
## 8 12   Sofia Female  19            Yes         Tangerang
Data <- SB19 %>% 
filter(Gender=="Male") %>% 
print()
##   Id   Name Gender Age Martial_status Addres_By_City
## 1  7 Ardifo   Male  19             No      Tangerang
## 2  8 Julian   Male  19             No      Tangerang
## 3 10   Jeff   Male  19             No      Tangerang
## 4 11  Kefas   Male  19             No      Tangerang