Reputation: 348
tibble(x=c(1,2,3)) %>%
mutate(y = runif(1))
This results in the same quantity assigned to each Y. Why is this and how can i ensure the function I supply is called once for each row, regardless of the value and type of the arguments?
Upvotes: 2
Views: 36
Reputation: 34376
The n
argument of runif()
takes either an integer or a vector of values. If a vector is used its length is taken to be the number required. This means you can pass it a vector of any class and it will return a result of equal length.
library(dplyr)
tibble(x=c(1,2,3)) %>%
mutate(y = runif(x))
# A tibble: 3 x 2
x y
<dbl> <dbl>
1 1 0.445
2 2 0.778
3 3 0.632
Upvotes: 3