- Parallel Computing Exercises: Foreach and DoParallel (Part-2)
- Power Analysis: Exercises
- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-2)
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

It is said that, in R, *everything that happens is a function call*. So, if we want to improve our ability to make things happen the way we want them to, maybe it’s worth getting very comfortable with how functions work in R.

In this exercise set, we’ll try to gain better fluency and deepen our understanding of the R logic by (mostly) writing some functions.

Solutions are available here.

**Exercise 1**

Write a function that calculates number `a`

to the power of `b`

, but let `b`

have a default value of `2`

.

**Exercise 2**

Re-write the function from exercise 1 so that `a`

has a default value of `b+1`

already from the formals (from the argument definition.)

**Exercise 3**

Write a function `div()`

that checks if the first number divides the second.

**Exercise 4**

Write an *infix* function `%div%`

that checks if the left-hand side divides the right-hand side.

# Example

3 %div% 42

[1] TRUE

3 %div% 13

[1] FALSE

**Exercise 5**

Write a function that changes the value of `pi`

in the R global environment to whatever you specify as the argument. Note: it is not recommended to re-define the value of “pi” in a real-life R program.

# Example

pi

[1] 3.141593

chpi(4)

pi

[1] 4

chpi("I like cats")

pi

[1] "I like cats"

**Exercise 6**

Write an infix (binary) function that checks if the left hand side (lhs) is in the range between the maximum and minimum of the rhs.

set.seed(1)

rnorm(6) %betw% c(-1, 1)

[1] TRUE TRUE TRUE FALSE TRUE TRUE

**Exercise 7**

Write another infix operator that pastes two strings and uses it in a function that takes `ellipsis`

. Now, call the function and feed it two strings and concatenate them to produce some text.

**Exercise 8**

What did you think about the example in exercise 3? What does the following code (from the book Advanced R) return?

f2 <- function(x = z) {

z <- 100

x

}

f2()

**Exercise 9**

Write a function that creates a function analogous to the one from exercise 1 by feeding 2 as an argument. Note: this is not a typo.

# Example of a function called power()

powerof2 <- power(2)

powerof2(3)

[1] 9

**Exercise 10**

Make a new `names<-`

(replacement) function called `names2<-`

that also allows you to update the names of data structures, except it forces the names it is given to all-lowercase.

# Example

x <- 3:4

names2(x) <- c("IMPORTANT", "For The CAT do NOT EAT")

x

important for the cat do not eat

3 4

(Photo by Pascal)

]]>- A Primer in Functional Programming in R (Part – 1)
- Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-2)
- Multiple Regression (Part 3) Diagnostics
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory

In the last exercise, We have seen how powerful functional programming principles can be and how it can drammatically increase the readablity of the code and how easily you can work with them .In this set of exercises we will look at functional programming principles with `purrr`

.Purrr comes with a number of interesting features and is really useful in writing clean and concise code . Please check the documentation and load the purrr library in your R session before starting these exercise set .

Answers to the exercises are available here

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

From the airquality dataset( available in base R ) , Find the mean ,median ,standard deviation of all columns using map functions .

**Exercise 2**

In the same dataset,find 95th percentile of each column excluding the NA values

**Exercise 3**

Load the iris dataset ,with help of `pipe`

and `map `

functions find out the mean of the relavant columns.Keep in mind mean is meant for numeric columns ,so you may need multiple map like functions.I expect the output as a dataframe .

**Exercise 4**

I have a vector ` x <- 1:20 `

,I want to multiply every odd element with 2 and every even element with 4 , find a solution using purrr .

**Exercise 5**

I have a sequence ` x <- 1:100`

, I want to increase the 1st,11th, 21st…91st element by 1 . How can I achieve that .

**Exercise 6**

Suppose I have two character vectors

x <- letters # letters is a vector of alphabets in small available in R

y <- LETTERS # LETTERS is a vector of alphabets in caps available in R

How do I join each capital letter with the small letter so that the end result looks like this

"A,a" "B,b" ,.. and so on

**Exercise 7**

The previous exercise gave you the intuition of how to work parallely on two vectors.Now accepts a list of character vectors of same size and joins them like the previous exercise . so if I have a

