Syllabus and Book
Practice - R Programming ( Conditions )
R Programming - repeat Loops
R Programming - while Loops
R Programming - for Loops
R Programming - apply groups
QUIZ
Extended Syllabus PDF
PDF - (179 - 185)
TRUE & TRUE
TRUE & FALSE
TRUE | FALSE
!TRUE
2 == 3
5 < 6
c(1,4) >= 6
9 != 8
5 < 6 & 9 != 8
score <- 80
exam_no <- 2
score >= 75 | exam_no == 1
score>=75 & score<90 | exam_no==1
Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;
What do you gonna do if it is Friday.
if (...) {
print('Weekend : Sorry. Lockdown.')
} else {
print('Do some work.')
}
Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;
What do you gonna do if it is Friday.
if ( day... | day... ) {
print('Weekend : Sorry. Lockdown.')
} else {
print('Do some work.')
}
ANSWER : You want to enjoy, and let’s say the day is;
It is okay, you can fun if it is weekend.
if (day == 'Saturday' | day == 'Sunday') {
print('Weekend : Sorry. Lockdown.')
} else {
print('Do some work.')
}## [1] "Do some work."
Problem : You want to go out and your question is
“Should I take an umbrella?”
RADIO: The sky is cloudy and the chance of rain is high.
Your conditions, for two variables
# you want to go out and your question is "Should I take an umbrella?"
sky <-
high_chance_of_rain <-
Your conditions, for two variables
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUE# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif (...) {
print("...")
} else {
print("...")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif (...) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif ( sky... & high_chance_of_rain... ) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUE
if (sky == "cloudy" & high_chance_of_rain == TRUE) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}## [1] "Take umbrella!"
Loops are R’s method for repeating a task, which makes them a useful tool for programming simulations.
repeat
{
message("Happy Free Day!")
break
}
I have 3 COINS and I want to play the game. (So, at first I have 0 GAME)
When I started the repeat-loop I want to
- Reduce (decrease) 1 Coin for each game
- Enhance (increase) 1 Game for each game
I have 3 COINS and I want to play the game. (So, at first I have 0 GAME)
When I spend my all coins, I want to stop playing game
## Game =1 Nice try, play again
## Game =2 Nice try, play again
## Game =3 Nice try, play again
What if, there is no any break command ?!
# Statements previous to repeat loop
coins <- 3
game <- 0
repeat {
# block of statements
game <- game + 1
coins <- coins -1
message("Game =",game, " Nice try, play again")
# breaking condition
# there is no any break command !
}
# use CTRL + C
While loops are like backward repeat loops.
## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] 10
While loops are like backward repeat loops.
## Game = 1 Nice try, play again
## Game = 2 Nice try, play again
## Game = 3 Nice try, play again
## [1] 0 3
When you know exactly how many times you want the code to repeat.
## [1] 1
## [1] 2
## [1] 3
## [1] 16
When you know exactly how many times you want the code to repeat.
## [1] "a"
## [1] "b"
## [1] "c"
## [1] 16
loop is related with the length of the vector !
## just say it 1 times
## just say it 2 times
When you know exactly how many times you want the code to repeat.
## just say it 1 times
## just say it 2 times
## just say it apple times
## just say it banana times
LOOP is related with the length(vector)
## [1] "January" "February" "March" "April" "May" "June"
## [7] "July" "August" "September" "October" "November" "December"
## The month of January
## The month of February
## The month of March
## The month of April
## The month of May
## The month of June
## The month of July
## The month of August
## The month of September
## The month of October
## The month of November
## The month of December
for (i in 1:10) {
if (i == 2){
print("i don't use i=2")
next
}
print(i)
}
## [1] 1
## [1] "i don't use i=2"
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 1
## [1] "i don't use i=2"
## [1] 3
## [1] 4
## [1] 5
## [1] 106
## [1] 7
## [1] 8
## [1] 9
## [1] 10
apply(X, MARGIN, FUN, ...)
# X is an array or a matrix, dim(X) must have a positive length
# MARGIN=1, it applies over rows, whereas with
# MARGIN=2, it works over columns. Note that when you use the construct
# MARGIN=c(1,2), it applies to both rows and columns
# FUN, which is the function that you want to apply to the data.
apply(my_matrix, MARGIN=1, mean)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2.02005688 -0.7510888 0.85241277 0.5159643 0.8189546
## [2,] -0.05705512 0.3836436 -0.08993873 0.4894480 0.1533690
## [3,] -0.57701045 0.4081727 -1.81256234 0.1610991 -0.6685669
## [1] 0.6912600 0.1758934 -0.4977736
## [1] 0.46199710 0.01357582 -0.35002943 0.38883714 0.10125224
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.8610810 1.4086325 -0.06280369 -0.002602994 0.05864157
## [2,] 0.9518314 -0.7794236 0.68562154 -0.340228385 -0.78159277
## [3,] -0.8031686 -1.1291807 0.72255161 0.221269524 0.70444499
## [,1] [,2] [,3]
## [1,] 0.4525897 -0.05275835 -0.05681664
## [2,] 2.2629484 -0.26379173 -0.28408318
## col1 col2 col3
## a -0.2826586 0.6926466 0.05438504
## b 1.0349155 -1.2255437 -0.06929384
## c 1.6882855 -0.3319602 0.68032112
## d -0.4779836 0.4604776 0.52732802
## e 0.4011958 -0.1248847 0.08651011
## a b c d e
## 1 2 2 1 2
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 4 8 12
## [2,] 5 9 13
## [3,] 6 10 14
## [4,] 7 11 15
##
## [[3]]
## [,1] [,2]
## [1,] 8 8
## [2,] 9 9
## [3,] 10 10
## [[1]]
## [1] 4 5 6
##
## [[2]]
## [1] 8 9 10 11
##
## [[3]]
## [1] 8 9 10
## [[1]]
## [1] 1 4 7
##
## [[2]]
## [1] 4 8 12
##
## [[3]]
## [1] 8 8
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 4 8 12
## [2,] 5 9 13
## [3,] 6 10 14
## [4,] 7 11 15
##
## [[3]]
## [,1] [,2]
## [1,] 8 8
## [2,] 9 9
## [3,] 10 10
## [1] 1 4 8
## [,1] [,2] [,3]
## [1,] 1 4 8
## [2,] 2 5 9
## [3,] 3 6 10
Conditions and Loops, DataCamp