可以使用sapply函数在R中创建数据帧的副本,以使用rep.int,times参数设置要重复数据帧的次数。例如,如果我们有一个数据帧df,并且想要创建df的5个副本并将它们添加到原始副本中,则可以使用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 3次-
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