0% found this document useful (0 votes)
22 views

Programming R Compatibility Mode

The document discusses programming in R, including conditions (if/else statements), loops (for, while), functions, and exercises to practice these concepts. It covers comparing and logical operators, finding the maximum of values, defining vectors, using loops and if statements to find the maximum value in each row of a data frame, and creating functions for factorial, combination, summary statistics, and calculating averages. Exercises are provided to create functions for calculating series and formulas.

Uploaded by

Hamidah Midah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Programming R Compatibility Mode

The document discusses programming in R, including conditions (if/else statements), loops (for, while), functions, and exercises to practice these concepts. It covers comparing and logical operators, finding the maximum of values, defining vectors, using loops and if statements to find the maximum value in each row of a data frame, and creating functions for factorial, combination, summary statistics, and calculating averages. Exercises are provided to create functions for calculating series and formulas.

Uploaded by

Hamidah Midah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming R

Zulhanif, M.Sc
Scope
• Kondisi
• Looping
• Function
Kondisi
• Comparison operators: == (equal), != (not
equal), >= (greater than or equal), etc.
Logical operators: &(and) ,| (or), !(not)
• If Statement
Syntax
if (cond1=true) { cmd1 } else { cmd2 }
Example
if (1==0) { print(1) } else { print(2) }
• Ifelse Statement

Syntax
ifelse(test, true_value, false_value)

Example
x = 1:10
ifelse(x<5 | x>8, x, 0)
Latihan
• Buatlah program untuk mencari nilai
maksimum dari 2 bilangan sbb: 1 dan 2
• Buatlah program untuk mencari nilai
maksimum dari 3 bilangan sbb: 1,2 dan 3
• Pergunakan Kondisi IF dari kedua soal
diatas
x1=c(1,2)
if (x1[1]>x1[2]){print(x[1])}else
{print(x1[2])}
x1=c(1,2,3)
if ((x1[1]>x1[2])&(x1[1]>x1[3])){print(x1[1])}
if ((x1[2]>x1[1])&(x1[2]>x1[3])){print(x1[2])}
if ((x1[3]>x1[1])&(x1[3]>x1[2])){print(x1[3])}
x1=c(1,2,3)
if ((x1[1]>x1[2])&(x1[1]>x1[3]))
{print(x1[1])}else
if((x1[2]>x1[1])&(x1[2]>x1[3])){print(x1[2])}else
{print(x1[3])}
Loops
• For
Syntax
for(variable in sequence) {
statements
}
Example
for (i in 1:10){ print(1)}
While Loops
• while(condition) statements
Syntax
while(condition) statements

Example
z <- 0
while(z<5) {
z <- z+2
print(z)
}
Define Vector
• X=rep(0,k) #k=banyaknya elemen
Latihan
• Pergunakan fungsi loop dan if untuk
mencari nilai maksimum untuk masing-
masing baris dari suatu data frame sbb:
X1 X2 X2
2 4 5
5 6 2
1 2 3

X1 X2 X2 Max
2 4 5 5
5 6 2 6
1 2 3 3
x1=c(2,5,1)
x2=c(4,6,2)
x3=c(5,2,3)
max=c(0,0,0)
y=cbind(x1,x2,x3,max)
for (i in 1:3)
{
if ((y[i,1]>y[i,2])&(y[i,1]>y[i,3]))
{
y[i,4]=y[i,1]
}
if ((y[i,2]>y[i,1])&(y[i,2]>y[i,3]))
{
y[i,4]=y[i,2]
}
if ((y[i,3]>y[i,1])&(y[i,3]>y[i,2]))
{
y[i,4]=y[i,3]
}
}
Latihan
A B C A B C Kat
0.8 0.1 0.1 0.8 0.1 0.1 A
0.4 0.5 0.1 0.4 0.5 0.1 B
0.3 0.5 0.2 0.3 0.5 0.2 B
0.2 0.6 0.2 0.2 0.6 0.2 B
0.1 0.2 0.7 0.1 0.2 0.7 C
0.4 0.5 0.1 0.4 0.5 0.1 B
0.5 0.1 0.4 0.5 0.1 0.4 A
0.2 0.2 0.6 0.2 0.2 0.6 C
0.1 0.2 0.7 0.1 0.2 0.7 C
A=c(0.8,0.4,0.3,0.2,0.1,0.4,0.5,0.2,0.1)
B=c(0.1,0.5,0.5,0.6,0.2,0.5,0.1,0.2,0.2)
C=c(0.1,0.1,0.2,0.2,0.7,0.1,0.4,0.6,0.7)
kat=c(rep(0,9))
y=cbind(A,B,C,kat)
for (i in 1:9)
{
if ((y[i,1]>y[i,2])&(y[i,1]>y[i,3]))
{
y[i,4]="A"
}
if ((y[i,2]>y[i,1])&(y[i,2]>y[i,3]))
{
y[i,4]="B"
}
if ((y[i,3]>y[i,1])&(y[i,3]>y[i,2]))
{
y[i,4]="C"
}
}
y=as.data.frame(y)
Function
• Syntax
myfct <- function(arg1, arg2, ...) {
function_body }
Latihan 1
• Buatlah fungsi faktorial
• Buatlah fungsi kombinasi
• Buatlah fungsi untuk menampilakn rata-
rata, varians, dan median secara
sekaligus
• Buatlah fungsi untuk menghitung rata-rata
ukur
Tugas

• Buatlah fungsi untuk menghitung deret


sbb:

• Buatlah fungsi untuk menghitung rumus


sbb:

You might also like