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 |
# 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]
}
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.
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)
}
fib20 = get_fibonacci(20)
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_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_unit_interval(-1)
in_unit_interval(0)
in_unit_interval(0.5)
in_unit_interval(1)
in_unit_interval(2)
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.
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"
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"
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:
print(LETTERS[1:10])
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
L = length(LETTERS); L
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.
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"
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"
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"
?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.
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."
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.
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
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.
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:
seq(20, 50)
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
Mean of numbers from 20 to 60:
mean(20:60)
Sum of numbers from 51 to 91:
sum(51:91)
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$.
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)
df = data.frame(name, score, attempts, qualify)
df
name | score | attempts | qualify |
---|---|---|---|
<chr> | <dbl> | <dbl> | <lgl> |
Anastasia | 12.5 | 1 | TRUE |
Dima | 9.0 | 3 | FALSE |
Katherine | 16.5 | 2 | TRUE |
James | 12.0 | 3 | FALSE |
Emily | 9.0 | 2 | FALSE |
Michael | 20.0 | 3 | TRUE |
Matthew | 14.5 | 1 | TRUE |
Laura | 13.5 | 1 | FALSE |
Kevin | 8.0 | 2 | FALSE |
Jonas | 19.0 | 1 | TRUE |
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.
head(airquality)
Ozone | Solar.R | Wind | Temp | Month | Day | |
---|---|---|---|---|---|---|
<int> | <int> | <dbl> | <int> | <int> | <int> | |
1 | 41 | 190 | 7.4 | 67 | 5 | 1 |
2 | 36 | 118 | 8.0 | 72 | 5 | 2 |
3 | 12 | 149 | 12.6 | 74 | 5 | 3 |
4 | 18 | 313 | 11.5 | 62 | 5 | 4 |
5 | NA | NA | 14.3 | 56 | 5 | 5 |
6 | 28 | NA | 14.9 | 66 | 5 | 6 |
Extract 3rd and 5th rows with 1st and 3rd columns:
airquality[c(3,5), c(1,3)]
Ozone | Wind | |
---|---|---|
<int> | <dbl> | |
3 | 12 | 12.6 |
5 | NA | 14.3 |
Not same as:
airquality[3:5, 1:3]
Ozone | Solar.R | Wind | |
---|---|---|---|
<int> | <int> | <dbl> | |
3 | 12 | 149 | 12.6 |
4 | 18 | 313 | 11.5 |
5 | NA | NA | 14.3 |
Exercise 12¶
Use the built-in data frame mtcars
and create a plot to compare mpg
, cyl
(number of cylinders), and hp
(horsepower).
plot(mtcars[, c('hp', 'cyl', 'mpg')])
plot(mtcars[c('hp', 'cyl', 'mpg')])