Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

r - rep() with each equals a vector

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...