Optimize Data Exploration With Sapply() – Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)