Problem 16

a)

is.even = function(n) {if (n %% 2 == 0) TRUE else FALSE}
collatz_num = function(n) {if (is.even(n)) n/2 else 3 * n + 1}
collatz_sequence = function(n) {
seq = n
while (n != 1) {
n <- collatz_num(n)
seq <- c(seq, n)
}
return(seq)
}

b)

is.even = function(n) {if (n %% 2 == 0) TRUE else FALSE}
collatz_num = function(n) {if (is.even(n)) n/2 else 3 * n + 1}
collatz_sequence = function(n) {
seq = n
while (n != 1) {
n <- collatz_num(n)
if (n %in% seq) {seq <- c(seq, n)
return(seq)}
else{seq <- c(seq, n)}
}
return(seq)
}

Problem 17

conf.interval = function(vector) {
  if (length(unique(vector)) == 2) {
    return(prop.test(table(vector))$conf.int)
  }
  else {
    if (class(vector) == 'numeric') {
      return(t.test(vector)$conf.int)
    }
    else {
      cat('Unable to perform a confidence interval with this data')
      return(NA)
    }
  }
}

Problem 18

a)

data = Forbes2000
head(sort(table(data$country), decreasing = TRUE), n = 8)
## 
##  United States          Japan United Kingdom        Germany         France 
##            751            316            137             65             63 
##         Canada    South Korea          Italy 
##             56             45             41

b)

## [1] "United States"
## [1] "Number of companies in the United States is 751"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.190   2.025   4.350  10.058   9.845 256.330

## [1] "Japan"
## [1] "Number of companies in the Japan is 316"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.240   2.087   4.550  10.191  10.207 135.820

## [1] "United Kingdom"
## [1] "Number of companies in the United Kingdom is 137"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.33    2.75    5.27   10.45    9.32  232.57

## [1] "Germany"
## [1] "Number of companies in the Germany is 65"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.01    4.80    8.84   20.78   22.43  157.13

## [1] "France"
## [1] "Number of companies in the France is 63"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.31    4.74   11.04   20.10   24.86  131.64

## [1] "Canada"
## [1] "Number of companies in the Canada is 56"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.320   2.620   4.175   6.430  10.182  18.820

## [1] "South Korea"
## [1] "Number of companies in the South Korea is 45"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.380   3.280   4.720   7.969   6.300  50.220

## [1] "Italy"
## [1] "Number of companies in the Italy is 41"
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.59    2.34    3.95   10.21    8.17   58.22

c)

a = c()
for (i in 1:nrow(data)) {
  if (data[i, 4] == 'Banking') {
    a = c(a, FALSE)
    next
  }
  if (grepl('bank', tolower(data[i,2]),fixed=TRUE)) {
    a = c(a, TRUE)
  }
  else {
    a = c(a, FALSE)
  }
}
data[a,][c('name', 'category', 'country')]
##                     name               category   country
## 34   Deutsche Bank Group Diversified financials   Germany
## 451           Depfa Bank Diversified financials   Ireland
## 932       Macquarie Bank Diversified financials Australia
## 1024            Softbank    Software & services     Japan
## 1349         Aareal Bank Diversified financials   Germany

Problem 19

a)

text <- scan(file="UDHR.txt", what = character())
for (i in 1:length(text)) {
text[i] <- tolower(gsub("[[:punct:]]", "", text[i]))
}
head(sort(table(text), decreasing = TRUE),50)
## text
##         the         and          of          to          in       right 
##         120         106          90          83          43          33 
##          be     article    everyone          or         has       shall 
##          31          30          30          30          28          27 
##         his      rights           a         any         for          by 
##          21          21          19          18          17          13 
##          is         all       human          as       equal     freedom 
##          13          12          12          11          11          11 
##        this    freedoms          no         one    entitled         law 
##          11          10          10          10           9           9 
##  protection       which        with         are   education        have 
##           9           9           9           8           8           8 
##     nations      social        free     whereas     against          at 
##           8           8           7           7           6           6 
## declaration      family        full fundamental       other     country 
##           6           6           6           6           6           5 
##     dignity        from 
##           5           5

b)

free_words = 0
liberty_words = 0
for (i in 1:length(table(text))) {
if (grepl('free', names(table(text))[i])) {
  free_words = free_words + as.numeric(table(text)[i])
}
if (grepl('liberty', names(table(text))[i])) {
liberty_words = liberty_words + as.numeric(table(text)[i])
}
}
print(paste('Number of words derived from free:', free_words))
## [1] "Number of words derived from free: 30"
print(paste('Number of words derived from liberty:', liberty_words))
## [1] "Number of words derived from liberty: 1"