Unexpected mutate behavior with dnorm()

dplyr

#1

Hey all,

I’m having some trouble understanding why the following code does not cause an error.

posterior <- data_frame(
  mu = seq(from = 140, to = 160, length.out = 200),
  sigma = seq(from = 4, to = 9, length.out = 200)
)

height = rnorm(10, mean = 180, sd = 15)

mutate(posterior, LL = dnorm(height, mean = mu, sd = sigma))

In the last line, I create a new column called LL, using dnorm(). However, dnorm() returns 10 values, which should cause an error because it’s not 1 or the length of the data frame.

Am I missing something here?


#2

The length of the vector returned by dnorm is the length of the longest vector passed in as an argument. From the doc’s

“The length of the result is determined by n for rnorm, and is the maximum of the lengths of the numerical arguments for the other functions.”

Here is an reprex ( https://www.jessemaegan.com/post/so-you-ve-been-asked-to-make-a-reprex/ ) that shows the calculations that are made in the mutate.

suppressPackageStartupMessages(library(tidyverse))

posterior <- data_frame(
    mu = seq(from = 140, to = 160, length.out = 200),
    sigma = seq(from = 4, to = 9, length.out = 200)
)

height = rnorm(10, mean = 180, sd = 15)

d0 <- mutate(posterior, LL = dnorm(height, mean = mu, sd = sigma))


# the length of dnorm is the length of the longest of its arguments. See docs
# "The length of the result is determined by n for rnorm,
# and is the maximum of the lengths of the numerical arguments for the other functions."

# longest arg is 100
d <- dnorm(height, 1:100, 1:40)
# length of result is 100
length(d)
#> [1] 100

# In the mutate you are calculating against columns of posterior
# length of each column is 200
d2 <- dnorm(height, posterior$mu, posterior$sigma)
# so length of result is 200
length(d2)
#> [1] 200
# and the values are taken from corresonding position of the result
# here you can see that the values in d2 are the same as those
# in d0$LL, the result of the mutation you have done.
identical(d2, d0$LL)
#> [1] TRUE
# and these are the values
d2
#>   [1] 8.521692e-23 4.184289e-16 1.507475e-14 8.350397e-04 1.212874e-26
#>   [6] 1.742231e-14 1.141794e-15 6.972448e-02 8.829001e-12 2.806612e-31
#>  [11] 1.820626e-19 1.010371e-13 2.126270e-12 2.574261e-03 7.100334e-23
#>  [16] 2.214531e-12 2.059282e-13 7.865211e-02 4.351490e-10 5.082012e-27
#>  [21] 9.392330e-17 8.630620e-12 1.170515e-10 6.062531e-03 8.755270e-20
#>  [26] 1.136527e-10 1.421372e-11 8.202380e-02 1.030133e-08 1.683672e-23
#>  [31] 1.595045e-14 3.275440e-10 3.090190e-09 1.161968e-02 3.167706e-17
#>  [36] 2.863876e-09 4.611515e-10 8.077830e-02 1.370501e-07 1.449973e-20
#>  [41] 1.124045e-12 6.547694e-09 4.563903e-08 1.899288e-02 4.315653e-15
#>  [46] 4.104245e-08 8.209925e-09 7.629098e-02 1.153473e-06 4.237269e-18
#>  [51] 3.918044e-11 7.843891e-08 4.238146e-07 2.742019e-02 2.678428e-13
#>  [56] 3.741515e-07 9.026168e-08 6.988691e-02 6.722941e-06 5.157840e-16
#>  [61] 7.731072e-10 6.217735e-07 2.707089e-06 3.590761e-02 8.773897e-12
#>  [66] 2.365469e-06 6.714198e-07 6.261982e-02 2.910162e-05 3.067816e-14
#>  [71] 9.591377e-09 3.522351e-06 1.275381e-05 4.353123e-02 1.702170e-10
#>  [76] 1.109486e-05 3.629204e-06 5.522665e-02 9.881851e-05 1.010992e-12
#>  [81] 8.125269e-08 1.514962e-05 4.681851e-05 4.963394e-02 2.142142e-09
#>  [86] 4.071419e-05 1.508121e-05 4.816805e-02 2.748445e-04 2.039972e-11
#>  [91] 5.018517e-07 5.190114e-05 1.398761e-04 5.388710e-02 1.880533e-08
#>  [96] 1.219447e-04 5.039460e-05 4.169606e-02 6.480670e-04 2.730814e-10
#> [101] 2.381796e-06 1.471750e-04 3.521714e-04 5.625301e-02 1.220966e-07
#> [106] 3.084055e-04 1.403925e-04 3.591986e-02 1.331857e-03 2.587877e-09
#> [111] 9.062696e-06 3.563111e-04 7.685348e-04 5.690035e-02 6.147987e-07
#> [116] 6.769523e-04 3.357600e-04 3.085848e-02 2.439697e-03 1.830520e-08
#> [121] 2.861817e-05 7.552697e-04 1.487315e-03 5.611389e-02 2.495893e-06
#> [126] 1.318856e-03 7.060186e-04 2.647872e-02 4.056918e-03 1.009432e-07
#> [131] 7.715511e-05 1.430895e-03 2.600769e-03 5.422048e-02 8.434540e-06
#> [136] 2.322852e-03 1.331163e-03 2.272065e-02 6.216673e-03 4.498576e-07
#> [141] 1.817957e-04 2.464477e-03 4.173089e-03 5.153839e-02 2.436343e-05
#> [146] 3.755086e-03 2.287302e-03 1.951369e-02 8.888040e-03 1.669438e-06
#> [151] 3.817101e-04 3.913531e-03 6.223300e-03 4.834841e-02 6.149522e-05
#> [156] 5.642410e-03 3.630398e-03 1.678608e-02 1.197878e-02 5.289858e-06
#> [161] 7.258701e-04 5.797445e-03 8.717978e-03 4.488130e-02 1.381665e-04
#> [166] 7.963870e-03 5.382992e-03 1.447010e-02 1.535013e-02 1.461680e-05
#> [171] 1.267281e-03 8.090921e-03 1.157486e-02 4.131609e-02 2.806660e-04
#> [176] 1.065201e-02 7.527467e-03 1.250457e-02 1.883808e-02 3.585401e-05
#> [181] 2.054775e-03 1.072622e-02 1.467505e-02 3.778449e-02 5.223122e-04
#> [186] 1.360250e-02 1.000702e-02 1.083574e-02 2.227546e-02 7.926459e-05
#> [191] 3.124363e-03 1.360260e-02 1.787977e-02 3.437809e-02 9.005228e-04
#> [196] 1.668864e-02 1.273319e-02 9.417210e-03 2.551034e-02 1.599893e-04
d0$LL
#>   [1] 8.521692e-23 4.184289e-16 1.507475e-14 8.350397e-04 1.212874e-26
#>   [6] 1.742231e-14 1.141794e-15 6.972448e-02 8.829001e-12 2.806612e-31
#>  [11] 1.820626e-19 1.010371e-13 2.126270e-12 2.574261e-03 7.100334e-23
#>  [16] 2.214531e-12 2.059282e-13 7.865211e-02 4.351490e-10 5.082012e-27
#>  [21] 9.392330e-17 8.630620e-12 1.170515e-10 6.062531e-03 8.755270e-20
#>  [26] 1.136527e-10 1.421372e-11 8.202380e-02 1.030133e-08 1.683672e-23
#>  [31] 1.595045e-14 3.275440e-10 3.090190e-09 1.161968e-02 3.167706e-17
#>  [36] 2.863876e-09 4.611515e-10 8.077830e-02 1.370501e-07 1.449973e-20
#>  [41] 1.124045e-12 6.547694e-09 4.563903e-08 1.899288e-02 4.315653e-15
#>  [46] 4.104245e-08 8.209925e-09 7.629098e-02 1.153473e-06 4.237269e-18
#>  [51] 3.918044e-11 7.843891e-08 4.238146e-07 2.742019e-02 2.678428e-13
#>  [56] 3.741515e-07 9.026168e-08 6.988691e-02 6.722941e-06 5.157840e-16
#>  [61] 7.731072e-10 6.217735e-07 2.707089e-06 3.590761e-02 8.773897e-12
#>  [66] 2.365469e-06 6.714198e-07 6.261982e-02 2.910162e-05 3.067816e-14
#>  [71] 9.591377e-09 3.522351e-06 1.275381e-05 4.353123e-02 1.702170e-10
#>  [76] 1.109486e-05 3.629204e-06 5.522665e-02 9.881851e-05 1.010992e-12
#>  [81] 8.125269e-08 1.514962e-05 4.681851e-05 4.963394e-02 2.142142e-09
#>  [86] 4.071419e-05 1.508121e-05 4.816805e-02 2.748445e-04 2.039972e-11
#>  [91] 5.018517e-07 5.190114e-05 1.398761e-04 5.388710e-02 1.880533e-08
#>  [96] 1.219447e-04 5.039460e-05 4.169606e-02 6.480670e-04 2.730814e-10
#> [101] 2.381796e-06 1.471750e-04 3.521714e-04 5.625301e-02 1.220966e-07
#> [106] 3.084055e-04 1.403925e-04 3.591986e-02 1.331857e-03 2.587877e-09
#> [111] 9.062696e-06 3.563111e-04 7.685348e-04 5.690035e-02 6.147987e-07
#> [116] 6.769523e-04 3.357600e-04 3.085848e-02 2.439697e-03 1.830520e-08
#> [121] 2.861817e-05 7.552697e-04 1.487315e-03 5.611389e-02 2.495893e-06
#> [126] 1.318856e-03 7.060186e-04 2.647872e-02 4.056918e-03 1.009432e-07
#> [131] 7.715511e-05 1.430895e-03 2.600769e-03 5.422048e-02 8.434540e-06
#> [136] 2.322852e-03 1.331163e-03 2.272065e-02 6.216673e-03 4.498576e-07
#> [141] 1.817957e-04 2.464477e-03 4.173089e-03 5.153839e-02 2.436343e-05
#> [146] 3.755086e-03 2.287302e-03 1.951369e-02 8.888040e-03 1.669438e-06
#> [151] 3.817101e-04 3.913531e-03 6.223300e-03 4.834841e-02 6.149522e-05
#> [156] 5.642410e-03 3.630398e-03 1.678608e-02 1.197878e-02 5.289858e-06
#> [161] 7.258701e-04 5.797445e-03 8.717978e-03 4.488130e-02 1.381665e-04
#> [166] 7.963870e-03 5.382992e-03 1.447010e-02 1.535013e-02 1.461680e-05
#> [171] 1.267281e-03 8.090921e-03 1.157486e-02 4.131609e-02 2.806660e-04
#> [176] 1.065201e-02 7.527467e-03 1.250457e-02 1.883808e-02 3.585401e-05
#> [181] 2.054775e-03 1.072622e-02 1.467505e-02 3.778449e-02 5.223122e-04
#> [186] 1.360250e-02 1.000702e-02 1.083574e-02 2.227546e-02 7.926459e-05
#> [191] 3.124363e-03 1.360260e-02 1.787977e-02 3.437809e-02 9.005228e-04
#> [196] 1.668864e-02 1.273319e-02 9.417210e-03 2.551034e-02 1.599893e-04
d0
#> # A tibble: 200 x 3
#>       mu sigma                                  LL
#>    <dbl> <dbl>                               <dbl>
#>  1   140  4.00 0.0000000000000000000000852        
#>  2   140  4.03 0.000000000000000418               
#>  3   140  4.05 0.0000000000000151                 
#>  4   140  4.08 0.000835                           
#>  5   140  4.10 0.0000000000000000000000000121     
#>  6   141  4.13 0.0000000000000174                 
#>  7   141  4.15 0.00000000000000114                
#>  8   141  4.18 0.0697                             
#>  9   141  4.20 0.00000000000883                   
#> 10   141  4.23 0.000000000000000000000000000000281
#> # ... with 190 more rows

Created on 2018-02-24 by the reprex package (v0.2.0).


#3

I missed that line in the docs; thanks!