Please this should be solved by R studio # Fun with FUNctions # Part 4. Finish t
ID: 3202912 • Letter: P
Question
Please this should be solved by R studio
# Fun with FUNctions
# Part 4. Finish the function definition for interpret(), that interprets the number of profile views on a single day:
# The function takes one argument, num_views. If num_views is greater than 15, the function prints out "You're popular!"
# to the console and returns num_views. Else, the function prints out "Try to be more visible!" and returns 0. interpret <- function(num_views) { if (num_views > 15) { } else { } }
# Part 5. Call the interpret() function twice: on the first value of the linkedin vector and on the second element of
# the facebook vector. 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)
# Call the interpret function twice
# Part 6. Using two for loops call the function interpret on all the elements of linkedin and facebook
Explanation / Answer
Solution :
> linkedin <- c(16, 9, 13, 5, 2, 17, 14)
> facebook <- c(17, 7, 5, 16, 8, 13, 14)
> interpret <- function(num_views) {
+ if (num_views > 15) {
+ print("You're Popular")
+ }
+ else {
+ print("Try to be more visible")
+ }
+
+ }
> for( i in 1:length(linkedin))
+ {
+
+ interpret(linkedin[i])
+
+ }
[1] "You're Popular"
[1] "Try to be more visible"
[1] "Try to be more visible"
[1] "Try to be more visible"
[1] "Try to be more visible"
[1] "You're Popular"
[1] "Try to be more visible"
> for( i in 1:length(facebook))
+ {
+
+ interpret(facebook[i])
+
+ }
[1] "You're Popular"
[1] "Try to be more visible"
[1] "Try to be more visible"
[1] "You're Popular"
[1] "Try to be more visible"
[1] "Try to be more visible"
[1] "Try to be more visible"