Generating a vector with random numbers repeated 'n' times - r

In RCode, How to generate a vector with 1000 values, with randomic numbers between 1:3, but each value repeat "n" times in sequence?
I know that
sample(1:3,1000, replace=TRUE)
will generate 1000 values ranging between 1 and 3, but I need each value to repeat, for example, 5 times. Like below:
[1] 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 3 3 3 3 3 2 2 2 2 2 1 1 1 1 1 3 3 3 3 3
.
.
.

We can use the rep with each
rep(sample(1:3, 1000, replace = TRUE), each = 5)

Try this:
rep(sample(1:3,1000, replace=TRUE),each=5)

Here is another option using kronecker produce
kronecker(sample(1:3,1000,replace = TRUE),rep(1,5))

Related

Create sequence based on a condition

How to conditionally increment if the previous value is greater than the current value? Say I have a column x on my data frame and I want a column y which starts from 1 and increments if the previous value is greater than the current.
x y
1 1
2 1
3 1
4 1
5 1
6 1
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
1 3
2 3
5 3
As #A5C1D2H2I1M1N2O1R2T1 mentioned, you can use cumsum with diff to generate y.
cumsum(diff(x) < 0) + 1
#[1] 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3
You might want to prepend 1 in the beginning to get y with same length as x.
c(1, cumsum(diff(x) < 0) + 1)
#[1] 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3
data
x <- c(1:6, 1:8, 1, 2, 5)

How to always ensure the shuffled samples with the designated group size in R

I am just trying to split the sample into groups with the fixed group size based on the designated probabilities by using R, but would like to always ensure that the group size for shuffled sample is always the same. For example, let's assume that the sample size is 100, the number of groups is 4, and for each group, the group size is 40, 30, 20, 10, respectively, as shown below:
category_split <- sample(1:4, 100, replace=T, prob=c(0.4,0.3,0.2,0.1))
category_split
# [1] 1 2 3 3 1 1 3 3 2 1 1 2 1 4 2 1 3 2 1 1 1 2 3 4 1 2 2 1 2 2 1 1 1 3 3 4 3 1 2 2 2 3 1 1 3 2 3 1 1 1 4 1 4 1
#[55] 1 2 3 4 1 1 1 1 2 1 3 2 2 3 1 3 3 2 1 4 1 2 1 2 3 2 3 3 1 2 1 2 3 1 1 1 1 1 3 2 3 1 1 1 2 3
table(category_split)
#category_split
# 1 2 3 4
#43 26 24 7
But, with the probabilistic nature of the sampling process, the results could not always ensure the exactly one with the same designated group size as stipulated (40, 30, 20, 10), although the results are approximately similar. Is there any way that I can get the random shuffling results with the same group size by using sample function or any other functions in R?
First create a vector with the necessary number of elements and then sample
category_split = sample(rep(1:4, c(40, 30, 20, 10)))
table(category_split)
#category_split
# 1 2 3 4
#40 30 20 10

rep and/or seq function to create continuously reducing vector?

Suppose I have a vector from 1 to 5,
a<-c(1:5)
What I need to do is to repeat the vector by losing one element continuously. That is, the final outcome should be like
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
We can reverse the vector and apply sequence
sequence(rev(a))
#[1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Or another option is toeplitz
m1 <- toeplitz(a)
m1[lower.tri(m1, diag=TRUE)]
#[1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

r repeat sequence number sequence while keeping the order of the sequence

I want repeat a sequence for specific length:
Sequence is 1:4 and I want to repeat the sequence till number of rows in a data frame.
Lets say length of the data frame is 24
I tried following:
test <- rep(1:4, each=24/4)
1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
Lengthwise this is fine but i want to retain the sequence
1 2 3 4 1 2 3 4 1 2 3 4.....
You need to use times instead of each
rep(1:4, times=24/4)
[1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
We can just pass it without any argument and it takes the times by default
rep(1:4, 24/4)
#[1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

Generating large drawing lists in R

Say I have a list in R like so,
[1] 3 5 4 7
And I want to generate all "drawings" from this list, from 1 up to the value of each number. For example,
1 1 1 1
1 1 1 2
1 1 1 3
...
2 3 3 1
2 3 3 2
2 3 3 3
...
3 5 4 7
I know I have used rep() in the past to do something very similar, which works for lists of 2 or 3 numbers (i.e. something like 1 4 5), but I'm not sure how to generalize this here.
Thoughts?
As suggested in comments, use Map function to apply seq to elements of your vector, then use expand.grid to generate data.frame with Cartesian product of result's elements:
head(expand.grid(Map(seq,c(3,5,4,7))))
Var1 Var2 Var3 Var4
1 1 1 1 1
2 2 1 1 1
3 3 1 1 1
4 1 2 1 1
5 2 2 1 1
6 3 2 1 1

Resources