ControlStructures.pdf
(
87 KB
)
Pobierz
Introduction to the R Language
Control Structures
Roger Peng, Associate Professor
Johns Hopkins Bloomberg School of Public Health
Control Structures
Control structures in R allow you to control the flow of execution of the program, depending on
runtime conditions. Common structures are
·
if
,
else
: testing a condition
·
for
: execute a loop a fixed number of times
·
while
: execute a loop
while
a condition is true
·
repeat
: execute an infinite loop
·
break
: break the execution of a loop
·
next
: skip an interation of a loop
·
return
: exit a function
Most control structures are not used in interactive sessions, but rather when writing functions or
longer expresisons.
2/14
Control Structures: if
if(<condition>)
{
## do something
}
else
{
## do something else
}
if(<condition1>)
{
## do something
}
else if(<condition2>)
{
## do something different
}
else
{
## do something different
}
3/14
if
This is a valid if/else structure.
if(x
>
3)
{
y <-
10
}
else
{
y <-
0
}
So is this one.
y <-
if(x
>
3)
{
10
}
else
{
0
}
4/14
if
Of course, the else clause is not necessary.
if(<condition1>)
{
}
if(<condition2>)
{
}
5/14
Plik z chomika:
mileymandy
Inne pliki z tego folderu:
Subsetting.pdf
(100 KB)
tapply.pdf
(62 KB)
reading_data_II.pdf
(84 KB)
Scoping.pdf
(185 KB)
reading_data_I.pdf
(101 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 09.04.2026
Pliki dostępne do 19.01.2025
●● BIZNES i PRACA
Absolwentka
Zgłoś jeśli
naruszono regulamin