Reputation: 403
I'm sorry if this is a repeat but I can't find it:
I'm trying to use the . placeholder with the pipe (%>%) from magrittr, and it seems not to work when it's the second call.
For instance, this works:
data.frame(t = c(1.1,2.2,3.3), y = c(1,2,3)) %$% (t-y)^2 %>% sum(.)
But this doesn't:
data.frame(t = c(1.1,2.2,3.3), y = c(1,2,3)) %$% (t-y)^2 %>% sum(.)/length(.)
Any intuition for why this is happening? Thanks!
Danny
Upvotes: 3
Views: 1881
Reputation: 886938
We need to place it inside the braces for evaluating as a unit
data.frame(t = c(1.1,2.2,3.3), y = c(1,2,3)) %$%
(t-y)^2 %>%
{sum(.)/length(.)}
#[1] 0.04666667
which is the same as mean
data.frame(t = c(1.1,2.2,3.3), y = c(1,2,3)) %$%
(t-y)^2 %>%
mean
#[1] 0.04666667
Upvotes: 2