In this section, you are expected to be able to shape data in vectors, perform basic mathematical operations, and also manipulate vectors.
Create a vector A containing numeric values, starting from the last 2 digits of your student id up to 30.
## [1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## [26] 28 29 30
Create a vector B containing 12 character values; all names of your classmate including yourself.
B<-c("Putri", "Nikita", "Jocelyn", "Ardifo", "Jeffry", "Vanessa", "Sherly", "Kefas", "Julian", "Lala", "Siana", "Fallen")
B## [1] "Putri" "Nikita" "Jocelyn" "Ardifo" "Jeffry" "Vanessa" "Sherly"
## [8] "Kefas" "Julian" "Lala" "Siana" "Fallen"
In this section, you are expected to be able to shape data in Matrices, perform basic mathematical operations, and also manipulate Matrices.
Create a matrices M1 order by \(rows \times columns \space (4 \times 4)\) containing 16 numeric values, random number between 60 and 100.
## [,1] [,2] [,3] [,4]
## [1,] 121.8007 189.3466 198.5069 191.1880
## [2,] 125.3152 162.0745 142.6238 160.7238
## [3,] 154.0121 175.3286 142.4218 170.2167
## [4,] 108.6694 187.1693 101.9369 176.6635
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.M1, give your opinion about the result.M1, give your opinion about the result.library(matlib)
b<-runif(16, 30, 60)
M2<-matrix(b, 4, 4)
# `3 * M1`, This function will result the matrix A multiplication by 3 for its each element.
# `M1 + M2`, This function will result the addition of M1 and M2.
# `Mi - M2`, This function will result the M1 subtract by M2.
# `Mi * M2`, This function will result the multiplication between M1 and M2.
# `M1/M2`, This function will result the M1 division by M2.
# determinant of `M1`, this function will give you the result of M1 determinant
det(M1)## [1] 1144635
## [,1] [,2] [,3] [,4]
## [1,] -0.01741804 0.02193424 0.00999684 -0.01073714
## [2,] 0.19917111 -0.64251033 0.32771209 0.05323969
## [3,] 0.04200437 -0.09729302 0.04861606 -0.00378511
## [4,] -0.22453820 0.72336606 -0.38140172 -0.04195658
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’ variableC that you has been created in the exercise 3. Name it as a ‘scores’ variable.## [,1] [,2] [,3]
## [1,] "Putri" "Nikita" "Jocelyn"
## [2,] "92.5563990417868" "76.8769671302289" "94.0822895243764"
## [,4] [,5] [,6]
## [1,] "Ardifo" "Jeffry" "Vanessa"
## [2,] "73.0529234092683" "92.8504252899438" "93.6884501669556"
## [,7] [,8] [,9] [,10]
## [1,] "Sherly" "Kefas" "Julian" "Lala"
## [2,] "99.7973069828004" "99.3992507178336" "61.833117865026" "73.4648727718741"
## [,11] [,12]
## [1,] "Siana" "Fallen"
## [2,] "77.9740542266518" "90.0629026535898"
# In my opinion, the matrix will look better if we make it this way
data<-matrix(Scores, 1, 12, dimnames = list("Score", Names))
data## Putri Nikita Jocelyn Ardifo Jeffry Vanessa Sherly Kefas
## Score 92.5564 76.87697 94.08229 73.05292 92.85043 93.68845 99.79731 99.39925
## Julian Lala Siana Fallen
## Score 61.83312 73.46487 77.97405 90.0629
In this section, you are expected to be able to shape data by using the list() function, perform some basic manipulations.
Please create a data set as the List variable by using the list() function, contain the following vectors:
name, the values including your classmate and yourselfage, the values including your classmate and yourselfgender, the values including your classmate and yourselfname<-B
age<-c(19, 19, 19, 19, 19, 18, 19, 18, 15, 20, 23, 24)
gender<-c("female", "female", "female", "male", "male", "female", "female", "male", "male", "female", "female", "male")
List<-list(name, age, gender)
List## [[1]]
## [1] "Putri" "Nikita" "Jocelyn" "Ardifo" "Jeffry" "Vanessa" "Sherly"
## [8] "Kefas" "Julian" "Lala" "Siana" "Fallen"
##
## [[2]]
## [1] 19 19 19 19 19 18 19 18 15 20 23 24
##
## [[3]]
## [1] "female" "female" "female" "male" "male" "female" "female" "male"
## [9] "male" "female" "female" "male"
In this section, you are expected to be able to shape data by using the factor() function, perform some basic manipulations.
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", "yes", "no", "yes", "no", "yes", "no", "yes", "no"))
marital_status## [1] yes no yes no yes no yes no yes no yes no
## Levels: no yes
In this section, you are expected to be able to shape data by using the data.frame() function, perform some basic manipulations.
Please create a data set as the DF1 variable, contain the following vectors:
id, assume 1 up to 6name the values according to your classmate and yourselfgender the values according to your classmate and yourselfage the values according to your classmate and yourselfmarital_status the values according to your classmate and yourselfaddress_by_city the values according to your classmate and yourselfaddress<-c("Tigaraksa","Jakarta", "Tangerang", "Kalimantan", "Tangerang", "Tobelo", "Jakarta", "Tangerang", "Tangerang", "Tangerang", "Tangerang", "Tangerang", "Tangerang")
DF1<-data.frame(id=1:6, name=head(B, n = 6), gender=head(gender, n=6), age=head(age, n=6), marital_status=head(marital_status, n=6), addres_by_city=head(address, n=6), stringsAsFactors = F)
DF1## id name gender age marital_status addres_by_city
## 1 1 Putri female 19 yes Tigaraksa
## 2 2 Nikita female 19 no Jakarta
## 3 3 Jocelyn female 19 yes Tangerang
## 4 4 Ardifo male 19 no Kalimantan
## 5 5 Jeffry male 19 yes Tangerang
## 6 6 Vanessa female 18 no Tobelo
Please create a data set as the DF2 variable, contain the following vectors:
id, assume 7 up to 12name the values according to your classmate and yourselfgender the values according to your classmate and yourselfage the values according to your classmate and yourselfmarital_status the values according to your classmate and yourselfaddress_by_city the values according to your classmate and yourselfDF2<- data.frame(id = 7:12, name = tail(B, n=6), gender=tail(gender, n=6), age=tail(age, n=6), marital_status=tail(marital_status, n=6), addres_by_city=tail(address, n=6), stringsAsFactors = F)
DF2## id name gender age marital_status addres_by_city
## 1 7 Sherly female 19 yes Tangerang
## 2 8 Kefas male 18 no Tangerang
## 3 9 Julian male 15 yes Tangerang
## 4 10 Lala female 20 no Tangerang
## 5 11 Siana female 23 yes Tangerang
## 6 12 Fallen male 24 no Tangerang
In this final exercise, please consider the following tasks:
DF1 and DF2, assign it as SB19 variable!SB19!SB19 dataset!SB19 dataset like an Excel file on your Rstudio?SB19!SB19, filter it by their gender accordingly! (as you have learn last week)##
## 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
## id name gender age marital_status addres_by_city
## 1 1 Putri female 19 yes Tigaraksa
## 2 2 Nikita female 19 no Jakarta
## 3 3 Jocelyn female 19 yes Tangerang
## 4 4 Ardifo male 19 no Kalimantan
## 5 5 Jeffry male 19 yes Tangerang
## 6 6 Vanessa female 18 no Tobelo
## 7 7 Sherly female 19 yes Tangerang
## 8 8 Kefas male 18 no Tangerang
## 9 9 Julian male 15 yes Tangerang
## 10 10 Lala female 20 no Tangerang
## 11 11 Siana female 23 yes Tangerang
## 12 12 Fallen male 24 no Tangerang
## id name gender age marital_status addres_by_city
## 1 1 Putri female 19 yes Tigaraksa
## 2 2 Nikita female 19 no Jakarta
## 3 3 Jocelyn female 19 yes Tangerang
## 'data.frame': 12 obs. of 6 variables:
## $ id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ name : chr "Putri" "Nikita" "Jocelyn" "Ardifo" ...
## $ gender : chr "female" "female" "female" "male" ...
## $ age : num 19 19 19 19 19 18 19 18 15 20 ...
## $ marital_status: Factor w/ 2 levels "no","yes": 2 1 2 1 2 1 2 1 2 1 ...
## $ addres_by_city: chr "Tigaraksa" "Jakarta" "Tangerang" "Kalimantan" ...
## [1] 12 6
# To show all the male gender, you can type the function as below
SB19 %>% filter(gender == "male")%>% print ()## id name gender age marital_status addres_by_city
## 1 4 Ardifo male 19 no Kalimantan
## 2 5 Jeffry male 19 yes Tangerang
## 3 8 Kefas male 18 no Tangerang
## 4 9 Julian male 15 yes Tangerang
## 5 12 Fallen male 24 no Tangerang
# To show all the female gender, you can type the function as below
SB19 %>% filter(gender == "female")%>% print ()## id name gender age marital_status addres_by_city
## 1 1 Putri female 19 yes Tigaraksa
## 2 2 Nikita female 19 no Jakarta
## 3 3 Jocelyn female 19 yes Tangerang
## 4 6 Vanessa female 18 no Tobelo
## 5 7 Sherly female 19 yes Tangerang
## 6 10 Lala female 20 no Tangerang
## 7 11 Siana female 23 yes Tangerang
# To show only people name with male gender, you can type the function with `select` as below
SB19 %>% select(name, gender)%>% filter(gender=="male") %>% print ()## name gender
## 1 Ardifo male
## 2 Jeffry male
## 3 Kefas male
## 4 Julian male
## 5 Fallen male
# To show only people name with female gender, you can type the function with `select` as below
SB19 %>% select(name, gender)%>% filter(gender=="female")%>% print ()## name gender
## 1 Putri female
## 2 Nikita female
## 3 Jocelyn female
## 4 Vanessa female
## 5 Sherly female
## 6 Lala female
## 7 Siana female