list like ` x <- list(c("a","b","c"),c("d","e",f"),c("g","h","i"))`

,it should give me output as .

` [1] "a d g" "b e h" "c f i" `

**Exercise 8**

using a functional tool from purrr ,reverse the letters so that my output is a string of 26 character starting from z and ending at a

like “z y x w v u t s r q p o n m l k j i h g f e d c b a”

**Exercise 9**

Exercise on Purrr wont be complete if We dont mention its “Filter” like functions . take a numeric vector of 1:100 , keep all the numbers which are divisible by 2 and after that remove the one’s which are divisible by 5

**Exercise 10**

Ability to create partial functions is a powerful tool in functional programming. This allow functions to behave a little like data structures .Consider creating a partial function whenever you see you are repeating functions argument. This may not sound very useful in context of this exercise but I am sure you will find it very useful in your R gigs .Now create a Partial function

ql ,which is to find the 25th percentile and 50th percentile of a column from a dataframe .use it to find the same from airquality dataset .Dont use quantile twice in this exercise .

In the exercises below we cover the basics of functional programming in R( part 1 of a two series exercises on functional programming) . We consider recursion with R , apply family of functions , higher order functions such as Map ,Reduce,Filter in R .

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

Create a function which calculates factorial of a number with help of `Reduce `

,

**Exercise 2**

Create the same function which calculates factorial but with recursion and memoization. `:`

**Exercise 3**

Create a function ` cum_add`

which makes cumulative summation for e.g if `x <- 1:3 `

cum_add(x) will result in `1 3 6 `

.Don’t use cumsum .

**Exercise 4**

Create a function which takes a dataframe and returns mean , minimum and maximum of all numeric columns . Your function should take a dataframe as input .for e.g ` your_func(iris)`

.Try to avoid loops ,its possible to make it in one line .swag your functional skills .

**Exercise 5**

Create a function `centerColumnAroundMean`

which takes a numeric dataframe and manipulates the dataframe such a way that all column’s values are centered with mean value of the column . For example if my data frame is like this

` df <- data.frame(x= 1:5,y=6:10,c=11:15) `

then ` centerColumnAroundMean(df) `

will result in

It is possible to achieve this by single line of R , Please dont use loops .

**Exercise 6**

I have list of movies ,which have two movie franchieses as the elements ,Starwars and LOTR.You can create the movie list by

`my_movie_list<- list(STARWARS= list("A NEW HOPE","The Last Jedi","The Force Awakens"),LOTR=list("THE FELLOWSHIP OF THE RING","THE Two Towers","The RETURN of the KING","Hobbit" = list("An unexpected Journey","The Battle of the FIVE ARMY","The Desolation of Smaug") )) `

now the problem I am facing is some of the texts are in caps and some of them are in small ,without any particular order . I would like the list to have a format like

“The Last Jedi” , Help me in writing a function which will do the same . Please keep in mind that the list is a nested list .

**Exercise 7**

Load the diamonds data set from ggplot 2 package , I want to buy a diamond of each color but don’t want to pay the top price , I believe that the second highest price is good enough for me . Help me in finding the second highest price for each color from the dataset.

**Exercise 8**

Use the already loaded diamonds dataset from previous exercise , I want to know the average price for each combination of cut and color .Your output should be similar to following.Don’t use `table`

.

**Exercise 9**

Load the iris dataset , I want to get the third row for each group of species . Help me to write a short function for me to achieve that .

**Exercise 10**

Create a new environment with ` new.env() `

command and create 3 vector variables under that environment as ` a=1:10;b=100:500;c=1000:1500 `

without knowing or manually calling mean for all the vector variables can you print the mean of all variables of the new environment .

In the Exercises we will discuss User Defined Function in R

Answers to the exercises are available here.

**Exercise 1**

Create a function to print square of number

**Exercise 2**

Create a function to print a number raise to another with the one argument a default argument

**Exercise 3**

Create a function to print class of an argument

**Exercise 4**

Create a function to accept two matrix arguments and do matrix operations with same.

**Exercise 5**

Create a user defined function to accept a name from the user

**Exercise 6**

Create a user defined function to accept values from the user using scan and return the values

