1

所以这是一个非常奇怪的问题,但是我在 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

这有意义吗?

4

3 回答 3

5

使用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"
于 2013-02-22T21:56:44.343 回答
1

采用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"
于 2013-02-22T21:56:29.473 回答
1

这当然是有道理的。

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"
于 2013-02-22T21:57:21.263 回答