Advertisement
Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.80 KB | None | 0 0
  1. # Controle de fluxo no R base
  2.  
  3. # -------------------------------------------------
  4.  
  5. # for (x in seq) {executa comandos para cada x da seq}
  6.  
  7. for (i in 1:10) {print("Olá")}
  8.  
  9. x <- c(2, 7, 9)
  10. for (i in x) {print("Olá")}
  11.  
  12. x <- c("Antonio?", "Sou eu!", "E você, qual seu nome?")
  13. for (i in x) {print(i)}
  14.  
  15. # -------------------------------------------------
  16.  
  17. # while (cond) {executa comandos enquanto cond=TRUE}
  18.  
  19. i <- 5
  20. while (i > 0) {print("Olá!"); i <- i - 1}
  21.  
  22. # repeat {executa comandos até cond break = TRUE}
  23.  
  24. x <- 1
  25. repeat {
  26.   print(x)
  27.   x = x+1
  28.   if (x == 6){
  29.     break
  30.   }
  31. }
  32.  
  33.  
  34. # -------------------------------------------------
  35.  
  36. # if (cond) {executa se cond=TRUE}
  37.  
  38. if (1!=0) {print("Executa o comando entre chaves se cond=TRUE")}
  39.  
  40. if (1!=1) {print("Executa o comando entre chaves se cond=TRUE")}
  41. print("Se cond=FALSE, prossegue para o próximo comando!")
  42.  
  43. # -------------------------------------------------
  44.  
  45. x <- 0
  46. if (1!=0) {x <- x + 10} else {x <- x - 10}
  47. x
  48.  
  49. x <- 0
  50. if (1!=1) {x <- x + 10} else {x <- x - 10}
  51. x
  52.  
  53. if (1!=0) {
  54.   print("Executa comandos entre primeiras chaves se cond=TRUE")
  55. } else {
  56.   print("Executa comandos entre segundas chaves se cond=FALSE")
  57. }
  58.  
  59. if (1!=1) {
  60.   print("Executa comandos entre primeiras chaves se cond=TRUE")
  61. } else {
  62.   print("Executa comandos entre segundas chaves se cond=FALSE")
  63. }
  64.  
  65. # ---------------------------------------------------------
  66.  
  67. # switch (x in vetor) {executa comparando com elementos do vetor}  
  68.  
  69. sentimentos <- c("triste", "temeroso")
  70.  
  71. for (i in sentimentos) {print(switch(i,
  72.            alegre = "Que bom que você está feliz!",
  73.            temeroso = "Não há o que temer neste caso!",
  74.            triste = "Sua tristeza logo se dissipará!",
  75.            raivoso = "Acalme-se, por favor!"
  76.            ))}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement