R: How to apply a function to a vector and get vectors of different lengths?


username

I have a list of methods like

avgs = c(1,2,3)

and a function:

simulate <- function (avg)
{ rnorm(n=10,m=avg,sd=1) }

What's the best way to get a vector of 30 values ​​instead of a multidimensional array from it sapply(avgs,simulate)?

Payton

In your case, just take advantage of the fact that rnormvectorization accepts the entire vector as a parameter:

rnorm(30, avgs, 1)

You can also remove dimensions from a matrix using c:

c(sapply(avgs, simulate))

But this method is slower and less straightforward.

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