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.
Create a function which calculates factorial of a number with help of
Create the same function which calculates factorial but with recursion and memoization.
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 .
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 .
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)
It is possible to achieve this by single line of R , Please dont use loops .
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 .
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.
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
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 .
Create a new environment with
new.env() command and create 3 vector variables under that environment as
without knowing or manually calling mean for all the vector variables can you print the mean of all variables of the new environment .
- 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
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below