【问题标题】:How to split one vector in to multiple vectors with pattern in R如何将一个向量拆分为具有R中模式的多个向量
【发布时间】:2020-05-17 17:07:02
【问题描述】:

我有一个向量,我想用某种模式将它拆分成多个向量。例如:

a table x with a vector  of 14 numbers like:
x
1
2
3
4
5
6
7
8
9
10
11
12
13
14

我想根据上面的向量创建一个包含多个向量的新表

n=2,m1=3,m2=4
column n=2, for column1:row=n*m1 and column2: row= n*m2 (Here, the number could be variables)

1   7
2   8
3   9
4   10
5   11
6   12
    13
    14

非常感谢

【问题讨论】:

  • n <- 2; split(1:14, rep(1:2, n*3:4)) 也许吧。
  • 如果表中你的意思是data.frame,这可能几乎是你想要的? stackoverflow.com/questions/46380954/…
  • n
  • 嗨,snd,因为链接的解决方案看起来不是我想要的。我无法理解编码。有更简单的解决方案吗?
  • @Michale 如果您看到错误“数据长度不是拆分变量的倍数”,您应该提供模拟您的实际问题的示例数据。

标签: r


【解决方案1】:

markus 的解决方案是正确的。非常感谢。

n <- 2; split(1:14, rep(1:2, n*3:4))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2021-09-15
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多