R Language 流程控制 if(cond)statement if-else if(cond)statement1 else statement2 ifelse if(cond,statement1,statement2) if(FALSE) 一个表达式,不能是NA X message("This won't execute...") }else +{ message("and you'll get an error before you reach this." +} and you'll get an error before you reach this. ifelse(rbinom(10,1,0.5),"Head","Tail") [1]"Tail""Head""Head""Head""Tail""Head""Head""Tail" [9]"Tail""Tail
if-else if(cond) statement if(cond) statement1 else statement2 ifelse if(cond, statement1, statement2) Ӟӻᤒᬡୗ҅ӧᚆฎNA R Language 流程控制
R Language 流程控制 switch switch(expr,... > feelings <-c("sad","afraid") > for (i in feelings) print( switch(i, happy ="I am glad you are happy", + afraid "There is nothing to fear", sad "Cheer up", angry ="Calm down now" ) +) [1]"Cheer up" [1]"There is nothing to fear
switch switch(expr, …) R Language 流程控制
R Language 循环控制 repeat repeat(statement) for for(var in seq)statement while while(cond)statement > repeat + Happy Groundhog Day! + message("Happy Groundhog Day!") action Make an ice statue + action <-sample( Happy Groundhog Day! c( action Rob a bank "Learn French", Happy Groundhog Day! "Make an ice statue", action Win heart of Andie McDowell "Rob a bank", "Win heart of Andie McDowell" + 1 break + + message("action ="action) if(action =="Win heart of Andie McDowell")break next +
repeat repeat(statement) for for(var in seq) statement while while(cond) statement break next R Language 循环控制