如何在R中重复整个数据帧?
要在 R 中重复整个数据帧,我们可以按照以下步骤操作 -
首先,创建一个数据框。
然后,使用 rep 函数重复数据帧。
示例
创建数据框
让我们创建一个数据框,如下所示 -
x<-rnorm(20)输出结果y<-rnorm(20)
z<-rnorm(20)
df<-data.frame(x,y,z)
df
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
x y z1 -0.72553554 -0.003781742 -1.36481696
2 -0.12039265 -0.208840085 0.81715654
3 -1.97592285 0.887936261 1.98675032
4 0.17376084 0.515153192 0.69978704
5 -1.07646565 0.167017490 -2.30298717
6 0.98623624 0.481496490 0.55212882
7 1.66696821 1.454184146 -1.01862076
8 1.00844431 -0.722831649 -0.23451775
9 0.53058163 0.485509531 1.45703838
10 0.23904645 1.456821988 -0.11157839
11 0.50032955 -1.276985702 0.24204625
12 2.28643294 -0.189765951 -0.16021579
13 -0.37134394 -1.144893466 -1.84426120
14 -0.84774565 0.304323165 0.99355776
15 0.03875027 0.034829337 1.19431531
16 -2.13755826 0.877838682 -0.08493668
17 0.66341839 0.910711816 1.75597566
18 -0.76871288 0.998276851 -0.66642423
19 1.85887892 0.762980833 0.23355600
20 0.65030663 2.218971071 -0.35667725
重复数据框
使用 rep 函数重复数据帧 df 两次 -
x<-rnorm(20)输出结果y<-rnorm(20)
z<-rnorm(20)
df<-data.frame(x,y,z)
df[rep(1:nrow(df),2),]
x y z1 -0.72553554 -0.003781742 -1.36481696
2 -0.12039265 -0.208840085 0.81715654
3 -1.97592285 0.887936261 1.98675032
4 0.17376084 0.515153192 0.69978704
5 -1.07646565 0.167017490 -2.30298717
6 0.98623624 0.481496490 0.55212882
7 1.66696821 1.454184146 -1.01862076
8 1.00844431 -0.722831649 -0.23451775
9 0.53058163 0.485509531 1.45703838
10 0.23904645 1.456821988 -0.11157839
11 0.50032955 -1.276985702 0.24204625
12 2.28643294 -0.189765951 -0.16021579
13 -0.37134394 -1.144893466 -1.84426120
14 -0.84774565 0.304323165 0.99355776
15 0.03875027 0.034829337 1.19431531
16 -2.13755826 0.877838682 -0.08493668
17 0.66341839 0.910711816 1.75597566
18 -0.76871288 0.998276851 -0.66642423
19 1.85887892 0.762980833 0.23355600
20 0.65030663 2.218971071 -0.35667725
1.1 -0.72553554 -0.003781742 -1.36481696
2.1 -0.12039265 -0.208840085 0.81715654
3.1 -1.97592285 0.887936261 1.98675032
4.1 0.17376084 0.515153192 0.69978704
5.1 -1.07646565 0.167017490 -2.30298717
6.1 0.98623624 0.481496490 0.55212882
7.1 1.66696821 1.454184146 -1.01862076
8.1 1.00844431 -0.722831649 -0.23451775
9.1 0.53058163 0.485509531 1.45703838
10.1 0.23904645 1.456821988 -0.11157839
11.1 0.50032955 -1.276985702 0.24204625
12.1 2.28643294 -0.189765951 -0.16021579
13.1 -0.37134394 -1.144893466 -1.84426120
14.1 -0.84774565 0.304323165 0.99355776
15.1 0.03875027 0.034829337 1.19431531
16.1 -2.13755826 0.877838682 -0.08493668
17.1 0.66341839 0.910711816 1.75597566
18.1 -0.76871288 0.998276851 -0.66642423
19.1 1.85887892 0.762980833 0.23355600
20.1 0.65030663 2.218971071 -0.35667725
以上是 如何在R中重复整个数据帧? 的全部内容, 来源链接: utcz.com/z/331652.html