Optimize Data Exploration With Sapply() – Exercises

October 14, 2016
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

approved-1049259_960_720

The apply() functions in R are a utilization of the Split-Apply-Combine strategy for Data Analysis, and are a faster alternative to writing loops.

The sapply() function applies a function to individual values of a dataframe, and simplifies the output.

Structure of the sapply() function: sapply(data, function, ...)

The dataframe used for these exercises:
dataset1 <- data.frame(observationA = 16:8, observationB = c(20:19, 6:12))

Answers to the exercises are available here.

Exercise 1

Using sapply(), find the length of dataset1‘s observations:

Exercise 2

Using sapply(), find the sums of dataset1‘s observations:

Exercise 3

Use sapply() to find the quantiles of dataset1‘s columns:

Exercise 4

Find the classes of dataset1‘s columns:

Exercise 5

Required function:
DerivativeFunction <- function(x) { log10(x) + 1 }

Apply the “DerivativeFunction” to dataset1, with simplified output:

Exercise 6

Script the “DerivativeFunction” within sapply(). The data is dataset1:

Exercise 7

Find the range of dataset1:

Exercise 8

Print dataset1 with the sapply() function:

Exercise 9

Find the mean of dataset1‘s observations:

Exercise 10

Use sapply() to inspect dataset1 for numeric values:

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)