R Introduction Exercises¶

Programming for Data Science Bootcamp

Exercise 1¶

Write an R script (not a function) to get the first 10 Fibonacci numbers.

For $n > 2$: $F(n) = F_{n - 1} + F_{n - 2}$

$F_1 = 1, F_2 = 1$

n 1 2 3 4 5 6 7 8 9 10
F 1 1 2 3 5 8 13 21 34 55
In [1]:
# Initialize a vector of length 10
fibonacci <- numeric(10)

# Set the first two elements to 1
fibonacci[1] <- fibonacci[2] <- 1

# Iterate and apply the kernel function
for (i in 3:10) {
    fibonacci[i] <- fibonacci[i - 2] + fibonacci[i - 1]
}
In [2]:
print(fibonacci)
 [1]  1  1  2  3  5  8 13 21 34 55

Exercise 2¶

Convert the previous code into a function that takes an argument of the length of the sequence.

Instead of printing the results, just return the vector.

In [3]:
get_fibonacci <- function(n = 10) {
  fibonacci <- numeric(n)
  fibonacci[1] <- fibonacci[2] <- 1
  for (i in 3:n) {
      fibonacci[i] <- fibonacci[i - 2] + fibonacci[i - 1]
  }
  return(fibonacci)
}
In [4]:
fib20 = get_fibonacci(20)
In [6]:
print(fib20)
 [1]    1    1    2    3    5    8   13   21   34   55   89  144  233  377  610
[16]  987 1597 2584 4181 6765

Exercise 3¶

Write a function that takes a numeric $x$ and returns $1$ if $0 <= x <= 1$, else returns $0$.

Hint: use & for "and"

In [3]:
in_unit_interval <- function(x) { 
  if (x >= 0 & x <= 1) { 
    return(1) 
  } else { 
    return(0) 
  } 
}

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses. Source.

Exercise 4¶

Apply the function to five values.

In [14]:
in_unit_interval(-1) 
in_unit_interval(0) 
in_unit_interval(0.5)
in_unit_interval(1) 
in_unit_interval(2)
0
1
1
1
0
In [19]:
for (i in -1:3) {
    print(in_unit_interval(i))
}
[1] 0
[1] 1
[1] 1
[1] 0
[1] 0

Exercise 5¶

Write an R program that does the following:

  • Extracts the first $10$ English letters in lower case
  • Extracts the last $10$ letters in upper case
  • Extracts the $22^{nd}$, $23^{rd}$, and $24^{th}$ letters in upper case.

Hint: R comes with letters and LETTERS, two vectors that contain what you think they do.

