Today we’re practicing functions! In the exercises below, you’re asked to write curt R scripts that define functions aimed at specific tasks. The exercises start out at an piece of cake level, together with gradually motion towards slightly to a greater extent than complex functions.
Answers to the exercises are available here.
If you lot obtained a unlike solution than the 1 posted on the answers page, delight allow us know of your solution yesteryear posting it every bit a comment at the halt of that page.
Note: For about exercises, the solution volition endure quite piece of cake if you lot brand clever utilization of about of R’s built-in functions. For about exercises, you lot powerfulness desire to do a vectorized solution (i.e., avoiding loops), and/or a (usually slower) non-vectorized solution. However, the exercises do non aim to practise vectorization together with speed, simply rather defining together with calling functions.
Exercise 1
Create a component that volition furnish the amount of ii integers.
Create a component that volition furnish the amount of ii integers.
Exercise 2
Create a component what volition furnish TRUE if a given integer is within a vector.
Create a component what volition furnish TRUE if a given integer is within a vector.
Exercise 3
Create a component that given a information frame volition impress yesteryear covert the advert of the column together with the shape of information it contains (e.g. Variable1 is Numeric).
Create a component that given a information frame volition impress yesteryear covert the advert of the column together with the shape of information it contains (e.g. Variable1 is Numeric).
Exercise 4
Create the component unique, which given a vector volition furnish a novel vector alongside the elements of the outset vector alongside duplicated elements removed.
Create the component unique, which given a vector volition furnish a novel vector alongside the elements of the outset vector alongside duplicated elements removed.
Exercise 5
Create a component that given a vector together with an integer volition furnish how many times the integer appears within the vector.
Create a component that given a vector together with an integer volition furnish how many times the integer appears within the vector.
Exercise 6
Create a component that given a vector volition impress yesteryear covert the hateful together with the measure deviation, it volition optionally too impress the median.
Create a component that given a vector volition impress yesteryear covert the hateful together with the measure deviation, it volition optionally too impress the median.
Exercise 7
Create a component that given an integer volition calculate how many divisors it has (other than 1 together with itself). Make the divisors seem yesteryear screen.
Create a component that given an integer volition calculate how many divisors it has (other than 1 together with itself). Make the divisors seem yesteryear screen.
Exercise 8
Create a component that given a information frame, together with a disclose or graphic symbol volition furnish the information frame alongside the graphic symbol or disclose changed to NA.
Create a component that given a information frame, together with a disclose or graphic symbol volition furnish the information frame alongside the graphic symbol or disclose changed to NA.
Want to exercise functions a fleck more? We accept to a greater extent than exercise sets on this topic here.
________________________________________
Below are the solutions to these exercises on functions. If you lot obtained a unlike (smarter, vectorized, etc) solution, delight post service every bit a comment below.
# Exercise 1 f.sum <- function (x, y) { r <- x + y r } f.sum(5, 10)
## [1] fifteen
# Exercise ii f.exists <- function (v, x) { exist <- FALSE i <- 1 while (i <= length (v) & !exist) { if (v[i] == x) { exist <- TRUE } i <- 1 + i } exist } f.exists(c(1:10), 10)
## [1] TRUE
f.exists(c(9, 3, 1), 10)
## [1] FALSE
# Exercise 3 f.class <- function (df) { for (i in 1:ncol(df)) { cat(names(df)[i], "is", class(df[, i]), "\n") } } f.class(cars)
## speed is numeric ## dist is numeric
# Exercise iv f.uniq <- function (v) { s <- c() for(i in 1:length(v)) { if(sum(v[i] == s) == 0) { s <- c(s, v[i]) } } s } f.uniq(c(9, 9, 1, 1, 1, 0))
## [1] nine 1 0
# Exercise five f.count <- function (v, x) { count <- 0 for (i in 1:length(v)) { if (v[i] == x) { count <- count + 1 } } count } f.count(c(1:9, rep(10, 100)), 10)
## [1] 100
# Exercise 6 desi <- function(x, med=FALSE) { mean <- round(mean(x), 1) stdv <- round(sd(x), 1) cat("Mean is:", mean, ", SD is:", stdv, "\n") if(med) { median <- median(x) cat("Median is:", median , "\n") } } desi(1:10, med=TRUE)
## Mean is: 5.5 , SD is: three ## Median is: 5.5
# Exercise seven f.div <- function(n) { i <- 2 counter <- 0 while(i <= n/2) { if(n%%i==0) { counter <- counter + 1 cat (i ,"\n") } i <- i + 1 } counter } f.div(13)
## [1] 0
f.div(16)
## ii ## iv ## 8
## [1] three
# Exercise 8 f.na <- function (df, otherna) { for(i in 1:ncol (df)) { for(j in 1:nrow (df)) { if(df[j,i] == otherna) { df[j,i] <- NA } } } df } carsnew <- f.na(cars, 10)
_______________________________________
Sources:
http://www.r-exercises.com/2016/02/07/functions-exercises/
http://www.r-exercises.com/2016/02/07/functions-exercises-solutions/