重复特定行在R中的给定次数

发布于 2025-01-17 19:48:30 字数 536 浏览 0 评论 0原文

我有矩阵,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2025-01-24 19:48:30

使用序列

s <- rep(seq(nrow(x) / 2), y)
x[sequence(rep(2, length(s)), s*2-1), ]

#       [,1] [,2]
#  [1,]    1  101
#  [2,]    1  103
#  [3,]    1  101
#  [4,]    1  103
#  [5,]    2  102
#  [6,]    2  103
#  [7,]    2  102
#  [8,]    2  103
#  [9,]   10  111
# [10,]   10  112
# [11,]   10  111
# [12,]   10  112
# [13,]   20  120
# [14,]   20  121
# [15,]   21  120
# [16,]   21  121
# [17,]   30  130
# [18,]   30  131
# [19,]   31  130
# [20,]   31  131
# [21,]   40  140
# [22,]   40  141

Using sequence:

s <- rep(seq(nrow(x) / 2), y)
x[sequence(rep(2, length(s)), s*2-1), ]

#       [,1] [,2]
#  [1,]    1  101
#  [2,]    1  103
#  [3,]    1  101
#  [4,]    1  103
#  [5,]    2  102
#  [6,]    2  103
#  [7,]    2  102
#  [8,]    2  103
#  [9,]   10  111
# [10,]   10  112
# [11,]   10  111
# [12,]   10  112
# [13,]   20  120
# [14,]   20  121
# [15,]   21  120
# [16,]   21  121
# [17,]   30  130
# [18,]   30  131
# [19,]   31  130
# [20,]   31  131
# [21,]   40  140
# [22,]   40  141
開玄 2025-01-24 19:48:30
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)


s <- rep(seq_len(8),  c(2,2,2,1,1,1,1,1))

x[as.vector(rbind(s*2 -1, s*2)),]

##>
##>      [,1] [,2]
##> [1,]    1  101
##> [2,]    1  103
##> [3,]    1  101
##> [4,]    1  103
##> [5,]    2  102
##> [6,]    2  103
##> [7,]    2  102
##> [8,]    2  103
##> [9,]   10  111
##>[10,]   10  112
##>[11,]   10  111
##>[12,]   10  112
##>[13,]   20  120
##>[14,]   20  121
##>[15,]   21  120
##>[16,]   21  121
##>[17,]   30  130
##>[18,]   30  131
##>[19,]   31  130
##>[20,]   31  131
##>[21,]   40  140
##>[22,]   40  141  
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)


s <- rep(seq_len(8),  c(2,2,2,1,1,1,1,1))

x[as.vector(rbind(s*2 -1, s*2)),]

##>
##>      [,1] [,2]
##> [1,]    1  101
##> [2,]    1  103
##> [3,]    1  101
##> [4,]    1  103
##> [5,]    2  102
##> [6,]    2  103
##> [7,]    2  102
##> [8,]    2  103
##> [9,]   10  111
##>[10,]   10  112
##>[11,]   10  111
##>[12,]   10  112
##>[13,]   20  120
##>[14,]   20  121
##>[15,]   21  120
##>[16,]   21  121
##>[17,]   30  130
##>[18,]   30  131
##>[19,]   31  130
##>[20,]   31  131
##>[21,]   40  140
##>[22,]   40  141  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文