In [3]:
print("First 10 letters in lower case:")
print(head(letters, 10))
[1] "First 10 letters in lower case:"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
In [4]:
print("Last 10 letters in upper case:")
print(tail(LETTERS, 10))
[1] "Last 10 letters in upper case:"
 [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
In [5]:
print("Letters between 22nd to 24th letters in upper case:")
print(LETTERS[22:24])
[1] "Letters between 22nd to 24th letters in upper case:"
[1] "V" "W" "X"

Another way:

In [7]:
print(LETTERS[1:10])
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
In [8]:
L = length(LETTERS); L
26
In [9]:
print(LETTERS[(L - 9):L])
 [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

Exercise 6¶

Write an R program that prints the numbers from $1$ to $100$.

  • Print "Fizz" for multiples of $3$ (but not $5$)
  • Print "Buzz" for multiples of $5$ (but not $3$)
  • Print "FizzBuzz" for multiples of $3$ and $5$

Hint: Use the modulus operator %% to determine multiples.

In [5]:
for (n in 1:100) {
 if (n %% 3 == 0 && n %% 5 == 0) {
     print(paste(n, "FizzBuzz"))
 }
 else if (n %% 3 == 0) {
     print(paste(n, "Fizz"))
 }
 else if (n %% 5 == 0) {
     print(paste(n, "Buzz"))
 }
 else print(paste(n))
}
[1] "1"
[1] "2"
[1] "3 Fizz"
[1] "4"
[1] "5 Buzz"
[1] "6 Fizz"
[1] "7"
[1] "8"
[1] "9 Fizz"
[1] "10 Buzz"
[1] "11"
[1] "12 Fizz"
[1] "13"
[1] "14"
[1] "15 FizzBuzz"
[1] "16"
[1] "17"
[1] "18 Fizz"
[1] "19"
[1] "20 Buzz"
[1] "21 Fizz"
[1] "22"
[1] "23"
[1] "24 Fizz"
[1] "25 Buzz"
[1] "26"
[1] "27 Fizz"
[1] "28"
[1] "29"
[1] "30 FizzBuzz"
[1] "31"
[1] "32"
[1] "33 Fizz"
[1] "34"
[1] "35 Buzz"
[1] "36 Fizz"
[1] "37"
[1] "38"
[1] "39 Fizz"
[1] "40 Buzz"
[1] "41"
[1] "42 Fizz"
[1] "43"
[1] "44"
[1] "45 FizzBuzz"
[1] "46"
[1] "47"
[1] "48 Fizz"
[1] "49"
[1] "50 Buzz"
[1] "51 Fizz"
[1] "52"
[1] "53"
[1] "54 Fizz"
[1] "55 Buzz"
[1] "56"
[1] "57 Fizz"
[1] "58"
[1] "59"
[1] "60 FizzBuzz"
[1] "61"
[1] "62"
[1] "63 Fizz"
[1] "64"
[1] "65 Buzz"
[1] "66 Fizz"
[1] "67"
[1] "68"
[1] "69 Fizz"
[1] "70 Buzz"
[1] "71"
[1] "72 Fizz"
[1] "73"
[1] "74"
[1] "75 FizzBuzz"
[1] "76"
[1] "77"
[1] "78 Fizz"
[1] "79"
[1] "80 Buzz"
[1] "81 Fizz"
[1] "82"
[1] "83"
[1] "84 Fizz"
[1] "85 Buzz"
[1] "86"
[1] "87 Fizz"
[1] "88"
[1] "89"
[1] "90 FizzBuzz"
[1] "91"
[1] "92"
[1] "93 Fizz"
[1] "94"
[1] "95 Buzz"
[1] "96 Fizz"
[1] "97"
[1] "98"
[1] "99 Fizz"
[1] "100 Buzz"
In [62]:
for (n in 1:100) {
    a <- ''
    b <- ''
    if (n %% 3 == 0) {
        a <- "Fizz"
    }
    if (n %% 5 == 0) {
        b <- "Buzz"
    }
    print(paste(n, ' ', a, b, sep=''))
}
[1] "1 "
[1] "2 "
[1] "3 Fizz"
[1] "4 "
[1] "5 Buzz"
[1] "6 Fizz"
[1] "7 "
[1] "8 "
[1] "9 Fizz"
[1] "10 Buzz"
[1] "11 "
[1] "12 Fizz"
[1] "13 "
[1] "14 "
[1] "15 FizzBuzz"
[1] "16 "
[1] "17 "
[1] "18 Fizz"
[1] "19 "
[1] "20 Buzz"
[1] "21 Fizz"
[1] "22 "
[1] "23 "
[1] "24 Fizz"
[1] "25 Buzz"
[1] "26 "
[1] "27 Fizz"
[1] "28 "
[1] "29 "
[1] "30 FizzBuzz"
[1] "31 "
[1] "32 "
[1] "33 Fizz"
[1] "34 "
[1] "35 Buzz"
[1] "36 Fizz"
[1] "37 "
[1] "38 "
[1] "39 Fizz"
[1] "40 Buzz"
[1] "41 "
[1] "42 Fizz"
[1] "43 "
[1] "44 "
[1] "45 FizzBuzz"
[1] "46 "
[1] "47 "
[1] "48 Fizz"
[1] "49 "
[1] "50 Buzz"
[1] "51 Fizz"
[1] "52 "
[1] "53 "
[1] "54 Fizz"
[1] "55 Buzz"
[1] "56 "
[1] "57 Fizz"
[1] "58 "
[1] "59 "
[1] "60 FizzBuzz"
[1] "61 "
[1] "62 "
[1] "63 Fizz"
[1] "64 "
[1] "65 Buzz"
[1] "66 Fizz"
[1] "67 "
[1] "68 "
[1] "69 Fizz"
[1] "70 Buzz"
[1] "71 "
[1] "72 Fizz"
[1] "73 "
[1] "74 "
[1] "75 FizzBuzz"
[1] "76 "
[1] "77 "
[1] "78 Fizz"
[1] "79 "
[1] "80 Buzz"
[1] "81 Fizz"
[1] "82 "
[1] "83 "
[1] "84 Fizz"
[1] "85 Buzz"
[1] "86 "
[1] "87 Fizz"
[1] "88 "
[1] "89 "
[1] "90 FizzBuzz"
[1] "91 "
[1] "92 "
[1] "93 Fizz"
[1] "94 "
[1] "95 Buzz"
[1] "96 Fizz"
[1] "97 "
[1] "98 "
[1] "99 Fizz"
[1] "100 Buzz"
In [73]:
for (n in 1:100) {
    x <- paste(n, '')
    if (n %% 3 == 0) {
        x <- paste(x, "Fizz", sep='')
    }
    if (n %% 5 == 0) {
        x <- paste(x, "Buzz", sep='')
    }
    print(x)
}
[1] "1 "
[1] "2 "
[1] "3 Fizz"
[1] "4 "
[1] "5 Buzz"
[1] "6 Fizz"
[1] "7 "
[1] "8 "
[1] "9 Fizz"
[1] "10 Buzz"
[1] "11 "
[1] "12 Fizz"
[1] "13 "
[1] "14 "
[1] "15 FizzBuzz"
[1] "16 "
[1] "17 "
[1] "18 Fizz"
[1] "19 "
[1] "20 Buzz"
[1] "21 Fizz"
[1] "22 "
[1] "23 "
[1] "24 Fizz"
[1] "25 Buzz"
[1] "26 "
[1] "27 Fizz"
[1] "28 "
[1] "29 "
[1] "30 FizzBuzz"
[1] "31 "
[1] "32 "
[1] "33 Fizz"
[1] "34 "
[1] "35 Buzz"
[1] "36 Fizz"
[1] "37 "
[1] "38 "
[1] "39 Fizz"
[1] "40 Buzz"
[1] "41 "
[1] "42 Fizz"
[1] "43 "
[1] "44 "
[1] "45 FizzBuzz"
[1] "46 "
[1] "47 "
[1] "48 Fizz"
[1] "49 "
[1] "50 Buzz"
[1] "51 Fizz"
[1] "52 "
[1] "53 "
[1] "54 Fizz"
[1] "55 Buzz"
[1] "56 "
[1] "57 Fizz"
[1] "58 "
[1] "59 "
[1] "60 FizzBuzz"
[1] "61 "
[1] "62 "
[1] "63 Fizz"
[1] "64 "
[1] "65 Buzz"
[1] "66 Fizz"
[1] "67 "
[1] "68 "
[1] "69 Fizz"
[1] "70 Buzz"
[1] "71 "
[1] "72 Fizz"
[1] "73 "
[1] "74 "
[1] "75 FizzBuzz"
[1] "76 "
[1] "77 "
[1] "78 Fizz"
[1] "79 "
[1] "80 Buzz"
[1] "81 Fizz"
[1] "82 "
[1] "83 "
[1] "84 Fizz"
[1] "85 Buzz"
[1] "86 "
[1] "87 Fizz"
[1] "88 "
[1] "89 "
[1] "90 FizzBuzz"
[1] "91 "
[1] "92 "
[1] "93 Fizz"
[1] "94 "
[1] "95 Buzz"
[1] "96 Fizz"
[1] "97 "
[1] "98 "
[1] "99 Fizz"
[1] "100 Buzz"
In [32]:
?paste

Exercise 7¶

Write an R program to get the unique elements of a given string and unique numbers of vector.

Hint: use the unique() function.

In [6]:
str1 <- "The quick brown fox jumps over the lazy dog."
sv1 <- unlist(strsplit(tolower(str1), ' '))
print(sv1)
[1] "the"   "quick" "brown" "fox"   "jumps" "over"  "the"   "lazy"  "dog." 
In [7]:
print(unique(sv1))
[1] "the"   "quick" "brown" "fox"   "jumps" "over"  "lazy"  "dog." 

Note that unlist() converts a list into a vector.

We use it because unique() wants a vector as an argument.

In [8]:
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
In [10]:
print(unique(nums))
[1] 1 2 3 4 5 6

Exercise 8¶

Write an R program to find the maximum and the minimum value of a given vector.

Hint: use the max() and min() functions.

In [42]:
nums = c(10, 20, 30, 40, 50, 60)
print('Original vector:')
print(nums)   
print(paste("Maximum value of the said vector:",max(nums)))
print(paste("Minimum value of the said vector:",min(nums)))
[1] "Original vector:"
[1] 10 20 30 40 50 60
[1] "Maximum value of the said vector: 60"
[1] "Minimum value of the said vector: 10"

Exercise 9¶

Write an R program to

  • print a sequence of numbers from $20$ to $50$
  • print the mean of numbers from $20$ to $60$
  • print the sum of numbers from $51$ to $91$

Hint: Use seq(), mean(), and sum().

Sequence of numbers from 20 to 50:

In [10]:
seq(20, 50)
  1. 20
  2. 21
  3. 22
  4. 23
  5. 24
  6. 25
  7. 26
  8. 27
  9. 28
  10. 29
  11. 30
  12. 31
  13. 32
  14. 33
  15. 34
  16. 35
  17. 36
  18. 37
  19. 38
  20. 39
  21. 40
  22. 41
  23. 42
  24. 43
  25. 44
  26. 45
  27. 46
  28. 47
  29. 48
  30. 49
  31. 50

Mean of numbers from 20 to 60:

In [11]:
mean(20:60)
40

Sum of numbers from 51 to 91:

In [12]:
sum(51:91)
2911

Exercise 10¶

Write an R program to create a data frame from four given vectors.

Make one character vector, one float, one integer, and one boolean.

Make each vector be length $10$.

In [113]:
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas')
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19)
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1)
qualify = c(T, F, T, F, F, T, T, F, F, T)
In [118]:
df = data.frame(name, score, attempts, qualify)  
df
A data.frame: 10 × 4
namescoreattemptsqualify
<chr><dbl><dbl><lgl>
Anastasia12.51 TRUE
Dima 9.03FALSE
Katherine16.52 TRUE
James 12.03FALSE
Emily 9.02FALSE
Michael 20.03 TRUE
Matthew 14.51 TRUE
Laura 13.51FALSE
Kevin 8.02FALSE
Jonas 19.01 TRUE
In [119]:
print(df)
        name score attempts qualify
