Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rep() with each equals a vector

Tags:

r

sequence

I have quick question regarding sequence and each:

vect1 <- c(4, 5, 10, 3, 1) 

I want replicate with this vector as each such that first number is replicated 4, second 5, third 10, fourth 3, and fifth equal 1.

rep(1:5, each = vect1)   [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 Warning message: In rep(1:5, each = vect1) : first element used of 'each' argument  rep(1:5, each = c(4, 5, 10, 3, 1))       [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5     Warning message:     In rep(1:5, each = c(4, 5, 10, 3, 1)) :       first element used of 'each' argument 

I know this is misuse of each.

like image 450
jon Avatar asked Nov 11 '11 18:11

jon


People also ask

What does Rep () do in R?

What is the rep() function? In simple terms, rep in R, or the rep() function replicates numeric values, or text, or the values of a vector for a specific number of times.

How do you repeat a vector in R?

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.

How do I repeat the same number in R?

How do you Repeat a Sequence of Numbers in R? To repeat a sequence of numbers in R you can use the rep() function. For example, if you type rep(1:5, times=5) you will get a vector with the sequence 1 to 5 repeated 5 times.

How do you replicate a list in R?

Data Visualization using R Programming The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x),5).


1 Answers

rep(1:5, vect1) 

If you have questions about how to work functions in R, try

?function 

where "function" is whatever function you want to know about. From ?rep you would have read:

'times' A integer vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error.

like image 122
Rguy Avatar answered Oct 23 '22 17:10

Rguy