Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Controle de fluxo no R base
- # -------------------------------------------------
- # for (x in seq) {executa comandos para cada x da seq}
- for (i in 1:10) {print("Olá")}
- x <- c(2, 7, 9)
- for (i in x) {print("Olá")}
- x <- c("Antonio?", "Sou eu!", "E você, qual seu nome?")
- for (i in x) {print(i)}
- # -------------------------------------------------
- # while (cond) {executa comandos enquanto cond=TRUE}
- i <- 5
- while (i > 0) {print("Olá!"); i <- i - 1}
- # repeat {executa comandos até cond break = TRUE}
- x <- 1
- repeat {
- print(x)
- x = x+1
- if (x == 6){
- break
- }
- }
- # -------------------------------------------------
- # if (cond) {executa se cond=TRUE}
- if (1!=0) {print("Executa o comando entre chaves se cond=TRUE")}
- if (1!=1) {print("Executa o comando entre chaves se cond=TRUE")}
- print("Se cond=FALSE, prossegue para o próximo comando!")
- # -------------------------------------------------
- x <- 0
- if (1!=0) {x <- x + 10} else {x <- x - 10}
- x
- x <- 0
- if (1!=1) {x <- x + 10} else {x <- x - 10}
- x
- if (1!=0) {
- print("Executa comandos entre primeiras chaves se cond=TRUE")
- } else {
- print("Executa comandos entre segundas chaves se cond=FALSE")
- }
- if (1!=1) {
- print("Executa comandos entre primeiras chaves se cond=TRUE")
- } else {
- print("Executa comandos entre segundas chaves se cond=FALSE")
- }
- # ---------------------------------------------------------
- # switch (x in vetor) {executa comparando com elementos do vetor}
- sentimentos <- c("triste", "temeroso")
- for (i in sentimentos) {print(switch(i,
- alegre = "Que bom que você está feliz!",
- temeroso = "Não há o que temer neste caso!",
- triste = "Sua tristeza logo se dissipará!",
- raivoso = "Acalme-se, por favor!"
- ))}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement