Home > database >  Using weighted.mean() in mutate() to create rowwise weighted means
Using weighted.mean() in mutate() to create rowwise weighted means

Time:09-30

I am struggling with creating a new column, which should be constructed as row-wise weighted means of the X and Y variables. The reason I want to use weighted.mean() is the na.rm functionality which I need for my purpose.

I have the dataframe

library(tidyverse)
seed(2021)

df <- tibble(x = rnorm(10,0,5),
             y = rnorm(10,0,10))

And I try to create the new column like so:

df <- df %>% mutate(z = weighted.mean(cbind(x,y),cbind(rep(c(0.2,0.8),10))))

However, this yields a row filled with the same constant, so I do not seem to specify that I need rowwise operations. I would appreciate a guiding hand - thanks!

/F

CodePudding user response:

Try this way

df <- tibble(x = rnorm(10,0,5),
             y = rnorm(10,0,10))

df$z <- df %>% 
  rowwise %>%
  do(data.frame(
    z = weighted.mean(
      x = c(.$x, .$y),
      w = c(.2, .8)
    )
  )) %>%
  ungroup %>%
  magrittr::use_series("z")

or

df %>% 
  mutate( z= df %>% 
            rowwise %>% 
            do(data.frame(
    z = weighted.mean(
      x = c(.$x, .$y),
      w = c(.2, .8)
    )
  )))
  

            x       y     z
    <dbl>   <dbl>   <dbl>
 1  0.176  -1.95   -1.52 
 2 -3.33   -6.88   -6.17 
 3 -4.08    0.827  -0.154
 4  0.609   1.68    1.47 
 5  0.327   8.06    6.51 
 6 -8.63   -2.12   -3.42 
 7 -4.68   -8.52   -7.76 
 8  6.49  -13.0    -9.07 
 9 -2.95  -25.4   -20.9  
10  2.78    5.36    4.85

CodePudding user response:

You can do -

library(dplyr)

df %>%
  rowwise() %>%
  mutate(z = weighted.mean(c(x,y), c(0.8, 0.2))) %>%
  ungroup

#      x      y      z
#     <dbl>  <dbl>  <dbl>
# 1 -0.612  -10.8   -2.65
# 2  2.76    -2.73   1.66
# 3  1.74     1.82   1.76
# 4  1.80    15.1    4.46
# 5  4.49    16.0    6.80
# 6 -9.61   -18.4  -11.4 
# 7  1.31    16.2    4.29
# 8  4.58     1.31   3.93
# 9  0.0689  14.8    3.02
#10  8.65    15.1    9.95

Or in base R -

df$z <- apply(df, 1, function(x) weighted.mean(x, c(0.8, 0.2)))
  • Related