작성일자 : 2023-09-29
Ver 0.1.1
if() function
- if( cond ) expr
- if( cond ) cond.expr else alt.expr
x = 1:5
x
## [1] 1 2 3 4 5
y = -2:2
y
## [1] -2 -1 0 1 2
if ( any( x < 0 ) ) print( x ) # print() is not executed
if ( any( y < 0 ) ) print( abs( y ) ) # print() is executed
## [1] 2 1 0 1 2
if ( any( y < 0 ) ) {
print( abs( y ) )
print( 'y contains negative values' )
}
## [1] 2 1 0 1 2
## [1] "y contains negative values"
if ( pi > 3 ) print( 'expr if statement' ) else print( 'expre else statement' )
## [1] "expr if statement"
if( pi < 3 ) print( 'expr if statement' ) else print( 'expr else statement' )
## [1] "expr else statement"
# multiple conditions
if ( length( x ) == 5 ) {
if ( sum( x ) == 15 ) print( 'Vector x length=5, sum=15' )
} else {
print( 'Vector x length!=5' )
}
## [1] "Vector x length=5, sum=15"
# alternative form of multiple conditions
if( length( x ) == 5 && sum( x ) == 15 ) {
print( 'Vector x length=5, sum=15' )
} else {
print( 'Vector x length!=5' )
}
## [1] "Vector x length=5, sum=15"
ifelse() function
- ifelse( test, yes, no )
y = -2:2
ifelse ( y >= 0 , y , -y )
## [1] 2 1 0 1 2
abs( y )
## [1] 2 1 0 1 2
tmp = 3
ifelse ( tmp > 0 , 'positive' , ifelse( tmp < 0 , 'negative' , 'zero' ) )
## [1] "positive"
switch() function
- switch( EXPR , value1 , value2 , ... )
x = 1:10
switch( EXPR = 1 , mean( x ) , median( x ) , sd( x ) , range( x ) )
## [1] 5.5
switch( EXPR = 2 , mean( x ) , median( x ) , sd( x ) , range( x ) )
## [1] 5.5
switch( EXPR = 3 , mean( x ) , median( x ) , sd( x ) , range( x ) )
## [1] 3.02765
switch( EXPR = 4 , mean( x ) , median( x ) , sd( x ) , range( x ) )
## [1] 1 10
switch( EXPR = 5 , mean( x ) , median( x ) , sd( x ) , range( x ) )
x = 1:10
type = 'mean'
switch( EXPR = type ,
MEAN = , mean = mean( x ) ,
MEDIAN = , median = median( x ) ,
SD = , sd = sd( x ) ,
range( x ) )
## [1] 5.5
type = 'SD'
switch( EXPR = type ,
MEAN = , mean = mean( x ) ,
MEDIAN = , median = median( x ) ,
SD = , sd = sd( x ) ,
range( x ) )
## [1] 3.02765
type = 'Sd'
switch( EXPR = type ,
MEAN = , mean = mean( x ) ,
MEDIAN = , median = median( x ) ,
SD = , sd = sd( x ) ,
range( x ) )
## [1] 1 10
Loop statements
- for( var in seq ) expr
- while( cond ) expr
- repeat expr
for ( i in 5:1 ) print( rep( i , i ) )
## [1] 5 5 5 5 5
## [1] 4 4 4 4
## [1] 3 3 3
## [1] 2 2
## [1] 1
sum.x = 0
# sum from 1 to 10
for ( i in 1:10 )
sum.x = sum.x + i
sum.x
## [1] 55
# form a multiplication table
for ( i in 2:9 )
for( j in 1:9 )
cat( i , '*' , j , '=' , i * j , '\n' )
## 2 * 1 = 2
## 2 * 2 = 4
## 2 * 3 = 6
## 2 * 4 = 8
## 2 * 5 = 10
## 2 * 6 = 12
## 2 * 7 = 14
## 2 * 8 = 16
## 2 * 9 = 18
## 3 * 1 = 3
## 3 * 2 = 6
## 3 * 3 = 9
## 3 * 4 = 12
## 3 * 5 = 15
## 3 * 6 = 18
## 3 * 7 = 21
## 3 * 8 = 24
## 3 * 9 = 27
## 4 * 1 = 4
## 4 * 2 = 8
## 4 * 3 = 12
## 4 * 4 = 16
## 4 * 5 = 20
## 4 * 6 = 24
## 4 * 7 = 28
## 4 * 8 = 32
## 4 * 9 = 36
## 5 * 1 = 5
## 5 * 2 = 10
## 5 * 3 = 15
## 5 * 4 = 20
## 5 * 5 = 25
## 5 * 6 = 30
## 5 * 7 = 35
## 5 * 8 = 40
## 5 * 9 = 45
## 6 * 1 = 6
## 6 * 2 = 12
## 6 * 3 = 18
## 6 * 4 = 24
## 6 * 5 = 30
## 6 * 6 = 36
## 6 * 7 = 42
## 6 * 8 = 48
## 6 * 9 = 54
## 7 * 1 = 7
## 7 * 2 = 14
## 7 * 3 = 21
## 7 * 4 = 28
## 7 * 5 = 35
## 7 * 6 = 42
## 7 * 7 = 49
## 7 * 8 = 56
## 7 * 9 = 63
## 8 * 1 = 8
## 8 * 2 = 16
## 8 * 3 = 24
## 8 * 4 = 32
## 8 * 5 = 40
## 8 * 6 = 48
## 8 * 7 = 56
## 8 * 8 = 64
## 8 * 9 = 72
## 9 * 1 = 9
## 9 * 2 = 18
## 9 * 3 = 27
## 9 * 4 = 36
## 9 * 5 = 45
## 9 * 6 = 54
## 9 * 7 = 63
## 9 * 8 = 72
## 9 * 9 = 81
break 과 next
- break
- next
sum.x = 0
for ( i in 1:10 ) {
sum.x = sum.x + i
if ( sum.x > 20 ) break
cat( i , sum.x , '\n' )
}
## 1 1
## 2 3
## 3 6
## 4 10
## 5 15
for ( i in 2:9 ) {
if ( i > 5 ) break
for ( j in 1:9 ) {
if ( j > 1 ) break
cat( i , '*' , j , '=' , i * j , '\n' )
}
}
## 2 * 1 = 2
## 3 * 1 = 3
## 4 * 1 = 4
## 5 * 1 = 5
x = 0
sum.x = 0
while ( x < 10 ) {
x = x + 1
if ( x < 9 ) next
print( x )
sum.x = sum.x + x
}
## [1] 9
## [1] 10
sum.x
## [1] 19