I am looking for an idiomatic and compact way to achieve in Julia what I would do in R with
v1=1:5;v2=5:1;out=rep(v1,times=v2);out
# 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
i.e. replicate each element in vector v1
a number of times given by the corresponding element in vector v2
. Any thoughts?
Try using VectorizedRoutines.jl:
# Pkg.clone("https://github.com/ChrisRackauckas/VectorizedRoutines.jl")
# Will be Pkg.add("VectorizedRoutines") after being added to package system
using VectorizedRoutines
v1=1:5
v2 = 5:-1:1
R.rep(v1,each = v2)
The implementation is based off of RLEVectors.jl via the suggestion of aireties (improved the typing a bit so you don't have to collect).
This is a package I started up to get together all the vectorized routines from R/MATLAB/Python so that porting functions (and ideas) easier to Julia. Feel free to open issues on the Github repository for suggestions of functions to implement, functions implemented in other packages I should know about, syntax not matching other languages, or if there are any other problems. Also feel free to give a pull request if you implement any functions like this. If you need help, don't be afraid make a pull request with the basic function and I can help you out.