[R] sbusetting data by rows (every 69 rows)

arun smartpink111 at yahoo.com
Sun Aug 25 19:28:54 CEST 2013


#or you could try:

 lst2<- split(dat1,as.numeric(gl(69,69,2000)))
# identical(lst1,lst2)
#[1] TRUE
A.K.




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Sunday, August 25, 2013 1:17 PM
Subject: Re: sbusetting data by rows (every 69 rows)



Hi,
Try:
set.seed(24)
dat1<- as.data.frame(matrix(sample(1:400,2000*16,replace=TRUE),ncol=16))
 lst1<-split(dat1,((seq_len(nrow(dat1))-1)%/%69)+1)
sapply(lst1,function(x) range(as.numeric(row.names(x))))
#    1   2   3   4   5   6   7   8   9  10  11  12  13  14   15   16   17   18
#[1,]  1  70 139 208 277 346 415 484 553 622 691 760 829 898  967 1036 1105 1174
#[2,] 69 138 207 276 345 414 483 552 621 690 759 828 897 966 1035 1104 1173 1242
#       19   20   21   22   23   24   25   26   27   28   29
#[1,] 1243 1312 1381 1450 1519 1588 1657 1726 1795 1864 1933
#[2,] 1311 1380 1449 1518 1587 1656 1725 1794 1863 1932 2000
 str(lst1[[1]])
#'data.frame':    69 obs. of  16 variables:
# $ V1 : int  118 90 282 208 266 369 112 306 321 102 ...
# $ V2 : int  6 50 115 247 355 109 39 297 35 209 ...
# $ V3 : int  313 67 102 298 367 23 376 91 5 38 ...
# $ V4 : int  207 351 212 342 255 399 239 57 234 79 ...
# $ V5 : int  74 80 289 165 231 193 310 255 98 218 ...
# $ V6 : int  99 91 325 143 398 66 201 337 66 382 ...
# $ V7 : int  339 327 325 274 22 105 106 75 400 167 ...
# $ V8 : int  135 233 91 306 230 140 233 166 210 351 ...
# $ V9 : int  204 203 256 337 25 295 214 288 63 388 ...
# $ V10: int  370 328 161 227 381 164 300 313 303 375 ...
# $ V11: int  171 373 133 345 60 119 215 48 55 367 ...
# $ V12: int  118 309 67 250 286 127 171 248 46 20 ...
# $ V13: int  385 15 282 276 130 166 160 214 58 74 ...
# $ V14: int  90 165 39 154 294 84 106 367 359 145 ...
# $ V15: int  392 290 103 14 111 148 200 331 302 88 ...
# $ V16: int  323 210 167 345 249 325 217 171 150 223 ...
 sapply(lst1,nrow)
# 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
#69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 
#27 28 29 
#69 69 68 


A.K.



Hi There, 

It might be a simple problem but I didn't find a clear solution online. 
The task is quite straightforward -- I have a large data frame with 
more than 2000 rows and 16 columns. For further analysis, I need to 
subset every 69 rows into some new data frames. 

I tried to used the "for" command (code as showing below): 

n = nrow(data) 
w = 69 
for(i in 1:(n-w)){ 
  data= data[i:(i+w),] 
  } 

But it only gave me a subset with the last 69 rows. 
So my question is now how to subset the whole data frame with every 69 rows ( 1st to 69th rows, 70th to 139th rows, etc.). 

Any help will be appreciated.



More information about the R-help mailing list