**Exercise 7**

Create a user defined function to create a matrix and return the same.

**Exercise 8**

Create a function to take two arguments, one student marks and other student names and plot a graph based on the same.

**Exercise 9**

Create a function to accept an employee data frame(Name,Gender,Age,Designation & SSN) and print the First & Fifth employee as well as the Names & the Designation of all the employees in the function

**Exercise 10**

Create a function to create an employee data frame(Name,Gender,Age,Designation & SSN) and return the Name,Age & Designation of all employees.

]]>[For this exercise, first write down your answer, without using R. Then, check your answer using R.]

Answers to the exercises are available here.

**Exercise 1**

Create a function that given a data frame and a vector, will add a the vector (if the vector length match with the rows number of the data frame)

as a new variable to the data frame.

**Exercise 2**

Consider a data frame df:

Id=c(1:10)

Age=c(14,12,15,10,23,21,41,56,78,12)

Sex=c('F','M','M','F','M','F','M','M','F','M')

Code=letters[1:10]

df=data.frame(Id,Age,Sex,Code)

Create a function that, given a data frame and two indexes, exchanges two values of the Code variable with each other.

For example, if the index is 1 and 3, you assign:

df[1,'Code']=df[3,'Code']

df[3,'Code']=df[1,'Code']

**Exercise 3**

Consider two variables x,y and a data frame df:

x,y integer

```
```

`A=c(1:10)`

B=seq(100,10,-10)

H=seq(-200,-50,along.with=B)

df=data.frame(A,B,H)

Create a function that given a data frame df calculate a new variable ‘SUM_x_y'(If x=2 and y=3, then the new variable will be ‘SUM_2_3’,

if x=4 and y=10, then the new variable will be ‘SUM_4_10’),such that for each row ‘i’ is equal to:

sum(x*df[1:i,1])+sum(y*df[1:i,2])

**Exercise 4**

Create a function that given a numeric vector, sort this in ascending order and duplicate it by two.

**Exercise 5**

Create a function that given a vector alpha numeric, keep only the numbers and apply the function created on exercise 4.

For example, if the input is a vector `w="a" "v" "7" "4" "q"`

, the function will return `w=8 14`

.

**Exercise 6**

Create a function that given a string

ST='NAME: Maria /COUNTRY:uruguay /EMAIL: mariaUY@gmail.com'

return a matrix

[,1] [,2]

[1,] "NAME" " Maria "

[2,] "COUNTRY" "uruguay "

[3,] "EMAIL" " mariaUY@gmail.com"

**Exercise 7**

Consider a vector:

ST=c('NAME:Maria /COUNTRY:uruguay /EMAIL:mariaUY@gmail.com','NAME:Paul/COUNTRY:UK /EMAIL:PaulUK@gmail.com',

'NAME:Jhon /COUNTRY:USA /EMAIL:JhonUSA@gmail.com','NAME:Carlos /COUNTRY:Spain /EMAIL:CarlosSP@gmail.com')

Create a function that given a vector string ST return a matrix:

[,1] [,2] [,3] [,4] [,5]

[1,] "NAME" "Maria " "Paul" "Jhon " "Carlos "

[2,] "COUNTRY" "uruguay " "UK " "USA " "Spain "

[3,] "EMAIL" "mariaUY@gmail.com" "PaulUK@gmail.com" "JhonUSA@gmail.com" "CarlosSP@gmail.com"

**Exercise 8**

Create a function that given a numeric vector X returns the digits 0 to 9 that are not in X. If `X=0 2 4 8 `

the function return `1 3 5 6 7 9`

**Exercise 9**

Create a function that given two strings (one word each), check if one is an anagram of another.

**Exercise 10**

Create a function that given one word, return the position of word’s letters on `letters`

vector.

For example, if the word is ‘abc’, the function will return 1 2 3.

**Want to practice functions a bit more? We have more exercise sets on this topic here.**

`Map`

, `Reduce`

, `Filter`

, `Find`

, `Position`

, `Negate`

