Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a rep() function in a package in Julia?

Tags:

r

julia

I am looking for a function in Julia that has can take values similar to this R code:

rep(1, ncol(X))

I know I can use the package DataFrames for the length function for the ncol() function in R, but I can't find a rep function in Julia. Thank you!

like image 914
A. Hartman Avatar asked Jun 07 '18 15:06

A. Hartman


1 Answers

The equivalent of rep in Julia is repeat. As arguments it takes an AbstractArray and two keyword arguments innner (like each in R) and outer (like times in R). The benefit of repeat is that it works consistently with multidimensional arrays (you can have a look at the documentation for details).

For example:

julia> repeat([1,2,3], inner=2, outer=3)
18-element Array{Int64,1}:
 1
 1
 2
 2
 3
 3
 1
 1
 2
 2
 3
 3
 1
 1
 2
 2
 3
 3

in Julia gives you the same as:

> rep(c(1,2,3), each=2, times=3)
 [1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3

in R.

EDIT: If you want to repeat a scalar use fill, e.g.:

julia> fill(1, 5)
5-element Array{Int64,1}:
 1
 1
 1
 1
 1
like image 131
Bogumił Kamiński Avatar answered Sep 23 '22 04:09

Bogumił Kamiński