Advertisement
obernardovieira

Count number of brothers

Dec 28th, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.60 KB | None | 0 0
  1. calculate_brothers <- function(brothers_list) {
  2.     lt <- 0 #less than two
  3.     et <- 0 #equals two
  4.     mt <- 0 #more than two
  5.     for(i in 1:10) {
  6.         if(brothers_list[i] < 2) {
  7.             lt <- lt+1
  8.         }
  9.         else if(brothers_list[i] > 2) {
  10.             mt <- mt+1
  11.         }
  12.         else {
  13.             et <- et+1
  14.         }
  15.     }
  16.     result <- c(lt,et,mt)
  17.     return(result)
  18. }
  19.  
  20. number_of_brothers <- c(1, 2, 5, 2, 3, 1, 0, 3, 0, 1)
  21. numbers <- calculate_brothers(number_of_brothers)
  22. sprintf("You have %d people with less than 2 brothers, %d with more than 2, and %d with 2", numbers[1], numbers[2], numbers[3])
  23. # barplot(numbers) #use it, if you want to see the graphic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement