Created
April 17, 2012 15:29
-
-
Save geofferyzh/2406809 to your computer and use it in GitHub Desktop.
RinAction - R Flow Control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################### | |
# ----------------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