所以这是一个非常奇怪的问题,但是我在 R 中有两个向量:一个是 ID 的顺序列表,另一个是我希望该 id 在新向量中出现多少次。
例如:
ids: A B C D
times: 4 2 5 3
我想结束
new: A A A A B B C C C C C D D D
这有意义吗?
使用rep
函数如下:
> rep(c('A', 'B', 'C', 'D'), times=c(4, 2, 5, 3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"
采用rep()
> rep(LETTERS[1:4], times = c(4,2,5,3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"
这当然是有道理的。
rep(c("A","B","C","D"),times=c(4,2,5,3))
[1] "A" "A" "A" "A" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"