1  Anastasia  12.5        1    TRUE
2       Dima   9.0        3   FALSE
3  Katherine  16.5        2    TRUE
4      James  12.0        3   FALSE
5      Emily   9.0        2   FALSE
6    Michael  20.0        3    TRUE
7    Matthew  14.5        1    TRUE
8      Laura  13.5        1   FALSE
9      Kevin   8.0        2   FALSE
10     Jonas  19.0        1    TRUE

Exercise 11¶

Write an R program to extract 3rd and 5th rows with 1st and 3rd columns from the built in airquality data frame.

In [26]:
head(airquality)
A data.frame: 6 × 6
OzoneSolar.RWindTempMonthDay
<int><int><dbl><int><int><int>
141190 7.46751
236118 8.07252
31214912.67453
41831311.56254
5NA NA14.35655
628 NA14.96656

Extract 3rd and 5th rows with 1st and 3rd columns:

In [121]:
airquality[c(3,5), c(1,3)]
A data.frame: 2 × 2
OzoneWind
<int><dbl>
31212.6
5NA14.3

Not same as:

In [122]:
airquality[3:5, 1:3]
A data.frame: 3 × 3
OzoneSolar.RWind
<int><int><dbl>
31214912.6
41831311.5
5NA NA14.3

Exercise 12¶

Use the built-in data frame mtcars and create a plot to compare mpg, cyl (number of cylinders), and hp (horsepower).

In [51]:
plot(mtcars[, c('hp', 'cyl', 'mpg')])
No description has been provided for this image
In [126]:
plot(mtcars[c('hp', 'cyl', 'mpg')])
No description has been provided for this image