R you functional? (2)
A possible implementation of the interpret() function has been provided for you. In this exercise you'll be writing another function that will use the interpret() function to interpret all the data from your daily profile views inside a vector. Furthermore, your function will return the sum of views on popular days, if asked for. A for loop is ideal for iterating over all the vector elements. The ability to return the sum of views on popular days is something you can code through a function argument with a default value.
Bu egzersiz
Intermediate R
kursunun bir parçasıdırEgzersiz talimatları
Finish the template for the interpret_all() function:
- Make
return_suman optional argument, that isTRUEby default. - Inside the
forloop, iterate over allviews: on every iteration, add the result ofinterpret(v)tocount. Remember thatinterpret(v)returnsvfor popular days, and0otherwise. At the same time,interpret(v)will also do some printouts. - Finish the
ifconstruct: - If
return_sumisTRUE, returncount. - Else, return
NULL.
Call this newly defined function on both linkedin and facebook.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# The interpret() can be used inside interpret_all()
interpret <- function(num_views) {
if (num_views > 15) {
print("You're popular!")
return(num_views)
} else {
print("Try to be more visible!")
return(0)
}
}
# Define the interpret_all() function
# views: vector with data to interpret
# return_sum: return total number of views on popular days?
interpret_all <- function(views, return_sum) {
count <- 0
for (v in views) {
}
if (return_sum) {
} else {
}
}
# Call the interpret_all() function on both linkedin and facebook