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] 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Create a vector B containing 12 character values; all names of your classmate including yourself.
B <- c("sherly","kefas","jeffry","julian","vanessa","angel","nikita","ardifo","siana","lala","fallen","jocelyn")
B## [1] "sherly" "kefas" "jeffry" "julian" "vanessa" "angel" "nikita"
## [8] "ardifo" "siana" "lala" "fallen" "jocelyn"
Create a vector C containing 12 numeric values, random number between 60 and 100.
## [1] 74.17966 77.20349 85.75552 91.68635 63.67599 88.29406 98.92189 64.17309
## [9] 66.66394 88.03700 91.59677 96.42593
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,] 60.29262 75.61034 67.63119 71.84788
## [2,] 85.93878 99.48133 69.38620 71.81203
## [3,] 91.20837 77.05708 85.58754 90.72147
## [4,] 99.86933 73.96220 66.65561 77.23401
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.## [,1] [,2] [,3] [,4]
## [1,] 54.35386 32.53674 52.42905 47.45018
## [2,] 39.63872 43.24592 37.28960 48.14137
## [3,] 36.36820 44.06140 43.81385 52.34423
## [4,] 49.70138 48.26752 49.55326 30.76793
## [,1] [,2] [,3] [,4]
## [1,] 180.8778 226.8310 202.8936 215.5436
## [2,] 257.8164 298.4440 208.1586 215.4361
## [3,] 273.6251 231.1712 256.7626 272.1644
## [4,] 299.6080 221.8866 199.9668 231.7020
## [,1] [,2] [,3] [,4]
## [1,] 114.6465 108.1471 120.0602 119.2981
## [2,] 125.5775 142.7273 106.6758 119.9534
## [3,] 127.5766 121.1185 129.4014 143.0657
## [4,] 149.5707 122.2297 116.2089 108.0019
## [,1] [,2] [,3] [,4]
## [1,] 5.938754 43.07360 15.20214 24.39770
## [2,] 46.300065 56.23541 32.09660 23.67066
## [3,] 54.840174 32.99568 41.77369 38.37724
## [4,] 50.167942 25.69468 17.10236 46.46608
## [,1] [,2] [,3] [,4]
## [1,] 3277.137 2460.114 3545.839 3409.195
## [2,] 3406.503 4302.162 2587.383 3457.130
## [3,] 3317.085 3395.243 3749.920 4748.746
## [4,] 4963.644 3569.972 3303.003 2376.330
## [,1] [,2] [,3] [,4]
## [1,] 1.109261 2.323845 1.289956 1.514175
## [2,] 2.168051 2.300363 1.860739 1.491691
## [3,] 2.507915 1.748857 1.953436 1.733170
## [4,] 2.009387 1.532339 1.345131 2.510212
## [1] -444793
## [,1] [,2] [,3] [,4]
## [1,] -0.05152178 0.01820845 0.02123411 0.00605634
## [2,] 0.02482237 0.01580245 -0.03520345 0.00356666
## [3,] -0.15099149 0.08646208 0.16573403 -0.13460695
## [4,] 0.17316152 -0.11329765 -0.13677933 0.11787124
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.## names scores
## [1,] "sherly" "74.1796553414315"
## [2,] "kefas" "77.2034906595945"
## [3,] "jeffry" "85.7555165607482"
## [4,] "julian" "91.6863452177495"
## [5,] "vanessa" "63.6759854387492"
## [6,] "angel" "88.2940642070025"
## [7,] "nikita" "98.9218910224736"
## [8,] "ardifo" "64.1730893123895"
## [9,] "siana" "66.6639427188784"
## [10,] "lala" "88.0369957443327"
## [11,] "fallen" "91.5967671852559"
## [12,] "jocelyn" "96.425931090489"
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 <- c("sherly","kefas","jeffry","julian","vanessa","angel","nikita","ardifo","siana","lala","fallen","jocelyn")
age <- c(19, 19, 19, 19, 18, 19, 18, 19, 19, 19, 21, 19)
gender <- c("female","male","male","male","female","female","female","male","female","female","male","female")
List <- list(name, age, gender)
List## [[1]]
## [1] "sherly" "kefas" "jeffry" "julian" "vanessa" "angel" "nikita"
## [8] "ardifo" "siana" "lala" "fallen" "jocelyn"
##
## [[2]]
## [1] 19 19 19 19 18 19 18 19 19 19 21 19
##
## [[3]]
## [1] "female" "male" "male" "male" "female" "female" "female" "male"
## [9] "female" "female" "male" "female"
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
## [[1]]
## [1] "sherly" "kefas" "jeffry" "julian" "vanessa" "angel" "nikita"
## [8] "ardifo" "siana" "lala" "fallen" "jocelyn"
##
## [[2]]
## [1] 19 19 19 19 18 19 18 19 19 19 21 19
##
## [[3]]
## [1] "female" "male" "male" "male" "female" "female" "female" "male"
## [9] "female" "female" "male" "female"
##
## [[4]]
## [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 yourselfDF1 <- data.frame(id = c(1:6),
name = c("angel","julian","vanessa","sherly","jeffry","jocelyn"),
gender = c("female","male","female","female","male","female"),
age = c(19,20,18,19,19,19),
marital_status = c("yes","no","yes","no","yes","no"),
address_by_city = c ("tangerang","tangerang","maluku","jakarta","tangerang","tangerang"),stringsAsFactors = F)
DF1 ## id name gender age marital_status address_by_city
## 1 1 angel female 19 yes tangerang
## 2 2 julian male 20 no tangerang
## 3 3 vanessa female 18 yes maluku
## 4 4 sherly female 19 no jakarta
## 5 5 jeffry male 19 yes tangerang
## 6 6 jocelyn female 19 no tangerang
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 = c(7:12),
name = c("kefas","nikita","ardifo","fallen","ayu","siana"),
gender = c("male","female","male","male","female","female"),
age = c(19,19,19,21,20,19),
marital_status = c("yes","no","yes","no","yes","no"),
address_by_city = c ("tangerang","tangerang","kaltim","jakarta","tangerang","tangerang"),stringsAsFactors = F)
DF2## id name gender age marital_status address_by_city
## 1 7 kefas male 19 yes tangerang
## 2 8 nikita female 19 no tangerang
## 3 9 ardifo male 19 yes kaltim
## 4 10 fallen male 21 no jakarta
## 5 11 ayu female 20 yes tangerang
## 6 12 siana female 19 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)## id name gender age marital_status address_by_city
## 1 1 angel female 19 yes tangerang
## 2 2 julian male 20 no tangerang
## 3 3 vanessa female 18 yes maluku
## 4 4 sherly female 19 no jakarta
## 5 5 jeffry male 19 yes tangerang
## 6 6 jocelyn female 19 no tangerang
## 7 7 kefas male 19 yes tangerang
## 8 8 nikita female 19 no tangerang
## 9 9 ardifo male 19 yes kaltim
## 10 10 fallen male 21 no jakarta
## 11 11 ayu female 20 yes tangerang
## 12 12 siana female 19 no tangerang
## id name gender age marital_status address_by_city
## 1 1 angel female 19 yes tangerang
## 2 2 julian male 20 no tangerang
## 3 3 vanessa female 18 yes maluku
## [1] "data.frame"
## 'data.frame': 12 obs. of 6 variables:
## $ id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ name : chr "angel" "julian" "vanessa" "sherly" ...
## $ gender : chr "female" "male" "female" "female" ...
## $ age : num 19 20 18 19 19 19 19 19 19 21 ...
## $ marital_status : chr "yes" "no" "yes" "no" ...
## $ address_by_city: chr "tangerang" "tangerang" "maluku" "jakarta" ...
## [1] 12 6
Piping
Filter (untuk memunculkan data yang sesuai dengan beberapa argumen / variable values)
##
## 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 address_by_city
## 1 2 julian male 20 no tangerang
## 2 5 jeffry male 19 yes tangerang
## 3 7 kefas male 19 yes tangerang
## 4 9 ardifo male 19 yes kaltim
## 5 10 fallen male 21 no jakarta
## id name gender age marital_status address_by_city
## 1 1 angel female 19 yes tangerang
## 2 3 vanessa female 18 yes maluku
## 3 4 sherly female 19 no jakarta
## 4 6 jocelyn female 19 no tangerang
## 5 8 nikita female 19 no tangerang
## 6 11 ayu female 20 yes tangerang
## 7 12 siana female 19 no tangerang
Karena yang diminta hanya filter gender, jadi bisa menggunakan subset juga.
Subset (untuk memunculkan data dengan argumen tertentu)
## id name gender age marital_status address_by_city
## 2 2 julian male 20 no tangerang
## 5 5 jeffry male 19 yes tangerang
## 7 7 kefas male 19 yes tangerang
## 9 9 ardifo male 19 yes kaltim
## 10 10 fallen male 21 no jakarta
## id name gender age marital_status address_by_city
## 1 1 angel female 19 yes tangerang
## 3 3 vanessa female 18 yes maluku
## 4 4 sherly female 19 no jakarta
## 6 6 jocelyn female 19 no tangerang
## 8 8 nikita female 19 no tangerang
## 11 11 ayu female 20 yes tangerang
## 12 12 siana female 19 no tangerang