rep() 每个都等于一个向量 [英] rep() with each equals a vector
问题描述
我有关于序列和 each
的快速问题:
I have quick question regarding sequence and each
:
vect1 <- c(4, 5, 10, 3, 1)
我想用这个向量进行复制,使得第一个数字被复制 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.
推荐答案
rep(1:5, vect1)
如果您对如何在 R 中使用函数有疑问,请尝试
If you have questions about how to work functions in R, try
?function
其中函数"是您想了解的任何函数.从 ?rep
你会读到:
where "function" is whatever function you want to know about. From ?rep
you would have read:
'times' 一个整数向量,给出(非负)重复次数每个元素 if 的长度为 length(x),或者重复整个向量 if长度为 1.负值或 NA 值是错误的.
'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.
这篇关于rep() 每个都等于一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!