Questions tagged [replicate]
280 questions
3
votes
1 answer
R how to grow a data.table
I've a large data.table which looks like
custid, dayofweek, revenue
AA 2 345
AA 3 545
BB 1 544
BB 4 456
CC 7 231
I would like to "grow" this data table such that it has all 7 numbers for each custid with the revenue column set to NA. Example shown…

broccoli
- 4,738
- 10
- 42
- 54
2
votes
1 answer
What are the difference between 'rexp(1000, 1)' and 'replicate(1000, rexp(1,1))' in R?
I am trying to generate 1000 numbers using exponential distribution with parameter 1.
After setting the seed value to 1, I tried both rexp(1000, 1) and replicate(1000, rexp(1, 1)), but the medians of the resulting two vectors are different.
I…

Kana
- 21
- 3
2
votes
2 answers
R: Repeat rows of a data.frame k times and add prefix to new row values
I know from this answer how to duplicate rows of a dataframe. That's fine if you want to repeat the rows n times.
I want to do something similar but to also add a prefix within a column of newly added rows. Note: I am not basing the repetition…

Union find
- 7,759
- 13
- 60
- 111
2
votes
1 answer
Replicate/duplicate first value (column A) over all columns in entire row
Does anyone know, how to get from this:
A
B
C
D
E
F
G
Rate
RS
1.0
1.0
1.0
1.0
1.0
1.0
to this:
A
B
C
D
E
F
G
Rate
RS
RS
RS
RS
RS
RS
RS

carrera997
- 27
- 5
2
votes
1 answer
use of curly brackets within replicate function in R
B <- 10000
results <- replicate(B, {
hand <- sample(hands1, 2)
(hand[1] %in% aces & hand[2] %in% facecard) | (hand[2] %in% aces & hand[1] %in% facecard)
})
mean(results)
this piece of code works perfectly and do the desired thi
this is a monte…

Nadika Dushan
- 25
- 5
2
votes
1 answer
Extending rep to matrices?
If you call rep on a matrix, it repeats its elements rather than the entire matrix. The traditional fix is to call rep(list(theMatrix),...). I want to extend rep so that it does this automatically.
I attempted to use
rep.matrix<-function(x,...)…

J. Mini
- 1,868
- 1
- 9
- 38
2
votes
2 answers
Transform dataframe by repeating rows and create a variable counting values of two variables
This is a little subset of the data :
I have :
df
ID numberPOS numberNEG
1 2 3
2 5 4
3 1 2
and my wish is to transform dataframe with a new variable statut counting the number of times negative…

Lionel Lellouche
- 45
- 3
2
votes
1 answer
Function to replicate rows of dataframe if column contains list
I am not sure the best way to title this. If I have a dataframe and one of the columns, lets call it 'Tags', may contain a list or may not. If 'Tags' is a list, then I want to replicate that row as many times as there are unique items in the 'Tags'…

alws_cnfsd
- 105
- 6
2
votes
2 answers
How to replicate a String in a dataframe individually N times
I have a dataframe and I want to replicate the input of a single cell n times dependent on the input of the next cell and display it in a new cell.
My dataframe looks like this:
data <- data.frame(c(1,1,2,3,4,4,4), c("A","B","A","C","D","E","A"),…

Oliver
- 43
- 3
2
votes
1 answer
how to run an arbitrary number of generations in the bunny invasion problem
I'm working through a problem in the haskell wikibook and am totally stuck. They ask to "Generalize the bunny invasion example in the list monad chapter for an arbitrary number of generations."
The description of the bunny invasion example:
"It is…

Joe Williamson
- 159
- 2
- 11
2
votes
2 answers
How to count number of occurence in a large dataset
I'm trying to count the number of occurence of each "scenarios" that I have (0 to 9) in a data frame over 25 years.
Basically, I have 10000 simulations of scenarios named 0 to 9, each scenario having a probability of occurence.
My dataframe is too…

Jng
- 51
- 1
- 6
2
votes
3 answers
Get the first elements of a list of tuples
I have this list of tuples
[(4,'a'), (1,'b'), (2,'c'), (2,'a'), (1,'d'), (4,'e')]
I want to get the first elements of every tuple then replicate it to make the following: "aaaabccaadeeee"
I came up with this code, but it only gives me the…

Tyler Joe
- 357
- 2
- 18
2
votes
2 answers
Replicate vectors shifting them to the right
In Matlab, I have two single row (1x249) vectors in a 2x249 matrix and I have to create a matrix A by replicating them many times, each time shifting the vectors of 2 positions to the right. I would like to fill the entries on the left with zeros.…

cholo14
- 594
- 1
- 3
- 22
2
votes
1 answer
conditionally duplicating rows in a data frame
This is a sample of my data set:
day city count
1 1 A 50
2 2 A 100
3 2 B 110
4 2 C 90
Here is the code for reproducing it:
df <- data.frame(
day = c(1,2,2,2),
city = c("A","A","B","C"),
count =…

Milad
- 63
- 1
- 7
2
votes
1 answer
Why I can't read replicate data in MongoDB?
I have created a replica Set with 4 MongoD running.
port: 2017, 2018, 2019, 2020
Very standard. I followed the instruction as in the documentation.
Then I set 2020 be the primary one. And then I insert data.
Then I go back and login to another…

murvinlai
- 48,919
- 52
- 129
- 177