重复特定行在R中的给定次数
我有矩阵,
x=matrix(c(1,1,2,2,10,10,20,20,21,21,30,30,31,31,40,40,
101,103,102,103,111,112,120,121,120,121,130,131,130,131,140,141),16,2)
我想重复每排的x
给定次数,这是基于y = c(2,2,2,2,1,1,1,1,1 ,1)
。
我的意思是,x
的前两行重复两次(y [1]
等于2),接下来的两个行x 重复两次(
y [2]
等于2),依此类推。 x
的最后两行重复一次,因为(y [8]
等于1)等于1。
我已经尝试了rep
,但它重复每行,但不是每两行。
我不想使用任何软件包,只是基础。另外,我想避免使用循环的任何。
I have the matrix
x=matrix(c(1,1,2,2,10,10,20,20,21,21,30,30,31,31,40,40,
101,103,102,103,111,112,120,121,120,121,130,131,130,131,140,141),16,2)
I want to repeat each two rows of x
a given number of times, that is based on y=c(2,2,2,1,1,1,1,1)
.
I mean that the first two rows of x
are repeated two times (y[1]
is equal to 2), the next two rows of x
are repeated two times (y[2]
is equal to 2), and so on. The last two rows of x
are repeated once since (y[8]
is equal to 1) is equal to one.
I have tried with rep
but it repeats each row, but not every two rows.
I do not want to use any package, just base. Also, I want to avoid any for
loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
序列
:Using
sequence
: