ff <- c( F, F )
ff
ft <- c( F, T )
ft
tf <- c( T, F )
tf
tt <- c( T, T )
tt
!tt
!ff
ff & tt
ft & tt
ff | tt
ft | tt
xor( tt, ff )
xor( tf, ft )
xor( tf, tt )
xor( ff, ft )
is.na( x )
is.na( NA )
tt && tf
ft && tf
as.logic( tt )
as.logical( tt )
as.logical( tf )
as.logical( c(1,2) )
as.logical( c(1,0) )
as.logical( c(10,0) )
as.logical( c(-10,0) )
as.logical( c( -2.3, -1, 0, 2, 5))
as.logical( c( "T", "TRUE", "True", "ture"))
as.logical( c( "T", "TRUE", "True", "true"))
as.logical( c( "F", "FALSE", "False", "false"))
as.logical( c( "F", "FALSE", "False", "faLse"))
as.logical( c( "F", "FALSE", "FalSe", "faLse"))
as.logical( c( "handsome" ))
is.logical( tt )
is.logical( c )
is.logical( 1 )
is.logical( 1+ )
is.logical( 1+2 )
is.logical( 3 < 5 )
3 < 5
3 > 5
is.logical( 3 > 5 )
is.logical( c( "F", "FALSE", "FalSe", "faLse"))
is.logical( c( "F", "FALSE", "False", "false"))
is.logical( c( "F", "FALSE" ))
is.logical( c( F, FALSE ))
is.logical( c( F, FALSE, False, false) )
is.logical( c( F, FALSE, False ) )
is.logical( c( F, FALSE ) )
is.logical( c( F, FALSE, TRUE ) )
is.logical( c( F, FALSE, TRUE, T ) )
is.logical( c( F, FALSE, TRUE, T, True ) )
is.logical( c( F, FALSE, TRUE, T ) )
is.logical( c( -2, 0, 3))
is.logical( "handsome")
x <- c( -1.2, 0.5, 1.0, 1.3, 2.4, 5, 6.3 )
x
any( 1 < x )
any( 8 < x )
any( 0 > x )
any( -3 > x )
any( 1 < x & x < 5 )
any( 1 < x )
any( 1 < x ) & any( x < 5 )
all( 1 < x )
all( -3 < x )
all( x < 5 )
all( x < 9 )
all( 1 < x & x < 5 )
all( 1 < x ) & all( x < 5 )
all( -3 < x ) & all( x < 9 )
all( -3 < x & x < 9 )
x
which( x > 1 )
which( x > 2 )
which( x > 10 )
which( x < 10 )
which( x < -1 )
which( x < 0 )
which( x < 4 & x > 0 )
which( x < 0 )
class( which( x < 0 ))
class( which( x < 0 ) )
which( x < 0 )
which( x < 1 )
x[ which( x < 1 ) ]
x
which( x < 4 & x > 0 )
x[ which( x < 4 & x > 0 ) ]
x
which( x < 4 & x > 0 )
length( which( x < 4 & x > 0 ) )
length( which( x > 6 ) )
length( which( x > 6 ) )
length( which( x > 5 ) )
length( which( x >= 5 ) )
#條件分支
x <- 1
y <- 4
x > y
x < y
x <= y
x >= y
x == y
x != y
3 > 5
class( 3 > 5 )
class( y > x )
y > x
as.integer( 3 > 5 )
as.integer( 3 < 5 )
2.5 * ( 3 < 5 )
2.5 * ( 3 > 5 )
x <- c( 1.2, -3.4, 5.7, -6, 0, 3 )
x >= 0
as.integer( x >= 0 )
sum( x >= 0 )
table( x >= 0 )
x >= 0
x[ x >= 0 ]
x
x[ x <= 0 ]
x[ x < 0 ]
x[ x > 0 ]
x
x <- c( 1.2, -3.4, 5.7, -6, 0, 3 )
y <- c( 2.2, -4.4, 6.6, -8.8, 0, 3.3 )
x
y
x < y
sum( x < y )
table( x <  y )
x[ x < y ]
x[ x > y ]
x[ x = y ]
x[ x == y ]
x
y
x
y
x[ x == x ]
x <- 0.5 - 0.3
y <- 0.3 - 0.1
x
y
x == y
sprintf("%.20f", x)
sprintf("%.20f", y)
all.equal( x, y )
identical( all.equal( x, y ), TRUE )
round( x, 10 ) == round( y, 10 )
a <- 1:10/16
a
sprintf("%.20f", a)
a <- 1:10/10
a
sprintf("%.20f", a)
# if else
x <- "handsome"
y <- 2
z <- 2
if( x == "handsome" ) { y <- y + 3 }
y
if( x == "handsome" ) { y <- y + 3 }
y
if( x == "beautiful" ) { z <- z + 3 }
z
x == "beautiful"
x == "handsome"
x <- 2.5; y <- 4.7
x
y
if( x < y ) { z1 <- x } else { z1 <- y }
z1
y <- 1.3
if( x < y ) { z1 <- x } else { z1 <- y }
z1
min( x, y )
max( x, y )
if( x > y ) { z2 <- x } else { z2 <- y }
z2
y <- 4.7
if( x > y ) { z2 <- x } else { z2 <- y }
z2
ifelse( x > 0, x, -x )
x
ifelse( x > 0, x, -x )
z2
x < - -3.5
x <- -3.5
ifelse( x > 0, x, -x )
abs( x )
x <- 3
switch( x, 2+2, mean(1:10), 1:5 )
x <- 2
switch( x, 2+2, mean(1:10), 1:5 )
x <- 1
switch( x, 2+2, mean(1:10), 1:5 )
switch( x, 2+2, mean(1:10), 1:5 )
x <- 6
switch( x, 2+2, mean(1:10), 1:5 )
switch( 4, 2+2, mean(1:10), 1:5 )
switch( -1, 2+2, mean(1:10), 1:5 )
switch( 3, 2+2, mean(1:10), 1:5 )
switch( 2, 2+2, mean(1:10), 1:5 )
y <- "fruit"
y
switch( y, fruit = "banana", vegetable = "broccoli")
y <- "vegetable"
switch( y, fruit = "banana", vegetable = "broccoli")
y <- "meat"
switch( y, fruit = "banana", vegetable = "broccoli")
switch( y, fruit = "banana", vegetable = "broccoli", "Neither")
switch( y, fruit = "banana", vegetable = "broccoli", meat = "pork")
switch( y, fruit = "banana", vegetable = "broccoli", meat = "pork", "Non")
y <- "food"
switch( y, fruit = "banana", vegetable = "broccoli", meat = "pork", "Non")
#迴圈
x <- 0.2
for( k in 2:5 ) { x[k] <- 4*x[k-1] *( 1 - x[k-1] ) }
x
round(x,4)
x <- 1:10
x
seq( from=1, to=9, by =2)
odd <- seq( from=1, to=9, by =2)
seq( from=2, to=10, by =2)
even <- seq( from=2, to=10, by =2)
odd
even
odd.sum <- 0
sum.odd <- 0
sum.even <- 0
for( i in odd ) { sum.odd <- sum.odd + x[i] }
sum.odd
for( i in even ) { sum.even <- sum.even + x[i] }
sum.even
sum( odd)
sum( even )
for( i in  ) { sum.even <- sum.even + x[i] }
sum.x
sum.x <- 0
for( i in x ) { sum.x <- sum.x + x[i] }
sum.x
mysum <- mysum.odd <- mysum.even <- 0
mysum
mysum.odd
mysum.even
x <- 1:10
odd <- seq( from = 1, to = 9, by = 2 )
even <- seq( from = 2, to = 10, by = 2 )
mysum <- mysum.odd <- mysum.even <- 0
for( i in  x ) { mysum   <- mysum  + x[i]  }
for( i in  odd ) { mysum. odd   <- mysum. odd  + x[i]  }
for( j in even ) { mysum.even <- mysum.even + x[j]  }
x <- 1:10
odd <- seq( from = 1, to = 9, by = 2 )
even <- seq( from = 2, to = 10, by = 2 )
mysum <- mysum.odd <- mysum.even <- 0
for( i in  x ) { mysum   <- mysum  + x[i]  }
for( i in  odd ) { mysum.odd   <- mysum.odd  + x[i]  }
for( j in even ) { mysum.even <- mysum.even + x[j]  }
mysum
mysum.odd
mysum.even
sum( x)
sum( x[odd])
sum( odd)
sum( even )
mysum.even
even
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
x
total <- x[1]
total
count <- 0
while( total <= 12 ) { count = count + 1 ; total <- total + x[ count + 1 ] }
count
total
total
while( total <= 12 ) { count = count + 1 ; total <- total + x[ count + 1 ] }
total
count
total <- x[1]
count <- 0
total
count
x
y <- cumsum( x )
y
sum( y <= 12 )
( y <= 12 )
total
count
count
while( total <= 12 ) { count = count + 1 ; total <- total + x[ count + 1 ] }
count
x
y
count <- 1
total <- x[1]
while( total <= 12 ) { count = count + 1 ; total <- total + x[ count + 1 ] }
count
total
y
count <- 1
total <- x[1]
while( total <= 12 ) { count = count + 1 ; total <- total + x[ count ] }
total
count
x
while( total + x[count+1] <= 12 ) { count = count + 1 ; total <- total + x[ count ] }
total
count
count <- 1
total <- x[1]
while( total + x[count+1] <= 12 ) { count = count + 1 ; total <- total + x[ count ] }
total
count
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
total <- x[ 1 ]
count <- 1
while (  total <= 12      ) {
count <- count + 1
total <- total + x[ count  ]
}
count; total
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
total <- x[ 1 ]
count <- 1
while (  total <= 12      ) {
count <- count + 1
total <- total + x[ count  ]
}
count; total
total <- x[ 1 ]
count <- 1
while( total + x[count+1] <= 12 ) { count = count + 1 ; total <- total + x[ count ] }
count; total
y
sum( y <= 12)
?repeat
()
help( repeat "repeat")
help( "repeat")
while( total + x[count+1] <= 12 ) { count = count + 1 ; total <- total + x[ count ] }
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
total <- x[ 1 ]
count <- 1
Repeat{ count <- count + 1;  total <- total + x[ count]
if ( total + x[count+1] <= 12 ) break
}
count; total
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
total <- x[ 1 ]
count <- 1
repeat{ count <- count + 1;  total <- total + x[ count]
if ( total + x[count+1] <= 12 ) break
}
count; total
x
y
x <- c( 1.2, 3.4, 2.1, 4.3, 3.2, 5.5, 6.7 )
total <- x[ 1 ]
count <- 1
repeat{ count <- count + 1;  total <- total + x[ count ]
if ( total + x[ count + 1 ] >= 12 ) break
}
count; total
savehistory("L:/Data/Teaching/MyCourse/105-1_Programming/Code/data_161030_U07_Logical-Control.Rhistory")
