How to split a vector into sub-vectors of different lengths using R


Daniel James

I want to split a vector into sub-vectors of different lengths by stating the average length of the sub-vectors.

I was able to find an answer that only gave clues but not the answer I wanted

ts <- 1:23 # the parent vector
bs <- 3 # lenght of subvector
nb <- length(ts) / bs # number of subvector
split(ts, rep(1:nb, each=bs, length.out = length(ts)))
#$`1`
#[1] 1 2 3

#$`2`
#[1] 4 5 6

#$`3`
#[1] 7 8 9

#$`4`
#[1] 10 11 12

#$`5`
#[1] 13 14 15

#$ `6`
#[1] 16 17 18

#$ `7`
#[1]

what i want is

Average length is 4 Variance length is 2

the nature i want

#$`1`
#[1] 1 2 

#$`2`
#[1] 3 4 5 6

#$`3`
#[1] 7 8 9

#$`4`
#[1] 10 

#$`5`
#[1] 11 12 13 14 15 16

#$`6`
#[1] 17 18

#$`7`
#[1] 19 20 21 23 23
Jay

We can create a normally distributed density vector denswith length(ts), mean 4and variance 2. From this, we calculate the probability probsused to draw the random sample()length s ts. From this, we can sample binsthe desired length of split() ts. To ensure that the bins actually have the desired mean and variance , we can pack the whole thing into a repeatloop until we all.equal()have a specific tolsetting yield .TRUE

ts <- 1:23  # the parent vector
bs <- 3  # lenght of subvector
nb <- length(ts) / bs  # number of subvector


set.seed(69429)
repeat {
  dens <- dnorm(ts, mean=4, sd=sqrt(2))  # density
  probs <- dens/sum(dens)  # probabilities
  sizes <- sample(length(ts), size=nb, replace=TRUE, prob=probs)  # sample bin sizes
  bins <- as.numeric(sort(factor(
    sample(nb, length(ts), replace=TRUE),  # sample bins
    levels=1:length(ts))))
  if (all.equal(c(mean(table(bins)), var(table(bins))), c(4, 2), tol=.1) == TRUE) {
    break
  }
}
bins
# [1] 1 1 1 1 2 2 2 2 3 3 3 3 3 3 4 4 4 5 5 6 6 6 6

Split

(S <- split(ts, as.numeric(bins)))
# $`1`
# [1] 1 2 3 4
# 
# $`2`
# [1] 5 6 7 8
# 
# $`3`
# [1]  9 10 11 12 13 14
# 
# $`4`
# [1] 15 16 17
# 
# $`5`
# [1] 18 19
# 
# $`6`
# [1] 20 21 22 23

check

c(mean=mean(lengths(S)), var=var(lengths(S)))
#     mean      var 
# 3.833333 1.766667

Related


How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

How to bind vectors of different lengths in R?

Toby I need to combine some named numeric vectors in R into one dataframe. I tried it cbind.nain another question as a suggestion hint, but the name won't be considered. example: v1 <- c(1,5,6,7) names(v1) <- c("milk", "flour", "eggs", "sugar") v2 <- c(2,3) na

How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

How to bind vectors of different lengths in R?

Toby I need to combine some named numeric vectors in R into one dataframe. I tried it cbind.nain another question as a suggestion hint, but the name won't be considered. example: v1 <- c(1,5,6,7) names(v1) <- c("milk", "flour", "eggs", "sugar") v2 <- c(2,3) na

How to bind vectors of different lengths in R?

Toby I need to combine some named numeric vectors in R into one dataframe. I tried it cbind.nain another question as a suggestion hint, but the name won't be considered. example: v1 <- c(1,5,6,7) names(v1) <- c("milk", "flour", "eggs", "sugar") v2 <- c(2,3) na

How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

How to generate multiple vectors of different lengths in R?

Matthew Maylin I want to use a for loop to generate multiple vectors and save their values for later use. The ideal end result is: vector_1 = c(1) vector_2 = c(1,2,3) vector_3 = c(1,2,3,4,5,6) . . . vector_i = c(1,2,3,...,n) #for some n generated during the lo

R: Comparing vectors of different lengths

Selm I'm actually having trouble expressing my problem, so if anyone has feedback on this, I'd love to hear it. I'm working in R and have a vector and a dataframe of varying lengths: xp.data <- c(400,500,600,700) XPTable <- data.frame("Level"=1:10,"XP"=c(10,50

How to compare different lengths of vectors?

FRV I have two vectors: A <- 10 10 20 19 24 24 17 18 24 24 24 25 16 16 16 25 25 12 12 12 25 24 24 24 24 2 2 and B <- 2 4 2 2 2 3 2 3 2 3 2 I want to compare the first two elements of A (here: ) 10 10. Why? Because the first entry of B is two. Then, I want

How to add vectors of different lengths?

i I am trying to add two vectors. a is 41, 5 b is 28, 5, 3, 1 i just try to do this c <- a + b The answer is 69, 10, 44, 6. I guess it's reused? I want c 69, 10, 3, 1after adding a and b together. I have absolutely no experience with R, so please keep the sol

How to compare different lengths of vectors?

FRV I have two vectors: A <- 10 10 20 19 24 24 17 18 24 24 24 25 16 16 16 25 25 12 12 12 25 24 24 24 24 2 2 and B <- 2 4 2 2 2 3 2 3 2 3 2 I want to compare the first two elements of A (here: ) 10 10. Why? Because the first entry of B is two. Then, I want

How to fill R data.frame with named vectors of different lengths?

Moshe Abramowitz I need to populate an R data.frame (or data.table) with named vectors as rows. The problem is that named vectors used as rows usually don't have all the variables. In other words, usually the length of named vectors is less than the number of

Merge vectors of different lengths into a dataframe in R

chunjin I have four vectors like this: x1=letters[1:5] x2=c("a","b","c") x3=c("a","b","c","d") x4=c("a","b","e") Actually, I want to get a dataframe like this: data.frame(x1,x2=c("a","b","c",NA,NA), x3=c("a","b","c","d",NA), x4=c("a","

Storing vectors of different lengths as lists in R

username For the code below, I want to store each (a*b)edge in a vector op1and then store all the vectors in a list opp1. Based on the given data: I should have 5 vectors, op1each length(op1[1]=3)with a different length (eg: while the rest of the vectors are o

Merge vectors of different lengths into a dataframe in R

chunjin I have four vectors like this: x1=letters[1:5] x2=c("a","b","c") x3=c("a","b","c","d") x4=c("a","b","e") Actually, I want to get a dataframe like this: data.frame(x1,x2=c("a","b","c",NA,NA), x3=c("a","b","c","d",NA), x4=c("a","