ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
kan r_ifels
Kan, Hao-Cheng
Lunghwa University of Science and Technology
http://kanchengzxdfgcv.blogspot.tw/
kan r_ifels
kan r_ifels
kan r_ifels
> as.numeric(TRUE)
> as.numeric(FALSE)
> as.character(TRUE)
> as.character(FALSE)
> as.factor(TRUE)
> as.factor(FALSE)
>
> k = "kan"
> class(k)
> as.character(k)
>
> n = 123
> class(n)
> as.numeric(n)
>
> test = as.numeric(k)
> test
> 1 == 1
> 1 < 1
> 1 <= 1
> 1 > 1
> 1 >= 1
> 1 != 1
> toCheck = 1
if (toCheck == 1)
{
print("hello")
}
if (toCheck == 0)
{
print("hello")
}
check.bool = function(x){
if (x == 1){
print("hello")
}else{
print("goodbye")
}
}
> check.bool(1)
> check.bool(0)
> check.bool("k")
> check.bool(TRUE)
check.bool = function(x){
if (x == 1){
print("hello")
} else if (x == 0){
print("goodbye")
} else{
print("confused")
}
}
> check.bool(1)
> check.bool(0)
> check.bool(2)
> check.bool("k")
kan r_ifels
use.switch = function(x){
switch(x,
"a"="first",
"b"="second",
"z"="last",
"c"="third",
"other")
}
> use.switch("a")
> use.switch("b")
> use.switch("c")
> use.switch("d")
> use.switch("OwO")
> use.switch("z")
> use.switch(1)
> use.switch(2)
> use.switch(3)
> use.switch(4)
> use.switch(5)
> use.switch(6)
> is.null(use.switch(6))
kan r_ifels
> ifelse(1 == 1, "Yes", "No")
> ifelse(1 == 0, "Yes", "No")
>
> toTest = c(1, 1, 0, 1, 0, 1)
> class(toTest)
> toTest
>
> ifelse(toTest == 1, "Yes", "No")
> ifelse(toTest == 1, toTest * 3, toTest)
> ifelse(toTest == 1, toTest * 3, "Zero")
>
> toTest[2] = NA
> class(toTest)
> toTest
>
> ifelse(toTest == 1, "Yes", "No")
> ifelse(toTest == 1, toTest * 3, toTest)
> ifelse(toTest == 1, toTest * 3, "Zero")
kan r_ifels
> a = c(1, 1, 0, 1)
> b = c(2, 1, 0, 1)
> bs = c(2, 1, 0, 1)
>
> ifelse(a == 1 & b == 1, "Yes", "No")
> ifelse(a == 1 && b == 1, "Yes", "No")
> ifelse(a == 1 && bs == 1, "Yes", "No")
ifelse(a == 1 & b == 1, "Yes", "No") ifelse(a == 1 && b == 1, "Yes", "No")
kan r_ifels
kan r_ifels
squfc = function(xsqu,ysqu){
if( xsqu <= 0 || ysqu <= 0|| is.na(xsqu) || is.na( ysqu) || is.character(xsqu) ||is.character(ysqu)){
print(NA)
}else{
squmatem = xsqu * ysqu
return(squmatem)
}
}
> squfc( 3, 4)
> squfc( 0, 4)
> squfc( 3, 0)
> squfc( 3, NA)
> squfc( NA, 4)
> squfc( 3, "Hi")
> squfc( "Hi", 4)
>
> ww = c(1,NA,3,4,5)
> qq = c(6,7,NA,9,10)
> squfc(ww,qq)
kan r_ifels
kan r_ifels
kan r_ifels

More Related Content

kan r_ifels

  • 2. Kan, Hao-Cheng Lunghwa University of Science and Technology http://kanchengzxdfgcv.blogspot.tw/
  • 6. > as.numeric(TRUE) > as.numeric(FALSE) > as.character(TRUE) > as.character(FALSE) > as.factor(TRUE) > as.factor(FALSE) > > k = "kan" > class(k) > as.character(k) > > n = 123 > class(n) > as.numeric(n) > > test = as.numeric(k) > test
  • 7. > 1 == 1 > 1 < 1 > 1 <= 1 > 1 > 1 > 1 >= 1 > 1 != 1 > toCheck = 1 if (toCheck == 1) { print("hello") } if (toCheck == 0) { print("hello") }
  • 8. check.bool = function(x){ if (x == 1){ print("hello") }else{ print("goodbye") } } > check.bool(1) > check.bool(0) > check.bool("k") > check.bool(TRUE)
  • 9. check.bool = function(x){ if (x == 1){ print("hello") } else if (x == 0){ print("goodbye") } else{ print("confused") } } > check.bool(1) > check.bool(0) > check.bool(2) > check.bool("k")
  • 11. use.switch = function(x){ switch(x, "a"="first", "b"="second", "z"="last", "c"="third", "other") } > use.switch("a") > use.switch("b") > use.switch("c") > use.switch("d") > use.switch("OwO") > use.switch("z") > use.switch(1) > use.switch(2) > use.switch(3) > use.switch(4) > use.switch(5) > use.switch(6) > is.null(use.switch(6))
  • 13. > ifelse(1 == 1, "Yes", "No") > ifelse(1 == 0, "Yes", "No") > > toTest = c(1, 1, 0, 1, 0, 1) > class(toTest) > toTest > > ifelse(toTest == 1, "Yes", "No") > ifelse(toTest == 1, toTest * 3, toTest) > ifelse(toTest == 1, toTest * 3, "Zero") > > toTest[2] = NA > class(toTest) > toTest > > ifelse(toTest == 1, "Yes", "No") > ifelse(toTest == 1, toTest * 3, toTest) > ifelse(toTest == 1, toTest * 3, "Zero")
  • 15. > a = c(1, 1, 0, 1) > b = c(2, 1, 0, 1) > bs = c(2, 1, 0, 1) > > ifelse(a == 1 & b == 1, "Yes", "No") > ifelse(a == 1 && b == 1, "Yes", "No") > ifelse(a == 1 && bs == 1, "Yes", "No") ifelse(a == 1 & b == 1, "Yes", "No") ifelse(a == 1 && b == 1, "Yes", "No")
  • 18. squfc = function(xsqu,ysqu){ if( xsqu <= 0 || ysqu <= 0|| is.na(xsqu) || is.na( ysqu) || is.character(xsqu) ||is.character(ysqu)){ print(NA) }else{ squmatem = xsqu * ysqu return(squmatem) } }
  • 19. > squfc( 3, 4) > squfc( 0, 4) > squfc( 3, 0) > squfc( 3, NA) > squfc( NA, 4) > squfc( 3, "Hi") > squfc( "Hi", 4) > > ww = c(1,NA,3,4,5) > qq = c(6,7,NA,9,10) > squfc(ww,qq)