Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 17, 2012 15:29
Show Gist options
  • Save geofferyzh/2406809 to your computer and use it in GitHub Desktop.
Save geofferyzh/2406809 to your computer and use it in GitHub Desktop.
RinAction - R Flow Control
###################################################
# ----------------Flow Control -------------------#
# For Looping
# WHILE Looping
# IF-ELSE Conditional Execution
# IFELSE Conditional Execution
# SWITCH Conditional Execution
###################################################
# For Looping
for (i in 1:10) print("Hello")
# WHILE Looping
i<-10
while(i>0) {print("Hello"); i<-i-1}
# IF-ELSE Conditional Execution
if (is.character(grade)) {grade<-as.factor(grade)}
if (!is.factor(grade)) {grade <- as.factor(grade) else print("Grade already is a factor")}
# IFELSE Conditional Execution
ifelse(score>0.5, print("Passed"), print("Failed"))
# SWITCH Conditional Execution
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"
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment