如何在 R 中复制整个数据框并在原始数据框中添加它?


R 中数据框的复制可以通过 sapply 函数创建,为了设置我们想要重复数据框的次数,我们可以使用 rep.int,times 参数。例如,如果我们有一个数据框 df,并且我们想要创建 5 个 df 的副本并在原始数据框中添加它们,则可以使用 sapply(df,rep.int,times=5)。

示例

 在线演示

考虑以下数据框 -

set.seed(151)
x1<-rnorm(5,21,3)
x2<-rnorm(5,8,1.9)
x3<-rnorm(5,3,0.08)
x4<-rnorm(5,1008,32.4)
df<-data.frame(x1,x2,x3,x4)
df

输出

      x1       x2       x3       x4
1 20.84538 9.486324 2.961236 967.9296
2 23.29721 5.344792 3.044849 960.2204
3 20.55978 6.064207 3.005293 1086.9639
4 20.66044 8.436004 2.892010 1029.8222
5 19.81347 9.277129 2.980567 1018.0453

复制 df 两次 -

sapply(df,rep.int,times=2)

输出

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453

复制 df 三次 -

sapply(df,rep.int,times=3)

输出

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453

复制 df 四次 -

示例

sapply(df,rep.int,times=4)

输出

      x1       x2       x3       x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453
[16,] 20.84538 9.486324 2.961236 967.9296
[17,] 23.29721 5.344792 3.044849 960.2204
[18,] 20.55978 6.064207 3.005293 1086.9639
[19,] 20.66044 8.436004 2.892010 1029.8222
[20,] 19.81347 9.277129 2.980567 1018.0453

复制 df 五次 -

示例

sapply(df,rep.int,times=5)

输出

      x1          x2       x3    x4
[1,] 20.84538 9.486324 2.961236 967.9296
[2,] 23.29721 5.344792 3.044849 960.2204
[3,] 20.55978 6.064207 3.005293 1086.9639
[4,] 20.66044 8.436004 2.892010 1029.8222
[5,] 19.81347 9.277129 2.980567 1018.0453
[6,] 20.84538 9.486324 2.961236 967.9296
[7,] 23.29721 5.344792 3.044849 960.2204
[8,] 20.55978 6.064207 3.005293 1086.9639
[9,] 20.66044 8.436004 2.892010 1029.8222
[10,] 19.81347 9.277129 2.980567 1018.0453
[11,] 20.84538 9.486324 2.961236 967.9296
[12,] 23.29721 5.344792 3.044849 960.2204
[13,] 20.55978 6.064207 3.005293 1086.9639
[14,] 20.66044 8.436004 2.892010 1029.8222
[15,] 19.81347 9.277129 2.980567 1018.0453
[16,] 20.84538 9.486324 2.961236 967.9296
[17,] 23.29721 5.344792 3.044849 960.2204
[18,] 20.55978 6.064207 3.005293 1086.9639
[19,] 20.66044 8.436004 2.892010 1029.8222
[20,] 19.81347 9.277129 2.980567 1018.0453
[21,] 20.84538 9.486324 2.961236 967.9296
[22,] 23.29721 5.344792 3.044849 960.2204
[23,] 20.55978 6.064207 3.005293 1086.9639
[24,] 20.66044 8.436004 2.892010 1029.8222
[25,] 19.81347 9.277129 2.980567 1018.0453

更新于: 2020年10月17日

1K+ 浏览量

启动你的 职业生涯

通过完成课程获得认证

开始学习
广告