使用seq / rep在R中创建数据集

时间:2017-11-21 02:48:34

标签: r

如何在不手动输入数字的情况下在R中创建此数据集?

dataset

2 个答案:

答案 0 :(得分:3)

不确定您是否需要矩阵或数据框,并且不确定我的解决方案是否有太多的输入",但我希望它有用。

matrix(c(1:20, 1:20*2, rep(seq(3, 15, by = 3), 4),
         sapply(seq(4, 20, by = 4), function(x) x * 1:4),
         1:5, -5:-1, -6:-10, 6:10), ncol = 5)
#       [,1] [,2] [,3] [,4] [,5]
#  [1,]    1    2    3    4    1
#  [2,]    2    4    6    8    2
#  [3,]    3    6    9   12    3
#  [4,]    4    8   12   16    4
#  [5,]    5   10   15    8    5
#  [6,]    6   12    3   16   -5
#  [7,]    7   14    6   24   -4
#  [8,]    8   16    9   32   -3
#  [9,]    9   18   12   12   -2
# [10,]   10   20   15   24   -1
# [11,]   11   22    3   36   -6
# [12,]   12   24    6   48   -7
# [13,]   13   26    9   16   -8
# [14,]   14   28   12   32   -9
# [15,]   15   30   15   48  -10
# [16,]   16   32    3   64    6
# [17,]   17   34    6   20    7
# [18,]   18   36    9   40    8
# [19,]   19   38   12   60    9
# [20,]   20   40   15   80   10

答案 1 :(得分:-1)

setNames(data.frame(lapply(1:4,
                           function(x) seq(x, by = x, length.out = 20)),
                    c(1:5, -5:-1, -6:-10, 6:10)),
         LETTERS[1:5])