Skip to content

Instantly share code, notes, and snippets.

@prasoonsharma
Created December 2, 2010 11:09
Show Gist options
  • Save prasoonsharma/725143 to your computer and use it in GitHub Desktop.
Save prasoonsharma/725143 to your computer and use it in GitHub Desktop.
Loops and conditions in R
# 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