Questions tagged [replicate]

280 questions
763
votes
10 answers

Create list of single item repeated N times

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = length of the list). How do I create the lists, without using a list comprehension [e for number in xrange(n)] for…
chimeracoder
  • 20,648
  • 21
  • 60
  • 60
202
votes
10 answers

Repeat each row of data.frame the number of times specified in a column

df <- data.frame(var1 = c('a', 'b', 'c'), var2 = c('d', 'e', 'f'), freq = 1:3) What is the simplest way to expand each row the first two columns of the data.frame above, so that each row is repeated the number of times specified in…
wkmor1
  • 7,226
  • 3
  • 31
  • 23
102
votes
3 answers

Create sequence of repeated values, in sequence?

I need a sequence of repeated numbers, i.e. 1 1 ... 1 2 2 ... 2 3 3 ... 3 etc. The way I implemented this was: nyear <- 20 names <- c(rep(1,nyear),rep(2,nyear),rep(3,nyear),rep(4,nyear), …
Wesley Burr
  • 1,224
  • 3
  • 9
  • 9
82
votes
15 answers

How do you fix a bug you can't replicate?

The question says it all. If you have a bug that multiple users report, but there is no record of the bug occurring in the log, nor can the bug be repeated, no matter how hard you try, how do you fix it? Or even can you? I am sure this has happened…
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
66
votes
5 answers

How to duplicate a MySQL database on the same server

I have a large MySQL database, lets call it live_db, which I want to replicate on the same machine to provide a test system to play around with (test_db), including table structure and data. In regular intervals I want to update the test_db with the…
Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
22
votes
5 answers

Repeating a repeated sequence

We want to get an array that looks like this: 1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4 What is the easiest way to do it?
Fabian Stolz
  • 1,935
  • 7
  • 27
  • 30
19
votes
4 answers

Using "..." and "replicate"

In the documentation of sapply and replicate there is a warning regarding using ... Now, I can accept it as such, but would like to understand what is behind it. So I've created this little contrived example: innerfunction<-function(x, extrapar1=0,…
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
13
votes
3 answers

Is there a Python equivalent of Perl's x operator (replicate string)?

In Perl, I can replicate strings with the 'x' operator: $str = "x" x 5; Can I do something similar in Python?
Mat
  • 82,161
  • 34
  • 89
  • 109
13
votes
3 answers

How to run a function multiple times and write the results to a list?

I have a function that creates a matrix and I want to call this function a thousand times such that in the end I have a list of 1000 matrices. Here is an example: set.seed(1) gen_mat <- function(x) matrix(c(1, 1, 1, x + rnorm(1)), nrow = 2) Now, I…
Joe
  • 1,628
  • 3
  • 25
  • 39
10
votes
4 answers

Repeating a user-defined function using replicate() or sapply()

I have defined a custom function, like this: my.fun = function() { for (i in 1:1000) { ... for (j in 1:20) { ... } } return(output) } which returns an output matrix, output, composed by 1000 rows and…
Stefano Lombardi
  • 1,581
  • 2
  • 22
  • 48
9
votes
3 answers

Replicating Stata Probit with robust errors in R

I am trying to replicate a Stata Output in R. I am using the dataset affairs. I am having trouble replicating the probit function with robust standard errors. The Stata code looks like that: probit affair male age yrsmarr kids relig educ…
Semprini
  • 91
  • 3
7
votes
2 answers

How to use replicateM solving the eight queens problem?

I just started learning to code Haskell so apologies if this is a stupid question. I am trying to redo the eight queens problem by making use of the [] monad. Here is the code, import Control.Monad addqueen :: [Int] -> [[Int]] addqueen xs = [ x:xs…
user2812201
  • 437
  • 3
  • 7
7
votes
4 answers

Replicate() versus a for loop?

Does anyone know how the replicate() function works in R and how efficient it is relative to using a for loop? For example, is there any efficiency difference between... means <- replicate(100000, mean(rnorm(50))) And... means <- c() for(i in…
RickyB
  • 607
  • 1
  • 8
  • 20
6
votes
3 answers

Replicate same data.frame n times and save them into a list

I just need to replicate my data.frame n times (e.g. 100) and save all the outputs into a list. It should be quite easy and straightforward but I could not find any solution yet. Fake data.frame: df = read.table(text = 'a b 1 2 5 6 4 4 11 78 23 99',…
aaaaa
  • 149
  • 2
  • 18
  • 44
6
votes
3 answers

replicate function in R returns NULL

I am confused about the output from the replicate function in R, I am trying to use it in two different ways, that (in my mind) should give a matrix as output! so, if I use replicate(5, seq(1,5,1)) I get a matrix 5x5 [,1] [,2] [,3] [,4]…
TriRook
  • 147
  • 1
  • 3
  • 18
1
2 3
18 19