## 2.11 Coding Exercises

1. The stringr package contains a number of functions for working with strings. For this problem create the following character vector in R

x <- c("economics", "econometrics", "ECON 4750")

Install the stringr package and use the str_length function in the package in order to calculate the length (number of characters) in each element of x.

2. For this problem, we are going to write a function to calculate the sum of the numbers from 1 to $$n$$ where $$n$$ is some positive integer. There are actually a lot of different ways to do this.

• Approach 1: write a function called sum_one_to_n_1 that uses the R functions seq to create a list of numbers from 1 to $$n$$ and then the function sum to sum over that list.

• Approach 2: The sum of numbers from 1 to $$n$$ is equal to $$n(n+1)/2$$. Use this expression to write a function called sum_one_to_n_2 to calculate the sum from 1 to $$n$$.

• Approach 3: A more brute force approach is to create a list of numbers from 1 to $$n$$ (you can use seq here) and add them up using a for loop — basically, just keep track of what the current total is and add the next number to the total in each iteration of the for loop. Write a function called sum_one_to_n_3 that does this.

Hint: All of the functions should look like

sum_one_to_n <- function(n) {
# do something
}

Try out all three approaches that you came up with above for $$n=100$$. What is the answer? Do you get the same answer using all three approaches?

3. The Fibonacci sequence is the sequence of numbers $$0,1,1,2,3,5,8,13,21,34,55,\ldots$$ that comes from starting with $$0$$ and $$1$$ and where each subsequent number is the sum of the previous two. For example, the 5 in the sequence comes from adding 2 and 3; the 55 in the sequence comes from adding 21 and 34.

1. Write a function called fibonacci that takes in a number n and computes the nth element in the Fibonacci sequence. For example fibonacci(5) should return 3 and fibonacci(8) should return 13.

2. Consider an alternative sequence where, starting with the third element, each element is computed as the sum of the previous two elements (the same as with the Fibonacci sequence) but where the first two elements can be arbitrary. Write a function alt_seq(a,b,n) where a is the first element in the sequence, b is the second element in the sequence, and n is which element in the sequence to return. For example, if $$a=3$$ and $$b=7$$, then the sequence would be $$3,7,10,17,27,44,71,\ldots$$ and alt_seq(a=3,b=7,n=4) = 17.

4. This problem involves writing functions related to computing prime numbers. Recall that a prime number is a positive integer whose only (integer) factors are 1 and itself (e.g., $$6$$ is not prime because it factors into $$2\times 3$$, but $$5$$ is a prime number because its only factors are $$1$$ and $$5$$).

For this problem, you cannot use any built-in functions in R for computing prime numbers or checking whether or not a number is a prime number. However, a helpful function for this problem is the modulo function, %% discussed earlier in the notes. Hint: Notice that 6 %% 2 = 0 indicates that 2 is a factor of 6; on the other hand, if you divide $$5$$ by any integer small than itself (except for $$1$$), the remainder will always be non-zero.

1. Write a function is_prime that takes x as an argument and returns TRUE if x is a prime number and returns FALSE if x is not a prime number.

2. Write a function prime that takes n as an argument and returns a vector of all the prime numbers from $$1$$ to $$n$$. If it is helpful, prime can call the function is_prime that you wrote for part (a).

5. Base R includes a data frame called iris. This is data about iris flowers (you can read the details by running ?iris).

1. How many observations are there in the entire data frame?

2. Calculate the average Sepal.Length across all observations in iris.

3. Calculate the average Sepal.Width among the setosa iris species.

4. Sort iris by Petal.Length and print the first 10 rows.