Quick question about Functionals

, ,

Hi everyone, here is a super quick question: (Thank you very much for helping me!)

Screen Link: https://app.dataquest.io/m/341/working-with-functionals/3/using-functionals-to-apply-custom-functions

My Code:

percentage_of_total <- function(x) {
x/sum(x) * 100
}

map(scores$home_goals, percentage_of_total)

What I expected to happen:

$home_goals
[1] 3.947368 1.315789 1.315789 3.947368 3.947368 1.315789 1.315789 2.631579 3.947368 2.631579 5.263158 1.315789 2.631579
[14] 1.315789 0.000000 0.000000 0.000000 2.631579 2.631579 0.000000 2.631579 0.000000 2.631579 1.315789 1.315789 2.631579
[27] 2.631579 1.315789 1.315789 1.315789 0.000000 2.631579 1.315789 2.631579 0.000000 0.000000 0.000000 0.000000 2.631579
[40] 3.947368 2.631579 0.000000 1.315789 1.315789 2.631579 2.631579 1.315789 2.631579 2.631579 1.315789 2.631579 2.631579
[53] 0.000000 0.000000 1.315789 1.315789 0.000000 0.000000 1.315789

What actually happened:

[[1]]
[1] 100

[[2]]
[1] 100

[[3]]
[1] 100

[[4]]
[1] 100

[[5]]
[1] 100 (I am not showing the rest of the codes because it’s either 100 or NaN)

I know I could directly apply the function by using:

home_goals_percentage <- percentage_of_total(scores$home_goals)

Hoever, when I tried to use the functional, it returns a different result. I just want to understand here, what is the problem with my code.

Thank you for helping!

Hi @Qiulu,
Note that the map() functional returns a List as output. In the case of the expected output, map() applied the percentage_of_total function on home_goals column in a dataframe. So map() returned a List containing a vector of home_goals values.

However, in the code you executed, home_goals is a vector not part of a dataframe so map() applied the percentage_of_total function on the values in the home_goals vector and returned a List of the values.

I am an R beginner but hopefully, this helps.
Thanks!

Hi! Thank you so much. It helped! Apparently I overlooked that map() returns a list as output.

One small question from here: how exactly does R perform the code I put in? for example, for the fist element, it returns “100”, I am curious how was it calculated.

Here is the scores$home_goals vector: c(3 1 1 3 3 1 1 2 3 2 4 1 2 1 0 0 0 2 2 0 2 0 2 1 1 2 2 1 1 1 0 2 1 2 0 0 0 0 2 3 2 0 1 1 2 2 1 2 2 1 2 2 0 0 1 1 0 0 1)

Thank you again!

I was curious about that too so I run a small test to see what is going on. In line with my earlier response, regarding your code, what happens is that map() applies the percentage_of_total function to each value in the home_goals vector separately so sum(x) actually does not sum the entire home_goals vector. So, for example, when you take the first value 3 in the home_goals vector, percentage_of_total function will execute as follows: 3/sum(3) * 100 = 100; where you have zeros, the function returns NaN.

Glad to know my earlier response helped. If this helps too, please mark both as solution.
Thanks!

1 Like

it totally makes sense. now I see. Thanks for helping!

1 Like

You’re welcome! Glad to have helped.