xiaodai
xiaodai

Reputation: 16014

What's Julia's equivalent of R's replicate and rep?

In R replicate(n, expr) repeatedly run the expr expression n times in contrast to rep(value, n) which repeats the value n times.

What's Julia's equivalent of R's replicate and rep?

Eg. in R

rep(1:3, 3) yields c(1:3, 1:3, 1:3)

and replicate(3, runif(1)) generates 3 random numbers from the uniform distribute (i.e. it ran runif(1) 3 times.

Upvotes: 5

Views: 1560

Answers (1)

Julia Learner
Julia Learner

Reputation: 2912

I may be late to the party, but here is some R and Julia code that combines suggestions from the comments:

# R version 3.5.1 of rep function
> rep(1:3, 3)
[1] 1 2 3 1 2 3 1 2 3

# Julia version 1.0.0 of repeat function
julia> repeat(1:3, 3)
9-element Array{Int64,1}:
 1
 2
 3
 1
 2
 3
 1
 2
 3

# R version 3.5.1 for replicate function
> replicate(3, runif(1))
[1] 0.3849424 0.3277343 0.6021007

# Julia version 1.0.0 of an array comprehension
julia> [rand() for i in 1:3]
3-element Array{Float64,1}:
 0.8076220876500786
 0.9700908450487538
 0.14006111319509862

Upvotes: 6

Related Questions