R - Make a repetitive sequence with 'rep'

五迷三道 提交于 2019-12-10 19:52:23

问题


I'm wondering if there is a simpler way to make a list with, for example 10 '4', 20 '6' and 30 '3' then writing it by hand (example <- c(4,4,4,4,...)) with the function 'rep'. I know i can repeat a certain sequence n times and each by n times too, but i don't know how can i make one with different amounts of each number.


回答1:


Just use rep with both arguments being the desired vectors:

x <- rep(c(4, 6, 3), c(10, 20, 30))
table(x)

 3  4  6 
30 10 20 


来源:https://stackoverflow.com/questions/49557890/r-make-a-repetitive-sequence-with-rep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!