循环以创建数据帧并将其保存到列表中

时间:2017-05-19 18:14:54

标签: r for-loop apply lapply

我有下面的示例代码和数据。我想弄清楚的是如何编写一个循环,从数据帧(在本例中为datadf)中创建说x(在此示例中为x = 3)数据帧,并将这些数据帧保存在列表中。我坚持的主要部分是如何将每个数据帧保存到列表中。任何提示都非常感谢。

下面更新的代码似乎只是工作,除了数据帧的起始索引始终保持为1,而不是每次提前10步。有谁知道问题是什么?

更新

N<-3
x<-vector("list",N)
for (i in 1:N)
{
  a<-(1:100)*rnorm(1,0.5)
  b<-(1:100)*rnorm(1,2)

  datadf<-as.data.frame(cbind(a,b))

  n<-10
  t<-50


  datadfn<-datadf[((i-1)*n+1):(t+2*(i-1)*n),]


  x[[i]]<-datadfn

}    
Example Code:

n<-10
t<-50

datadf1<-datadf[1:t,]
datadf2<-datadf[(n+1):(t+n),]
datadf3<-datadf[(2*n+1):(t+2*n),]

dfList<-list(datadf1, datadf2, datadf3)


Data:

dput(datadf)
structure(list(a = c(2.00134717160119, 4.00269434320238, 6.00404151480358, 
8.00538868640477, 10.006735858006, 12.0080830296072, 14.0094302012083, 
16.0107773728095, 18.0121245444107, 20.0134717160119, 22.0148188876131, 
24.0161660592143, 26.0175132308155, 28.0188604024167, 30.0202075740179, 
32.0215547456191, 34.0229019172203, 36.0242490888215, 38.0255962604226, 
40.0269434320238, 42.028290603625, 44.0296377752262, 46.0309849468274, 
48.0323321184286, 50.0336792900298, 52.035026461631, 54.0363736332322, 
56.0377208048334, 58.0390679764346, 60.0404151480358, 62.041762319637, 
64.0431094912381, 66.0444566628393, 68.0458038344405, 70.0471510060417, 
72.0484981776429, 74.0498453492441, 76.0511925208453, 78.0525396924465, 
80.0538868640477, 82.0552340356489, 84.0565812072501, 86.0579283788513, 
88.0592755504524, 90.0606227220536, 92.0619698936548, 94.063317065256, 
96.0646642368572, 98.0660114084584, 100.06735858006, 102.068705751661, 
104.070052923262, 106.071400094863, 108.072747266464, 110.074094438066, 
112.075441609667, 114.076788781268, 116.078135952869, 118.07948312447, 
120.080830296072, 122.082177467673, 124.083524639274, 126.084871810875, 
128.086218982476, 130.087566154077, 132.088913325679, 134.09026049728, 
136.091607668881, 138.092954840482, 140.094302012083, 142.095649183685, 
144.096996355286, 146.098343526887, 148.099690698488, 150.101037870089, 
152.102385041691, 154.103732213292, 156.105079384893, 158.106426556494, 
160.107773728095, 162.109120899697, 164.110468071298, 166.111815242899, 
168.1131624145, 170.114509586101, 172.115856757703, 174.117203929304, 
176.118551100905, 178.119898272506, 180.121245444107, 182.122592615708, 
184.12393978731, 186.125286958911, 188.126634130512, 190.127981302113, 
192.129328473714, 194.130675645316, 196.132022816917, 198.133369988518, 
200.134717160119), b = c(2.05061146723527, 4.10122293447054, 
6.15183440170581, 8.20244586894108, 10.2530573361764, 12.3036688034116, 
14.3542802706469, 16.4048917378822, 18.4555032051174, 20.5061146723527, 
22.556726139588, 24.6073376068232, 26.6579490740585, 28.7085605412938, 
30.7591720085291, 32.8097834757643, 34.8603949429996, 36.9110064102349, 
38.9616178774701, 41.0122293447054, 43.0628408119407, 45.113452279176, 
47.1640637464112, 49.2146752136465, 51.2652866808818, 53.315898148117, 
55.3665096153523, 57.4171210825876, 59.4677325498228, 61.5183440170581, 
63.5689554842934, 65.6195669515287, 67.6701784187639, 69.7207898859992, 
71.7714013532345, 73.8220128204697, 75.872624287705, 77.9232357549403, 
79.9738472221756, 82.0244586894108, 84.0750701566461, 86.1256816238814, 
88.1762930911166, 90.2269045583519, 92.2775160255872, 94.3281274928224, 
96.3787389600577, 98.429350427293, 100.479961894528, 102.530573361764, 
104.581184828999, 106.631796296234, 108.682407763469, 110.733019230705, 
112.78363069794, 114.834242165175, 116.88485363241, 118.935465099646, 
120.986076566881, 123.036688034116, 125.087299501351, 127.137910968587, 
129.188522435822, 131.239133903057, 133.289745370293, 135.340356837528, 
137.390968304763, 139.441579771998, 141.492191239234, 143.542802706469, 
145.593414173704, 147.644025640939, 149.694637108175, 151.74524857541, 
153.795860042645, 155.846471509881, 157.897082977116, 159.947694444351, 
161.998305911586, 164.048917378822, 166.099528846057, 168.150140313292, 
170.200751780527, 172.251363247763, 174.301974714998, 176.352586182233, 
178.403197649469, 180.453809116704, 182.504420583939, 184.555032051174, 
186.60564351841, 188.656254985645, 190.70686645288, 192.757477920115, 
194.808089387351, 196.858700854586, 198.909312321821, 200.959923789056, 
203.010535256292, 205.061146723527)), .Names = c("a", "b"), row.names = c(NA, 
-100L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

只需将您的第二个表达式(t+2*(i-1)*n)更改为(t+(i-1)*n)或与第一个表达式((i-1)*n+t)对齐。另外,在lapply循环上考虑for,因为它的返回值是等于输入seq(N)[1] 1 2 3的列表:

N <- 3
n<-10
t<-50

dfList <- lapply(seq(N), function(i) {      
  a <- (1:100)*rnorm(1,0.5)
  b <- (1:100)*rnorm(1,2)

  datadf <- as.data.frame(cbind(a,b))
  datadf[((i-1)*n+1):((i-1)*n+t),]
})

或者更容易阅读:

dfList <- lapply(seq(N), function(i) {      
  a <- (1:100)*rnorm(1,0.5)
  b <- (1:100)*rnorm(1,2)
  s <- (i-1)*n

  datadf <- as.data.frame(cbind(a,b))
  datadf[(s+1):(s+t),]
})