. They enable us to complete complex operations by using simple single-purpose functions as their building blocks. In R this is especially helpful in cases where we cannot depend on vectorization and have to utilize control statements like for loops. In such scenarios higher order functions help us by: a) simplifying and shortening the syntax, b) getting rid of counter indices and c) getting rid of temporary storage values.
Exercises in this section will have to be solved by using one or more of the higher order functions mentioned above. It might be useful reading their help page before continuing.

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Exercise 1**

You are working on 3 datasets all at once:

`multidata <- list(mtcars, USArrests, rock)`

`summary(multidata[[1]])`

will return the summary information for a single dataset.

Obtain summary information for every dataset in the list.

**Exercise 2**

`cumsum(1:100)`

returns the cumulative sums of a vector of numbers from 1 to 100.

Do the same using `sum`

and an appropriate higher order function.

**Exercise 3**

You have a vector of numbers from 1 to 10. You want to multiply all those numbers first by 2 and then by 4. Why the following line does not work and how to fix it?

`Map(`*`, 1:10, c(2,4))`

**Exercise 4**

Expression `sample(LETTERS, 5, replace=TRUE)`

obtains 5 random letters.

Generate a list with 10 elements, where first element contains 1 random letter, second element 2 random letters and so on.

Note: use a fixed random seed: `set.seed(14)`

**Exercise 5**

Library `spatstat`

has a function `is.prime()`

that checks if a given number is a prime.

Find all prime numbers between 100 and 200.

**Exercise 6**

We have a vector containing all the words of the English language –

`words <- scan("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt", what="character")`

a. Using a function that checks if a given words contains any vowels:

`containsVowel <- function(x) grepl("a|o|e|i|u", x)`

find all words that do not contain any vowels.

b. Using a function `is.colour()`

from the `spatstat`

library find the index of the first word inside the words vector corresponding to a valid R color.

**Exercise 7**

a. Find the smallest number between 10000 and 20000 that is divisible by 1234.

b. Find the largest number between 10000 and 20000 that is divisible by 1234.

**Exercise 8**

Consider the `babynames`

dataset from the `babynames`

library.

Start with a list containing the used names for each year:

`library(babynames); namesData <- split(babynames$name, babynames$year)`

a. Obtain a set of names that were present in every year.

b. Obtain a set of names that are only present in year 2014

**Exercise 9**

Using the same `babynames`

dataset and a function that checks if word has more than 3 letters: `moreThan3 <- function(x) nchar(x) > 3`

Inside each year list leave only the names that have 3 letters or less.

**Exercise 10**

Using the same `babynames`

dataset:

a. Split each name to a list of letters.

b. Join each list of letters by inserting an underscore “_” after each letter.

Note: if you have a word `x <- "exercise"`

you can split it with `x2 <- strsplit(x, "")`

and join using underscores with `paste(x2[[1]], collapse="_")`

Answers to the exercises are available here.

If you obtained a different solution than the one posted on the answers page, please let us know of your solution by posting it as a comment at the end of that page.

Note: For some exercises, the solution will be quite easy if you make clever use of some of R’s built-in functions. For some exercises, you might want to create a vectorized solution (i.e., avoiding loops), and/or a (usually slower) non-vectorized solution. However, the exercises do not aim to practise vectorization and speed, but rather defining and calling functions.

**Exercise 1**

Create a function that will return the sum of 2 integers.

**Exercise 2**

Create a function what will return TRUE if a given integer is inside a vector.

**Exercise 3**

Create a function that given a data frame will print by screen the name of the column and the class of data it contains (e.g. Variable1 is Numeric).

**Exercise 4**

Create the function unique, which given a vector will return a new vector with the elements of the first vector with duplicated elements removed.

**Exercise 5**

Create a function that given a vector and an integer will return how many times the integer appears inside the vector.

**Exercise 6**

Create a function that given a vector will print by screen the mean and the standard deviation, it will optionally also print the median.

**Exercise 7**

Create a function that given an integer will calculate how many divisors it has (other than 1 and itself). Make the divisors appear by screen.

**Exercise 8**

Create a function that given a data frame, and a number or character will return the data frame with the character or number changed to NA.

**Want to practice functions a bit more? We have more exercise sets on this topic here.**

Image by Ninjahatori (Own work) via Wikimedia Commons

]]>