Created
December 2, 2010 11:09
-
-
Save prasoonsharma/725143 to your computer and use it in GitHub Desktop.
Loops and conditions in R
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
# LOOPS IN R | |
# For | |
i <- 1 | |
for(i in 1:10){ | |
print(paste("value of iterator: ", i)) | |
} | |
# While | |
i <- 1 | |
while(i <= 10){ | |
print(paste("value of iterator: ", i)) | |
i <- i + 1 | |
} | |
# ----------- | |
# CONDITIONS IN R | |
# If | |
age <- 15 | |
if(age < 16){ | |
print("Cannot drive a car alone") | |
} | |
# ifelse: ifelse(test, yes, no) | |
ifelse(age < 16, print("Cannot drive a car alone"), print("Go get your dad's car keys")) | |
# Nested if else loops | |
A<-2 | |
if(A==1){B<-1} else | |
if(A==2){B<-2} else | |
if(A==3){B<-3} | |
A | |
B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment