4
votes

I want to generate a column of normal random variables with mean defined in the dep variable. However, I got the non-random results.

I know there are other ways to do it like apply functions (sapply(1:5, rnorm, n=1)), but I am just curious to know how to do it using dplyr and why I got the mistakes.

library(dplyr)
data_frame(dep = 1:5) %>% 
        mutate(normal_mean = rnorm(1, mean=dep))
Source: local data frame [5 x 2]

dep normal_mean
1   1    1.574045
2   2    1.574045
3   3    1.574045
4   4    1.574045
5   5    1.574045
2
actually I just found group by row using rowwise() resolve the issueYifei

2 Answers

4
votes

I think rowwise is very slow. Instead, you should just correct the first argument to rnorm:

data.frame(dep=1:5) %>% mutate(normal_mean = rnorm(n(), mean=dep))
2
votes

Try adding rowwise()

library(dplyr)
data_frame(dep = 1:5) %>% 
  rowwise() %>%
  mutate(
    normal_mean = rnorm(1, mean=dep)
  )

  dep normal_mean
1   1   2.0999493
2   2   0.8764449
3   3   6.4460789
4   4   3.2802778
5   5   4.6731459